// Generated by CoffeeScript 1.8.0
(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;
    } 
  $(function() {
    $(".toast").hide();
    $("#text").html("上传头像");
    var src=GetQueryString("src");
    if(src!=null&&src!=undefined&&src!="null"&&src!=""){
    	$(".head")[0].src=src;
    	$("#headPhoto").html("重新上传头像");
    	$(".head").click(function(){
    		return location.href="../product/previewCertificate.html?src="+src;
    	});
    	
    	$("#headPhoto").on("click",function(){
    		return location.href="../product/photo.html";
    	});
    }else{
    	$("#headPhoto").html("上传头像");
    	$(".rotate").css('display','block');
    	 $(".head,.card").click(function() {
    	      return $("#camera").click();
    	    });
    	  $("#headPhoto").on("click", function() {
    	    	if(!isNotNull($("[name='imgBase64']")[0],"头像")){
    	    		return;
    	    	}
    	    	return Ajax({
    	            url: baseUrl+'/api/user/modify_head.do',
    	            data: $("form").serialize(),
    	            type:'post',
    	            success: function(res) {
    	            	if(res.status==200){
    	            		$.ajax({
    	            	    	url:baseUrl+"/api/user/get_user_info.do",
    	            	       
    	            	        success:function(data){
    	            	        	if(data.status==200){
    	            	        		var user=data.data.userinfo;
    	            	        		if(user.role==1&&user.isVerify==1){
    	            	        			return location.href="../user/userCenter.html";
    	            	        		}else{
    	            	        			return location.href="../user/index.html";
    	            	        		}
    	            	        	}else{
    	            	        		wrongMsg(data.message);
    	            	        		setTimeout(function(){
    	            	        			return location.href="./login.html";
    	            	        		},1000);
    	            	        		
    	            	        	}
    	            	        }
    	            	    });
    	            	}
    	            }
    	          });           
    	    });
    }
    
    $("#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 + ")"
        });
      }
    });
   
    
  });

}).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('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>140){
   	    		scale=140/height;
   	    	}
   	    }else{
   	    	if(width>140){
   	    		scale=140/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);
}  
