/**
*	@author: Coney
*/
jQuery.extend({
	/**
	*	@discription:	上传附件的插件。初始化需要一个“添加附件”的按钮，以及IFrame窗口放置表单区域。
	*	@param opt:	设置参数。json格式。
	*/
	attachUpload:	function(opt){
		var opt = jQuery.extend({
				appendBtnId:	"appendFile",	// “添加附件”按钮的ID
				//filesFrameId:	"filesFrame",	// 附件选择框所在iframe的ID
				fileLabel:	"附件：",	// 附件选择框的Label
				inputHeader: "affachment",	// 附件选择框（<input type="file"/>）的ID头缀，多个附件选择框自动按header[0],header[1]...的数组形式设置。
				uploadAction:	""
				//callback:	null
		}, opt);
		
		var appendBtn = $("#" + opt.appendBtnId);
		var root = appendBtn.parent();
		
		var frameId = "filesFrame";
		var uploadFrame = $('<iframe id="' + frameId + '"  style="display:none"></iframe>');
		
		$(root).append(uploadFrame);
		
		$(root).append("<br />");
		
		var formHeader = "fileUpload_form";
		
		// 附件选择框
		//var fileId = 0;	// 序列号
		
		var appendFile = function(){
			
			var inputId = opt.inputHeader;
			//var formId = formHeader + "[" + fileId + "]"; 
			var formId = formHeader + "[]"; 
			
			/**
			* 生成form表单
			*/
			//var uploadForm = $('<form class="attachLine" action="' + opt.uploadAction + '" method="POST" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data"></form>');
			var uploadForm = $('<form class="attachLine" action="' + opt.uploadAction + '" method="POST" name="' + formId + '" enctype="multipart/form-data" target = "'+frameId+'"></form>');
			//添加Label
			uploadForm.append('<label>' + opt.fileLabel + '</label>');
			//添加input
			var inputFile = $('<input name="' + inputId +'" type="file" class="inputFiles">');
			uploadForm.append(inputFile);
			//添加“删除”按钮
			var deleteBtn = $('<input type="button" value=" 删除 " class="btnDelete"  />').appendTo(uploadForm);
			
			root.append(uploadForm);
			
			/**
			* Input hidden, 记录附件名
			*/
			var inputFName = $('<input type="hidden" name="emailDto.affachmentPath" value="">').appendTo(root);
			
			// 选择文件后自动上传文件
			inputFile.change(function(){
				if (!inputFile.val() || inputFile.val == undefined)	return false;
				
				uploadForm.submit();
				var fileName = inputFile.val();
				inputFName.val(fileName);
			});
			
			// 点击“删除”按钮时删除该行
			deleteBtn.click(function(){
				//$("#"+formId).remove();
				uploadForm.remove();
				inputFName.remove();
			});
			
			//fileFrame.append(uploadForm);
			//fileId +=1;
		};
		
		appendBtn.click(appendFile);
	}
	/**
	cb_addFile : function(fileName){
		var root = $("#aAddAttach").parent();
		root.append('<input type="hidden" name="emailDto.attachmentPath[]" value="'+ filename +'">');
	}
	*/

});