(function($, window, undefined) {
	// 动态创建窗口
	"use strict";
	var CustomOpenModal = {
		init : function(opts) {
			var _self = this;
			var d = _self.dHtml(opts);
			$("body #main #content").append(d);
			var modal = $("#" + opts.componentId);
			modal.modal(opts);
			if (!opts.beforeOpenEvent) {
				open();
			} else if (typeof opts.beforeOpenEvent == 'function' && opts.beforeOpenEvent(function() {open()})) {
				open();
			}
			function open() {
				var backdrop = $('<div id='+opts.componentId +'-backdrop class="modal-backdrop custom-open-backdrop in " data-z-index='+opts.backdropZIndex+' style=" z-index:'+opts.backdropZIndex+';" />')
		        .appendTo(document.body);
				modal.on('hidden.bs.modal', function(e) {
					if (e.currentTarget == e.target) {
						$("#"+opts.componentId +"-backdrop").remove();
						if (opts.closeEvent) {
							var data = modal.attr("data-close") || "{}";
							opts.closeEvent.call(modal, JSON.parse(data));
						}
						modal.remove();
					}
				});
				if(opts.title){
					setTimeout(function() {
						$("#" + opts.componentId+"Label")&&$("#" + opts.componentId+"Label").text(opts.title);
					},300);
				}
				if (opts.formData) {
					if ($("#" + opts.componentId).find("form")) {
						setTimeout(function() {
							var formC = $("#" + opts.componentId).find("form");
							formC.autofill(opts.formData);
							if(formC.find("select").length>0){
								formC.find("select").trigger("change");
							}
							if(formC.find("select.select2").length>0){
								formC.find("select.select2").trigger("change");
							}
							if(formC.find("input[type='checkbox']").length>0){
								var checkboxNameData = {};
								formC.find("input[type='checkbox']").each(function(index, element) {
									var checkboxName = element.name;
									if(checkboxName){
										if(!checkboxNameData[checkboxName]){
											checkboxNameData[checkboxName]=checkboxName;
											var checkData = opts.formData[checkboxName];
											if (checkData) {
												var dataArry = checkData.split(",");
												$.each(dataArry, function(index, value) {
													formC.find("input[name='" + checkboxName + "'][value='" + value + "']").prop("checked", true);
												});
											}
										}
									}
								});
								formC.find("input[type='checkbox']").trigger("change");
							}
							if(formC.find("input[type='radio']").length>0){
								formC.find("input[type='radio']").trigger("change");
							}
							modal.modal('show');
						}, 400);
					}else{
						modal.modal('show');
					}
				}else{
					modal.modal('show');
				}

				modal.on("show.bs.modal", ".modal", function(){
			         $(this).draggable({
			         	 handle: ".modal-header"
			         });
			         $(this).css("overflow", "hidden");
			    });

			}
		},
		dHtml : function(o) {
			var $width = o.width;
			if (!o.width || o.width < 100) {
				$width = '';
			}else if($width&&$width!="auto"){
				$width = $width +"px";
			}
			var $tmp = '<div id="'
					+ o.componentId
					+ '" class="modal fade custom-open-dialog '+o.modalClass+'" role="dialog"  aria-labelledby="myModalLabel" aria-hidden="true"  confirm='
					+ o.confirm + ' style=" z-index:'+o.modalZIndex+'; left: 24px; top: 20px;" >';
			$tmp += '<div class="modal-dialog '+o.modalDialogClass+' " style="width: '
					+ $width + '" >';
			$tmp += '<input type="hidden" id="operateType" name="operateType" value="'
				+ o.operateType + '" />';
			$tmp += '<input type="hidden" id="dataId" name="dataId" value="'
					+ o.dataId + '" />';
			$tmp += '<input type="hidden" id="pkId" name="pkId" value="'
					+ o.pkId + '" />';
			$tmp += '<input type="hidden" id="extData" name="extData" value="'
					+ o.extData + '" />';
			$tmp += '<div class="modal-content"><div class="modal-header">';
			$tmp += '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>';
			$tmp += '<h3 class="modal-title" id="myModalLabel">' + o.title
					+ '</h3></div><div class="modal-body">';
			$tmp += '<button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>';
			$tmp += '<button class="btn btn-primary ok">确定</button></div></div></div></div>';
			return $tmp;
		},
		openDialog : function(options) {
			var defaults = {
				componentId : "CustomOpenModal", // 弹窗id
				title : "", // 弹窗标题
				width : "", // 弹窗宽度，暂时不支持%
				height : "480", // 弹窗高度,不支持%
				backdrop : "static", // 是否显示遮障，和原生bootstrap 模态框一样
				backdropZIndex:'1070',
				modalZIndex:'1080',
				keyboard : false, // 是否开启esc键退出，和原生bootstrap 模态框一样
				remote : "", // 加载远程url，和原生bootstrap 模态框一样
				openEvent : null, // 弹窗打开后回调函数
				closeEvent : null, // 弹窗关闭后回调函数
				okEvent : null,// 单击确定按钮回调函数,
				widthFlg : false,
				confirm : false,
				operateType : "",
				dataId : "",
				pkId : "",
				extData : "", // 扩展数据
				formData : null,
				beforeOpenEvent : null,
				modalClass:"",
				modalDialogClass:"modal-lg"
			};
			if($("body #main #content .custom-open-dialog").length>0){
				options.backdropZIndex = ($("body #main #content .custom-open-dialog").length*10)+1091;
				options.modalZIndex = ($("body #main #content .custom-open-dialog").length*10)+1101;
			}
			var opts = $.extend({}, defaults, options);
			var _self = this;
			_self.init(opts)
		},
		openDialogByData : function(componentId, remote,formData, title, width, options) {
			if (!componentId) {
				alert("openDialogById方法参数组件ID:componentId不能为空");
				return;
			}
			if (!remote) {
				alert("openDialogById方法参数页面:remote不能为空");
				return;
			}
			if(options&&(typeof options == 'object')){
				options.componentId = componentId;
				options.formData = formData;
				options.remote = remote;
                options.title = title;
                options.width = width;
			}else{
				options =  {
						componentId : componentId,// 弹窗id
						formData : formData, // 扩展数据
						title : title, // 弹窗标题
						width : width, // 弹窗宽度，暂时不支持%
						remote : remote, // 加载远程url，和原生bootstrap 模态框一样
					};
			}
			var _self = this;
			_self.openDialog(options);
		},
	};
	window.CustomOpenModal = CustomOpenModal;
})(jQuery, this);
