/*文件上传*/
function skyUpload(upid,url,success,error,uploadProgress)
{
		 var vFD = new FormData();
		 var f= document.getElementById(upid).files;
		 $("#"+upid+"loading").show();
		 for(var i=0;i<f.length;i++){ 
		vFD.append('upimg', document.getElementById(upid).files[i]);
		// create XMLHttpRequest object, adding few event listeners, and POSTing our data
		var oXHR = new XMLHttpRequest();        
		oXHR.addEventListener('load', success, false);
		oXHR.addEventListener('error', error, false);
		if(uploadProgress!=undefined){
			oXHR.upload.addEventListener("progress", uploadProgress, false);
		}
		oXHR.open('POST',url);
		oXHR.send(vFD);
	
		 }
}
$(function(){
	$(document).on("click",".js-upload-btn",function(){
			var p=$(this).parents(".js-upload-item");
			p.find(".js-upload-file").click();
		});
		
		$(document).on("change",".js-upload-file",function(){
			var $p=$(this).parents(".js-upload-item");
			var id=$(this).attr("id");
			var url=$p.attr("url");
			skyUpload(id,url,function(e){
				var data=eval("("+e.target.responseText+")");
				if(data.error==0){
					var html='<img src="'+data.data.trueimgurl+'.100x100.jpg">';
					$p.find(".js-imgbox").html(html);
					$p.find(".js-imgurl").val(data.data.imgurl);
				}
			},function(e){
				
			},function(evt){
				 
				 
				if (evt.lengthComputable) {
					$p.find(".js-upload-loading").show();
					var uptotal=document.getElementById(id).files[0].size;
	                var percentComplete = Math.round((evt.loaded )* 100 / uptotal);
					percentComplete =Math.min(100,percentComplete);
					
	                $('#picsup-progress').css("width",percentComplete.toString() + '%');
					if(evt.loaded==evt.total){
						
						$p.find('.js-upload-loading').css("width",'100%');
						$p.find(".js-upload-loading").hide();
					}
					
	            }
	            else {
	                $p.find(".js-upload-loading").hide();
	            }
			})
		})
})
