/**
 *
 */
function getRootPath(){
    //获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
    var curWwwPath=window.document.location.href;
    //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
    var pathName=window.document.location.pathname;
    var pos=curWwwPath.indexOf(pathName);
    //获取主机地址，如： http://localhost:8083
    var localhostPath=curWwwPath.substring(0,pos);
    
    //获取带"/"的项目名，如：/uimcardprj
    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
//    projectName = "/wypm-admin";
    return(localhostPath+projectName);
};

var uploadify_onSelectError = function(file, errorCode, errorMsg) {
    var msgText = "上传失败\n";
    switch (errorCode) {
        case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
            //this.queueData.errorMsg = "每次最多上传 " + this.settings.queueSizeLimit + "个文件";
            msgText += "每次最多上传 " + this.settings.queueSizeLimit + "个文件";
            break;
        case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
            msgText += "文件大小超过限制( " + this.settings.fileSizeLimit + " )";
            break;
        case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
            msgText += "文件大小为0";
            break;
        case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
            msgText += "文件格式不正确，仅限 " + this.settings.fileTypeExts;
            break;
        default:
            msgText += "错误代码：" + errorCode + "\n" + errorMsg;
    }
    alert(msgText);
};

var uploadify_onUploadError = function(file, errorCode, errorMsg, errorString) {  
    // 手工取消不弹出提示  
    if (errorCode == SWFUpload.UPLOAD_ERROR.FILE_CANCELLED  
            || errorCode == SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED) {  
        return;  
    }  
    var msgText = "上传失败\n";  
    switch (errorCode) {  
        case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:  
            msgText += "HTTP 错误\n" + errorMsg;  
            break;  
        case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:  
            msgText += "上传文件丢失，请重新上传";  
            break;  
        case SWFUpload.UPLOAD_ERROR.IO_ERROR:  
            msgText += "IO错误";  
            break;  
        case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:  
            msgText += "安全性错误\n" + errorMsg;  
            break;  
        case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:  
            msgText += "每次最多上传 " + this.settings.uploadLimit + "个";  
            break;  
        case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:  
            msgText += errorMsg;  
            break;  
        case SWFUpload.UPLOAD_ERROR.SPECIFIED_FILE_ID_NOT_FOUND:  
            msgText += "找不到指定文件，请重新操作";  
            break;  
        case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:  
            msgText += "参数错误";  
            break;  
        default:  
            msgText += "文件:" + file.name + "\n错误码:" + errorCode + "\n"  
                    + errorMsg + "\n" + errorString;  
    }  
    alert(msgText);  
    return parameters;  
}  

var uploadify_onSelect = function(){  
	   
}; 

$(function(){
	uploadify();
});
function uploadify(){
	setTimeout(function(){
	    $("#uploadify").uploadify({
	        'swf' 		: getRootPath()+'/resources/Scripts/vendor/uploadify/uploadify.swf',
	        'cancelImg' : getRootPath()+'/resources/Scripts/vendor/uploadify/uploadify-cancel.png',
	        'uploader' : getRootPath()+'/uploadify/uploadFile.do',
	        'queueID' : 'fileQueue',//文件在页面的显示队列的id
	        'queueSizeLimit' : 8,//可上传文件的个数
	        'auto' : false,
	        'method':'post',
	        'width'  : '72',
	        'height'  : '23',
	        'progressData':'speed',//显示进度条
	        'removeTimeout':0.5,
	        'multi' : true,
	        'fileSizeLimit': '8MB',//限制上传文件大小
	        'buttonText' : '选择文件',
	        'buttonClass' : 'btn btn-primary',
	        'overrideEvents' : [ 'onDialogClose', 'onUploadSuccess', 'onUploadError', 'onSelectError' ],  
		    'onSelect' : uploadify_onSelect,  
		    'onCancel':function(file){alert('文件：'+file.name+'取消！')},
		    'onSelectError' : uploadify_onSelectError,  
		    'onUploadError' : uploadify_onUploadError,  
	        'onUploadSuccess' : function(file, data, response) { //成功提醒 
	        	 alert('文件： ' + file.name 
	        	            + ' 上传成功！ ' 
	        	            );
	        	var jsonResult = eval('('+data+')');
	        	var trid=jsonResult.id;
	        	var filename = jsonResult.file_name;
				$("#fileInfo").append("<tr id="+trid+"><td>"+file.name+"</td>  <td>"+jsonResult.uploadTime+
						"</td> <td><a class='red' href=\"javascript:deleteTR('" + trid + "','"+ filename 
						+ "');\">删除</a></td><input type='hidden' name='id' value='"
						+jsonResult.id+"'></tr>")
	       	 	}
    	});
	},10);
}
function deleteTR(index,filename) {
	$("tr[id='"+index+"']").remove();
    $.post(''+getRootPath()+'/uploadify/deleteFile.do?id='+index+'&&name='+encodeURI(encodeURI(filename))+'');
}