// var baseurl = "http://192.168.2.105:8012/netdisk-web-perpc/";
var baseurl = 'http://wpan.nxjiewei.com:8012/netdisk-web-perpc/';
function guid() {
	function S4() {
		return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
	}
	return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}
var chunkSize = 1048576; //每个切块的大小（1M）
var threads = 2; //线程数
var uuid = guid();
var md5 = '',values = 0;
var uploader=null;
function init(){
	uploader = WebUploader.create({
			swf: 'Uploader.swf',
			server: baseurl+'disk/fileupload/uploadChunk',
			pick: '#picker',
			resize: false,
			auto:true,
			threads:threads,
			compress:false,//默认压缩，对jpg的md5计算产生影响
			chunkRetry:0,
			prepareNextFile:true,
			chunked:true,
			chunkSize:chunkSize,
			duplicate:false,//去重
			formData:{
			}
	});
	//第一步：加入队列
	uploader.on('fileQueued', function( file ) {
		// addNode(file.id,file.name,file.size);
    UIActionProgress.open({
     maskBg: 'rgba(0,0,0,0.5)',
     styles: {
         h: 108,
         bg: '#fff',
         title: {
             size: 13,
             color: '#000',
             marginT: 10
         },
         msg: {
             size: 12,
             color: '#000',
             marginT: 5
         },
         lable: {
             size: 12,
             color: '#696969',
             marginB: 5
         },
         progressBar: {
             size: 2,
             normal: '#000',
             active: '#4876FF',
             marginB: 35,
             margin: 5
         }
     },
     data: {
         title: '正在上传',
         msg: '',
         value: 0
     }
		 },function(ret){
		 });
	});
	//第二步：上传过程计算进度
	uploader.on('uploadProgress', function(file, percentage) {
		UIActionProgress.setData({
		 data: {
		     title: '正在上传...',
		     msg: '',
		     value: percentage*100
		 }
		});
	});
	//第三步：上传完成合并切块
	uploader.on('uploadSuccess', function( file,response ) {
		// updateNode(file.id,3,"上传成功","","");
		uploader.removeFile(file,true);
	});
	//第四步：上传失败
	//报错让前端捕捉有两种方式：1）response.setStatus(500);   2）uploaderAccept返回false
	uploader.on('uploadError', function( file,reason ) {
		// updateNode(file.id,4,"上传失败","","");
		return false;
	});
	uploader.on('uploadAccept', function( object,res ) {
		if(res.code==1){
      api.toast({
          msg: '上传失败',
          duration: 2000,
          location: 'top'
      });
      UIActionProgress.close();
			return false;
		}
	});
}
//计算md5
function getFileMd5(file,deferred,token){
	uploader.md5File(file).then(function(val) {
        var url=baseurl+"disk/fileupload/checkFile";
        md5 = val
      	var args={
      		"filemd5":val,
      		"filename":file.name,
      		"filesize":file.size,
      		"token":token
      	};
      	$.post(url,args,function(data){
      		if(data.code==0){
      			if(data.data==1){
      				uploader.skipFile(file);
      			}
      			deferred.resolve();
      		}else{
      			deferred.reject();
      		}
      	});
    });
}
