<%@ Page Language="C#" Inherits="IWMS.Admin.TinyMceSwfUpload"%>
<%
String container = "";
String batContainer = "swfuploadBatch";
String filetypes = "";
String description = "";

container = "swfuploadcontainer";

switch(Request.QueryString["type"]){
	case "img" :
		filetypes = "jpg,gif,bmp,png";
		description = "图片文件";
		break;
	case "media" :
		filetypes = "mp3,wav,mp4,swf,mpg,mpeg,wmv,avi,rm,ra,rmvb,asf,mov,flv";
		description = "媒体文件";
		break;
	default :
		filetypes = "jpg";
		description = "jpg图片";
		break;
}
string[] ctype = config.UploadType.Split(new Char[]{','});
string[] ftype = filetypes.Split(new Char[]{','});
filetypes="";
for(int i=0;i<ftype.Length;i++){
	for(int j=0;j<ctype.Length;j++){
		if(ftype[i]==ctype[j]){
			if(filetypes.Length>0){
				filetypes+=",";
			}
			filetypes+=ftype[i];
			break;
		}
	}
}
if(filetypes.Length==0){
	filetypes="jpg";
}
filetypes = "*."+filetypes.Replace(",",";*.");
%>
var swfu, swfu2;
var uLimit = <%=uploadLimit%>;
var batchHtml = "";

function dialogComplete(numFilesSelected, numFilesQueued){
	if (numFilesQueued > 0){
		this.startUpload();
	}else{
		var msg="你没选择文件或不符合以下要求";
		msg += "\r\n·上传文件最大限制"+ <%=config.UploadSize%> +"KB";
		if (uLimit>0){
			msg += "\r\n·今天还可上传 "+ (uLimit-swfu.getStats().successful_uploads) +" 个文件";
		}
		alert(msg);
	}
}

function uploadStart(file){
	dopost.show();
	swfu.setButtonDisabled(true);
	if(swfu2){
		swfu2.setButtonDisabled(true);
	}
}

function uploadSuccessSingle(file, responseText){
//	alert(file.name + " saveas " + responseText);
	dopost.hide();
	swfu.setButtonDisabled(false);
	if(swfu2){
		swfu2.destroy(false);
	}
	if(responseText.indexOf("<html>")>=0){
		alert("文件超过IIS上传大小限制或服务器错误");
		return;	
	}
//	eval("var jsObj={'success':false}");
	eval(responseText);
	if (jsObj.success){
		var oSrc = document.getElementById("src");
		var uSuccess = this.getStats().successful_uploads;
		var logFunction = tinyMCEPopup.getParam('swfupload_log_function',function(s){});
		oSrc.value = jsObj.msg;
		oSrc.onchange();
		// 添加新闻时记录上传文件
		logFunction(jsObj.msg);
		if (uLimit > 0){
			if (uSuccess<uLimit && swfu2){
				swfu2.setFileUploadLimit(uLimit-uSuccess);
				swfu2.setFileQueueLimit(uLimit-uSuccess);
			}else{
				swfu.destroy();
				if (swfu2) swfu2.destroy();
			}
		}
	}else{
		alert(jsObj.msg);
	}
}

function uploadSuccessBatch(file, responseText){
	if(responseText.indexOf("<html>")>=0){
		alert("文件超过IIS上传大小限制或服务器错误");
		return;	
	}
	eval(responseText);
	if (jsObj.success){
		var logFunction = tinyMCEPopup.getParam('swfupload_log_function',function(s){});
		logFunction(jsObj.msg);
		batchHtml += '<img src="'+ jsObj.msg +'" alt="" vspace="10"/><br/>';
	}else{
		alert(jsObj.msg);
	}
}

function uploadComplete(file){
	var stats = this.getStats();
	if (stats.files_queued>0){
		this.startUpload();
	}else{
		// 批量时插入
		if(batchHtml.length>0){
			var ed = tinyMCEPopup.editor;
			tinyMCEPopup.restoreSelection();
			if (stats.successful_uploads>1){
				ed.selection.collapse(false);
			}
			batchHtml = '<br/><div style="text-align:center" id="__mce_tmp_img">'+batchHtml+'<\/div><br/>';
			ed.execCommand('mceInsertContent',false,batchHtml);
			ed.selection.select(ed.dom.select('#__mce_tmp_img')[0],true);
			tinyMCEPopup.storeSelection();
			ed.dom.setAttrib('__mce_tmp_img', 'id', '');
			batchHtml = "";
		}
		
		alert("成功上传 "+ stats.successful_uploads +" 个，失败 "+ stats.upload_errors +" 个");
		dopost.hide();
		tinyMCEPopup.close();
	}
}

window.onload = function (){
var imgMark=self.parent.$('input[name$="$imgWithMark"]').attr("checked")?1:0;
<% if (uploadLimit>=0){%>
	swfu = new SWFUpload({
		// Backend Settings
		upload_url: "../../../admin_swfupload.aspx",
    post_params : {
			"user" : "<%=HttpUtility.UrlEncode(CoUser.Name,Encoding.UTF8)%>",
			"psw" : "<%=CoUser.Psw%>",
			"uploadSys" : tinyMCEPopup.getParam('swfupload_sys',false),
			"imgMark" : imgMark
    },
		debug : false,
		
		// File Upload Settings
		file_types : "<%=filetypes%>",
		file_types_description : "<%=description%>",
		file_size_limit : "<%=config.UploadSize%>",
		file_upload_limit : <%=uploadLimit%>,
		file_queue_limit : 1,
		
		// Event Handler Settings
		file_dialog_complete_handler : dialogComplete,
		upload_start_handler : uploadStart,
		upload_success_handler : uploadSuccessSingle,
		
		// Button settings
		button_image_url : "../../../swfupload/single.png",
		button_placeholder_id : "<%=container%>",
		button_width: 20,
		button_height: 20,
		button_text : '',
		button_text_style : '.button { font-family: Helvetica, Arial, sans-serif; font-size: 14pt; } .buttonSmall { font-size: 10pt; }',
		button_text_top_padding: 1,
		button_text_left_padding: 5,
		button_action : SWFUpload.BUTTON_ACTION.SELECT_FILE,
		
		// Flash Settings
		flash_url : "../../../swfupload/flash/swfupload.swf",	// Relative to this file
		
		// Debug Settings
		debug: false
	});
<% if (Request.QueryString["type"] == "img" && uploadLimit!=1){%>
	swfu2 = new SWFUpload({
		// Backend Settings
		upload_url: "../../../admin_swfupload.aspx",
    post_params : {
			"user" : "<%=HttpUtility.UrlEncode(CoUser.Name,Encoding.UTF8)%>",
			"psw" : "<%=CoUser.Psw%>",
			"imgMark" : imgMark
    },
		debug : false,
		
		// File Upload Settings
		file_types : "<%=filetypes%>",
		file_types_description : "<%=description%>",
		file_size_limit : "<%=config.UploadSize%>",
		file_upload_limit : <%=uploadLimit%>,
		file_queue_limit : <%=uploadLimit%>,
		
		// Event Handler Settings
		file_dialog_complete_handler : dialogComplete,
		upload_start_handler : uploadStart,
		upload_success_handler : uploadSuccessBatch,
		upload_complete_handler : uploadComplete,
		
		// Button settings
		button_image_url : "../../../swfupload/batch.png",
		button_placeholder_id : "<%=batContainer%>",
		button_width: 20,
		button_height: 20,
		button_text : '',
		button_text_style : '.button { font-family: Helvetica, Arial, sans-serif; font-size: 14pt; } .buttonSmall { font-size: 10pt; }',
		button_text_top_padding: 1,
		button_text_left_padding: 5,
		button_action : SWFUpload.BUTTON_ACTION.SELECT_FILES,
		
		// Flash Settings
		flash_url : "../../../swfupload/flash/swfupload.swf",	// Relative to this file
		
		// Debug Settings
		debug: false
	});
<%
}}%>
}