// 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;
  }
  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","");
          }
          
          descri.mihfInfo.replace("\r\n","<br/>");
          descri.riskInfo.replace("\r\n","<br/>");
          descri.teamInfo.replace("\r\n","<br/>");
          $(".shorterName").html(basic.shorterName);
          $(".mihfInfo").html(descri.mihfInfo);
          $(".riskInfo").html(descri.riskInfo);
          $(".teamInfo").html(descri.teamInfo);
          if(descri.subscribeInfo!=null&&descri.subscribeInfo!=undefined){
        	  descri.subscribeInfo.replace("\r\n","<br/>");
        	  $(".subscribeInfo").html(descri.subscribeInfo);
        	  $("#subscribeInfo").attr("style","");
          }
          
          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;
			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);		
          $(".roadshowInfo").html(descri.roadshowInfo);
          return $(document).on("click",".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);
    });
  };
  var getarg = function(url){
  	  var arg=url.split("#");
  	  if(arg[1]!=null&&arg[1]!=undefined){
  		  arg=arg[1].split("=");
  		  return arg[1];
  	  }else return null;
  	};
  	var GetQueryString = function(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() {
    var cur_index;
    var _id=$.cookie("_id");
    loadData();
    var uid = getarg(location.href);
    var phone;
    if(uid!=null&&uid!=""&&uid!="null"){
    	$.ajax({
    		url:baseUrl+'/api/user/get_user_verify.do',
    		data:{
    			uid:uid
    		},
	    	beforeSend: function() {
	            $$.am_toast.innerHTML = tipTpl.loading;
	            return toast.show();
	          },
	          success:function(data){
	        	  var user;
		    		if(data.status===200){	
		    			toast.hide(2000);
		    		  user = data.data.user;
		    		  phone = user.mobile;
		    		  $(".name").html(user.name);
		    		  $(".tip").html(user.companyName);
		    		  var portrait_uri = user.portraitUri;
		    		  if (portrait_uri != null && portrait_uri != '') {
		    			  $("#portrait_uri").attr("src", "/"+portrait_uri);
		    			  localStorage.setItem("y_portrait_uri", portrait_uri);
		    		  }
		    		  $("#user").attr("style","display:block");
		    		  $("#user2").attr("style","display:none");
		    		}else if(data.status===321){
		    			$("#user").attr("style","display:none");
		    			$("#user2").attr("style","display:block");
		    		}
	          }
    	});
    }else{
    	$("#user").attr("style","display:none");
		$("#user2").attr("style","display:block");
    }
    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 ? "75% 0" : "25% 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");
    });
    $("#appointment").on("tap", function(e) {
   	  var productId =GetQueryString("id");
   	  e.stopPropagation();
   	  return location.href='../product/subscribe.html?productId='+productId+"&uid="+uid;
    });
    $("#user").on("tap",function(e) {
        return location.href = "tel:" + phone;
        
    });
    $("#subscribe").on("click", function(e) {
      	var productId =GetQueryString("id");
      	e.stopPropagation();
      	return location.href='../product/subscribe.html?productId='+productId;
       });
  });

}).call(this);

