//微信分享的简介与用户名称
var wx_share_title = "";
var wx_share_product_shortername = "";
// Generated by CoffeeScript 1.8.0
(function() {
  var drawCircle, drawProcess, loadData;
  function GetQueryString(name)
  {
       var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
       var r = window.location.search.substr(1).match(reg);
       if(r!=null)return  unescape(r[2]); return null;
  }
  function getarg(url){
	  var arg=url.split("#");
	  if(arg[1]!=null&&arg[1]!=undefined){
		  arg=arg[1].split("=");
		  return arg[1];
	  }else return null;
  } 
  var productId =GetQueryString("id");
  loadData = (function(_this) {
    return function() {
      return _this.Ajax({
        url: _this.baseUrl + '/api/product/product.do',
        data: {
          productId: productId
        },
        callback: function(data) {
          var basic, descri, subscribe, commis,issue;
          basic = data.data.basic;
          descri = data.data.descri;
          subscribe = data.data.subscri;
          issue = data.data.issue;
          commis = data.data.commis;
          if(subscribe.accountName!=null&&subscribe.accountName!="" && subscribe.accountNumber!=null&&subscribe.accountNumber!="" &&subscribe.accountOpenBank!=null&&subscribe.accountOpenBank!=""){
        	  var _open_bank_info = "账户名：" + subscribe.accountName + "<br/>开户行：" + subscribe.accountOpenBank + "<br/>账　号：" + subscribe.accountNumber;
        	  $(".accountInfo").html(_open_bank_info);
        	  $("#accountInfo").attr("style","");
          }
         
         
          if(basic.shorterName!=null&&basic.shorterName!=undefined){
        	  $(".shorterName").html(basic.shorterName);
          }
          if(descri!=undefined&&descri!=null){
        	  if(descri.riskInfo!=undefined&&descri.riskInfo!=null){
            	  descri.riskInfo.replace("\r\n","<br/>");
            	  $(".riskInfo").html(descri.riskInfo);
              }
              if(descri.mihfInfo!=null&&descri.mihfInfo!=undefined){
            	  descri.mihfInfo.replace("\r\n","<br/>");
            	  $(".mihfInfo").html(descri.mihfInfo);
              }
              if(descri.teamInfo!=null&&descri.teamInfo!=undefined){
            	  descri.teamInfo.replace("\r\n","<br/>");
            	  $(".teamInfo").html(descri.teamInfo);
              }
              if(descri.subscribeInfo!=null&&descri.subscribeInfo!=undefined){
            	  descri.subscribeInfo.replace("\r\n","<br/>");
            	  $(".subscribeInfo").html(descri.subscribeInfo);
            	  $("#subscribeInfo").attr("style","");
              }
              $(".roadshowInfo").html(descri.roadshowInfo);
          }
          var appointment, payment, _appointment, _payment, appointmentPercent, paymentPercent, time,orderCount;
			appointment = data.data.appointment;
			payment = data.data.payment;
			_appointment = data.data._appointment;
			_payment = data.data._payment;
			appointmentPercent = data.data.appointmentPercent;
			paymentPercent = data.data.paymentPercent;
			time = data.data.time;
			orderCount = data.data.orderCount;
			
			wx_share_title = basic.brightPoint;
			wx_share_product_shortername = basic.shorterName;
			if(basic.status===2||basic.status==="2"){
				$("#bottom_bar").hide();
				$("#appointment1").html("运行中(暂不可预约)");
				$("#appointment1").attr("style","width:100%");
			}
			if(basic.status===0||basic.status==="0"){
				$("#bottom_bar").hide();
				$("#appointment1").html("已清盘");
				$("#appointment1").attr("style","width:100%");
			}
			if(basic.status===1||basic.status==="1"){
				$("#bottom_bar1").hide();
				$("#bottom_bar").show();
			}

			if(_appointment<=0){
				$("#appointmentPrice").html("已抢完");
			}else if(basic.status===1||basic.status==="1"){
				$("#appointmentPrice").html("已抢购<font color=\"#ed6d00\" size=\"3\">" +
						appointment + "</font>万元,还可抢购<font color=\"#ed6d00\" size=\"3\">" + _appointment
						+ "</font>万元");
			}
			else{
				appointmentPercent="100";
				$("#appointmentPrice").html("已抢完");
			}
			if(_payment<=0){
				$("#paymentPrice").html("已售罄");
			}else if(basic.status===1||basic.status==="1"){
				$("#paymentPrice").html("已打款<font color=\"#e0c248\" size=\"3\">" +
						payment + "</font>万元,还可进款<font color=\"#e0c248\" size=\"3\">" + _payment + "</font>万元");
			}
			else{
				paymentPercent="100";
				$("#paymentPrice").html("已售罄");
			}	
			drawProcess(appointmentPercent, paymentPercent,
					appointmentPercent+"%", time);
          return $(document).on("tap",".roadshowInfo img",function(){
        	  location.href="./preview.html?productId="+basic.productId+"&src="+$(this).attr("src");
          });
        }
      });
    };
  })(this);

  drawCircle = function(e, radil, process, color) {
    if (radil == null) {
      radil = 95;
    }
    if (process == null) {
      process = "0";
    }
    if (color == null) {
      color = "red";
    }
    process = process > 100 ? 100 : process;
    e.beginPath();
    e.moveTo(95, 95);
    if (process != 100) {
    	var _p = process / 100;
	    var _percent = _p >= 0.25 ? Math.PI * 2 * _p - Math.PI * 0.5 : 1.5*Math.PI + Math.PI * 2 * _p; 
	    e.arc(95, 95, radil, 1.5*Math.PI, _percent, false);
    } else {
    	e.arc(95, 95, radil, 0, Math.PI * 2, false);
    }
    e.closePath();
    e.fillStyle = color;
    return e.fill();
  };

  drawProcess = function(process1, process2, precent,time) {
    if (process1 == null) {
      process1 = "90";
    }
    if (process2 == null) {
      process2 = "70";
    }
    if (precent == null) {
      precent = "30%";
    }if(time == null){
    	time = "剩余0天0小时";
    }
    return $("canvas.process").each(function() {
      var context;
      context = this.getContext("2d");
      context.clearRect(0, 0, 190, 190);
      drawCircle(context, 90, 100, "#f9f9f9");
      drawCircle(context, 88, process1, "#ed6d00");
      drawCircle(context, 85, 100, "#f9f9f9");
      drawCircle(context, 80, 100, "#e0e0df");
      drawCircle(context, 78, process2, "#d6ad3d");
      drawCircle(context, 75, 100, "#e0e0df");
      drawCircle(context, 68, 100, "#FFF");
      context.font = "bold 10pt Arial";
      context.fillStyle = '#d6ad3d';
      context.textAlign = 'center';
      context.textBaseline = 'middle';
      context.fillText("已抢购", 95, 65);
      context.font = "bold 20pt Arial";
      context.fillText(precent, 95, 95);
      context.font = "bold 10pt Arial";
      return context.fillText(time, 95, 125);
    });
  };

  $(function() {
    var cur_index;
    loadData();
    var appId='wxa0eadefda1950ee0';
    var timestamp;
    var nonceStr;
    var signature;
    var uri=location.href;
    var id=GetQueryString("id");
    var uid=$.cookie("_id");
    $.ajax({
    	url: baseUrl+"/api/product/wx.do",
    	data: {
    		url: uri
    	},
    	success:function(data){
    		var wxs=data.data.url;
    		timestamp=wxs.timeStamp;
    		nonceStr=wxs.nonceStr;
    		signature=wxs.signature;
    		wx.config({
    	        debug: false,

    	        appId: appId,
    	        timestamp: timestamp,
    	        nonceStr: nonceStr,
    	        signature: signature,

    	        jsApiList: [
    	          'checkJsApi',
    	          'onMenuShareTimeline',
    	          'onMenuShareQQ',
    	          'onMenuShareAppMessage',
    	        ]
    	    });
    		wx.ready(function(){

    		    wx.checkJsApi({
    		      jsApiList: [
    		          'onMenuShareTimeline',
    				  'onMenuShareQQ',
    				  'onMenuShareAppMessage'
    		      ],
    		      success: function (res) {
    		      }
    		    });
    		    wx.onMenuShareTimeline({
    			      title: wx_share_product_shortername,
    			      link: "http://m.tao798.com/api/product/retweeted.html?id="+id+"#uid="+uid ,

    			      imgUrl: 'http://m.tao798.com/static/images/share_logo.png',
    			      trigger: function (res) {
    			      },
    			    
    			      success: function (res) {
    			      },
    			      cancel: function (res) {
    			      }
    			    });
    		    wx.onMenuShareQQ({
    			      title: wx_share_product_shortername,
    			      link: "http://m.tao798.com/api/product/retweeted.html?id="+id+"#uid="+uid ,
    			      imgUrl: 'http://m.tao798.com/static/images/share_logo.png',			      
    			      complete: function (res) {
    			        alert(JSON.stringify(res));
    			      },
    			      success: function (res) {
    			      },
    			      cancel: function (res) {
    			      }
    			    });
    		    wx.onMenuShareAppMessage({
    		        title: wx_share_product_shortername, // 分享标题
    		        desc: wx_share_title, // 分享描述,产品亮点数据
    		        link: "http://m.tao798.com/api/product/retweeted.html?id="+id+"#uid="+uid , // 分享链接
    		        imgUrl: 'http://m.tao798.com/static/images/share_logo.png', // 分享图标
    		        success: function () { 
    		        },
    		        cancel: function () { 
    		        }
    		    });
    	});
    	}
    });
    
    
    cur_index = 0;
    $(".tab_nav .am-flexbox-item").on("click", function() {
      var val;
      if (cur_index !== $(this).index()) {
        cur_index = $(this).index();
        val = cur_index === 1 ? "83% 0" : "17% 0";
        $(this).addClass("active").siblings().removeClass("active").parent().prev(".bg_sj").css("background-position", val);
        return $(".tab_content>div").eq(cur_index).addClass("active").siblings().removeClass("active");
      }
    });
    $(".item-name").on("click", function() {
       $(this).parent().toggleClass("active");
    });
    //判断是否有用户信息
    $.ajax({
    	url:baseUrl+'/api/user/get_verification_info.do',
    	data:{
    		favoriteType:2,
    		relationId:id
    	},
          success:function(data){
	    		if(data.status===200){
	    			if(data.data!=undefined&&data.data.userinfo.isVerify==1){
	    				$("#scheme").show();
		    			$(".icon-email").show();
	    			}
	    			if(data.data!=undefined&&data.data.userVinfo!=undefined){
	    				$('#icon_collect').addClass("cancle");
				        $('#icon_collect').css("background-image","url(../../static/images/collected.png)");
	    			}
	    			
	    			}else if(data.status===311){
	    				$("#icon_collect").click(function(){
	    					   localStorage.clear();	
					    	   localStorage.setItem("id", productId);
	    						return location.href="../user/login.html";
	    					//},1000);
	    				});
	    			}
	    			
	    		}
    });
    
    $("#icon_collect").click(function(){
   	 var productId =GetQueryString("id");
   	 if($('#icon_collect').hasClass("cancle")){
   		 $.ajax({
		    	url:baseUrl+'/api/user/cancel_collect.do',
		    	data:{
		    		favoriteType:2,
		    		relationId:productId
		    	},	 	    				    
			    success:function(data){
			    	if(data.status===311){	
			    	   localStorage.clear();	
			    	   localStorage.setItem("id", productId);
			    	   //toast.show({msg:"还未登录,请先登录"});
			    	   //setTimeout(function(){
			    		   return location.href="../user/login.html";
			    	   //},1000);
			    	}else if(data.status===200){
			    	   $('#icon_collect').removeClass("cancle");
			    	   $('#icon_collect').css("background-image","url(../../static/images/collect.png)");
			        }else{
			        	wrongMsg(data.message);
			        }
			  }
	    	});
   	 }else{
			    	$.ajax({
				    	url:baseUrl+'/api/user/collect.do',
				    	data:{
				    		favoriteType:2,
				    		relationId:productId,
				    		orginUri:location.href,
				    		thumbnail:"../../static/images/product_collect.jpg"
				    	},
					    success:function(data){
					    	if(data.status===311){	
					    	   localStorage.clear();	
					    	   localStorage.setItem("id", productId);
					    	   //toast.show({msg:"还未登录,请先登录"});
					    	   //setTimeout(function(){
					    		   return location.href="../user/login.html";
					    	   //},1000);
					    	}else if(data.status===200){
					    		$('#icon_collect').addClass("cancle");
						        $('#icon_collect').css("background-image","url(../../static/images/collected.png)");
					        }else{
					        	wrongMsg(data.message);
					        }
					  }
			    	});
			}
   	
   });
   
    $("#scheme").on("click", function() {
    	 function GetQueryString(name)
    	  {
    	       var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    	       var r = window.location.search.substr(1).match(reg);
    	       if(r!=null)return  unescape(r[2]); return null;
    	  }
    	  var productId =GetQueryString("id");
      $.ajax({
    	  url:baseUrl+'/api/product/commission.do',
    	  data:{
    		  productId:productId
    	  },
    	  beforeSend: function() {
              $$.am_toast.innerHTML = tipTpl.loading;
              return toast.show();
            },
    	  success:function(data){
    		  
    		  if(data.status===200){
    			  toast.hide(200);
    			  var commis,index,height,bottom;
        		  commis = data.data.commis;
        		  $("#commission").html('<div id="commissionList"></div>');
        		  $.each(commis,function(idx,item){ //循环对象取值
                      $("#commissionList").after("<div class='am-flexbox content'>" +
                      		"<div class='am-flexbox-item'>" +
                      		item.amount +
                      		"</div><div class='am-flexbox-item'>" +
                      		item.commission +
                      		"</div></div>");
                      index=idx;
                  });
        		  if(index==undefined||index==null||index==""){
        			  index=0;
        		  }
        		  	  height = "height:" + (index+1)*70 + "px";
        			  $("#share").show();
        			  $("#dv_scheme").attr("style",height);
        			  $("#dv_scheme").addClass("tran_active");
    		  }else if(data.status===321){
    			  toast.hide(2000);
    			  localStorage.clear();	
	    			localStorage.setItem("id", productId);
	    			toast.show({msg:"还未认证,请先认证"});
	    			setTimeout(function(){
	    				return location.href="../user/author.html";
		    		  },1000);
    		  }else{
    			  toast.hide(2000);
    			  localStorage.clear();	
	    		  localStorage.setItem("id", productId);
	    		  return location.href="../user/login.html";
    		  }
    		  
    	  }
           
      });	
    });
    
    $("#appointment").on("click", function() {
    	/*
    }
   	 function GetQueryString(name)
   	  {
   	       var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
   	       var r = window.location.search.substr(1).match(reg);
   	       if(r!=null)return  unescape(r[2]); return null;
   	  }
   	  var productId =GetQueryString("id");
   	  var _id=$.cookie("_id");
   	  if(_id!=null){
   	  */
   		  return location.href='../product/appointment.html?productId='+productId;
   		  /*
   	  }else{
   		  toast.hide(2000);
		  localStorage.clear();	
		  localStorage.setItem("id", productId);
		  //toast.show({msg:"还未登录,请先登录"});
		  //setTimeout(function(){
			 return location.href="../user/login.html";
  		  //},1000);
   	  }
   	  */
    });
    $("#share").click(function() {
    	  var $dv_scheme;
        $dv_scheme = $("#dv_scheme");
  	  if ($dv_scheme.hasClass("tran_active")) {
      	  $("#share").hide();
      	
      	  $dv_scheme.removeClass("tran_active");
        }
    });
    
   return $(".icon-email").on("click", function(_this) {
    	$.ajax({
	    	url:baseUrl+'/api/user/get_user_info.do',
	    	success:function(data){
	    		 function GetQueryString(name)
	    		  {
	    		       var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	    		       var r = window.location.search.substr(1).match(reg);
	    		       if(r!=null)return  unescape(r[2]); return null;
	    		  }
	    		  var productId =GetQueryString("id");
	    		if(data.status===311){	
	    		  localStorage.clear();	
	    		  localStorage.setItem("id", productId);
	    		  //toast.show({msg:"还未登录,请先登陆"});
	    		  //setTimeout(function(){
	    			  return location.href="../user/login.html";
	    		  //},1000);
	    		}else if(data.data.userinfo.isVerify===1){
	    			$(".emailTitle").html("发送产品资料到邮箱");
	    			$("#uEmail").val("");
	    			$("#uEmail").removeAttr("disabled");
	    	        $("#mask").show();
	    	    $("#cancle").on("click", function() {
	    	      return $("#mask").hide();
	    	    });
	    	    $("#sure").on("click", function() {
	    	      var el;
	    	      el = $("#uEmail")[0];
	    	      if (!isEmail(el, "邮箱")) {
	    	        return;
	    	      };
	    	     
	    	      function GetQueryString(name)
	    	      {
	    	           var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	    	           var r = window.location.search.substr(1).match(reg);
	    	           if(r!=null)return  unescape(r[2]); return null;
	    	      }
	    	      var productId =GetQueryString("id");
	    	     return $.ajax({
	    	        url: baseUrl + '/api/user/send_attachment.do',
	    	        type:"post",
	    	        data: {
	    	          receiveUser: el.value,
	    	          productId:productId,
	    	          productName:$(".shorterName").html()
	    	        },
	    	        beforeSend: function() {
	    	        	$("#mask").hide();
	    	            $$.am_toast.innerHTML = tipTpl.loading;
	    	            toast.show();
	    	          },
	    	        success: function(data) {
	    	        	$("#mask").hide();
	    	          if(data.status==200){
	    	        		  toast.success({msg:"发送成功"});
	    	        		  setTimeout(function(){
	    	        			  $$.am_toast.innerHTML=tipTpl.loading;
	    	        			  toast.hide();
	    	        		  },1000);
	    	        		  return el.value = "";
	    	          }else{
	    	        	  wrongMsg(data.message);  
	    	        	  setTimeout(function(){
    	        			  $$.am_toast.innerHTML=tipTpl.loading;
    	        			  toast.hide();
    	        		  },1000);
	    	          }
	    	        }
	    	      });
	    	    });
	    		}else if(data.data.userinfo.isVerify===0){
	    			toast.hide(2000);
	    			localStorage.clear();	
	    			localStorage.setItem("id", productId);
	    			toast.show({msg:"还未认证,请先认证"});
	    			setTimeout(function(){
	    				return location.href="../user/author.html";
		    		  },1000);
	    		}
	    	}
    	});
	});
    
  });

}).call(this);

