// Generated by CoffeeScript 1.8.0
(function() {
  $(function() {
    $(".toast").hide();
    $("#text").html("上传名片");
    var src=GetQueryString("src");
    if(src!=null&&src!=undefined&&src!="null"&&src!=""){
    	$(".head")[0].src=src;
    	$("#photo").html("重新上传名片");
    	var verify=localStorage.getItem("verify");
    	if(verify==0){
    		$(".fn-left").html("等待审核，请耐心等待");
    	}
    	if(verify==1){
    		$(".fn-left").html("您已认证，享有理财师特权");
    	}
    	$("#img").attr("onclick","location.href='./userInfo.html'");
    	$(".head").click(function(){
    		return location.href="../product/previewCertificate.html?src="+src;
    	});
    	
    	$("#photo").on("click",function(){
    		return location.href="./author.html";
    	});
    }else{
    	$(".fn-left").html("认证即享理财师特权");
    	$("#jump").show();
    	$("#photo").html("上传名片");
    	$(".rotate").css('display','block');
    	 $(".head,.card").click(function() {
    	      return $("#camera").click();
    	    });
    	 $("#photo").on("click", function() {
    	    	if(!isNotNull($("[name='imgBase64']")[0],"名片")){
    	    		return;
    	    	}
    	    	return Ajax({
    	            url: baseUrl+'/api/user/apply_certification.do',
    	            data: $("form").serialize(),
    	            success: function(res) {
    	            	if(res.status==200){
    	            		toast.success({msg:"上传成功，等待系统确认"});
    	            		setTimeout(function(){
    	            			return location.href="./index.html";
    	            		},1000);
    	            	}else{
    	            		wrongMsg(res.message);
    	            	}
    	            }
    	          });           
    	    });
    }
    
    $("#camera").localResizeIMG({
      width: 250,
      quality: 0.8,
      success: function(res) {
        var img;
        img = new Image();
        img.src = res.base64;
        res.clearBase64;
        $(".head")[0].src = res.base64;
        $("[name='imgBase64']")[0].value = res.base64;
        if ($(".head").length > 0) {
          $(".head").css({
            "background-image": "url(" + res.base64 + ")"
          }).addClass("radius");
        }
        return $(".card").css({
          "background-image": "url(" + res.base64 + ")"
        });
      }
    });
    $("#jump").on("click",function(){
    	return location.href = "./../product/index.html";
    });
   
  });

}).call(this);

function rotateImg(pid,direction) {  
    //最小与最大旋转方向，图片旋转4次后回到原方向  
    var min_step = 0;  
    var max_step = 3;  
    var img = $("#head")[0];  
    if (img == null||$("[name='imgBase64']")[0].value==null||$("[name='imgBase64']")[0].value=='')return;  
    //img的高度和宽度不能直接获取，否则会出错  
    var imgTem;
    imgTem = new Image();
    imgTem.src = img.src;
    //延迟处理，给客户端响应时间
    setTimeout(function(){
    	 var width = imgTem.width;  
    	    var height = imgTem.height;
    	    var step = img.getAttribute('step');  
    	    if (step == null) {  
    	        step = min_step;  
    	    }  
    	    if (direction == 'right') {  
    	        step++;  
    	        //旋转到原位置，即超过最大值  
    	        step > max_step && (step = min_step);  
    	    } else {  
    	        step--;  
    	        step < min_step && (step = max_step);  
    	    }  
    	    img.setAttribute('step', step);
    	    var canvas = document.getElementById('pic_' + pid);  
    	    if (canvas == null) {  
    	        img.style.display = 'none';  
    	        canvas = document.createElement('canvas');  
    	        canvas.setAttribute("style","position: relative;left: 0px;margin-left: 0px;");
    	        canvas.setAttribute('id', 'pic_' + pid);  
    	        
    	        img.parentNode.appendChild(canvas);  
    	    }  
    	    //旋转角度以弧度值为参数  
    	    var degree = step * 90 * Math.PI / 180;  
    	    var ctx = canvas.getContext('2d'); 
    	    //图片过大，进行缩放
    	    var scale=1;
    	    if(height>width){
    	    	if(height>280){
    	    		scale=280/height;
    	    	}
    	    }else{
    	    	if(width>280){
    	    		scale=280/width;
    	    	}
    	    }
    	    switch (step) {  
    	        case 0:  
    	            canvas.width = width*scale;  
    	            canvas.height = height*scale;  
    	            ctx.scale(scale,scale);
    	            ctx.drawImage(img, 0, 0);  
    	            break;  
    	        case 1:  
    	            canvas.width = height*scale;  
    	            canvas.height = width*scale;  
    	            ctx.rotate(degree);  
    	            ctx.scale(scale,scale);
    	            ctx.drawImage(img, 0, -height); 
    	            break;  
    	        case 2:  
    	            canvas.width = width*scale;  
    	            canvas.height = height*scale;  
    	            ctx.rotate(degree);  
    	            ctx.scale(scale,scale);
    	            ctx.drawImage(img, -width, -height);  
    	            break;  
    	        case 3:  
    	            canvas.width = height*scale;  
    	            canvas.height = width*scale;  
    	            ctx.rotate(degree);  
    	            ctx.scale(scale,scale);
    	            ctx.drawImage(img, -width, 0);  
    	            break;  
    	    }  
    	    //将canvas转换成Base64编码的URL
    	    $("[name='imgBase64']")[0].value=canvas.toDataURL();
    },500);
  
} 
