/*中文UTF*/

var _Popup = new Class();

_Popup.prototype={
	popups: {iframe:{}},
	
	initialize:function (config){
		this.config = common.extend({
			key: 'msg',
			contentType: 2,				//内容方式：1、contnet作为一个URL，2、自定义HTML，3、AJAX获取contnet结果
			showMask: true,			//是否显示全屏蒙布
			maskId: '_pop_mask_',
			title: '系统提示', 			//标题
			content: '',				//内容
			callBack: null,				//回调函数
			width: 300,
			height: 100,
			left: 0,					//位置，为0时为居中
			top: 0,
			ctrl: null,
			flash: 1,					//显示方式：0、直接显示，1、显示时渐大，2、关闭时渐小，4、显示时透明度渐显，8、关闭时透明度渐隐
			flashClassName: 'dashedBox',
			timeOut: 0					//对话框显示时间
		},
		config||{});
		var _this = this;
		$(document.documentElement).on({
			keydown: function (event) {
				e = common.getEvent(event);
				actualCode = e.keyCode ? e.keyCode : e.charCode;
				if(actualCode == 27) {
					if(_this.popid && $(_this.popid).style.display != 'none') {
						_this.close(_this.popid);
					}
				}
			}
		});
	},
	
	
	build: function () {
		var config = this.popups[this.popid].config;
		// 全屏蒙布
		if (config.showMask && !$(config.maskId)) {
			$('appendParent').create('div', {display: 'none'}, {id: config.maskId}, {className: 'popMask'});
		}

		// 对话框容器
		if (!$(this.popid)) {
			var o = $('appendParent').create('div', {position: 'absolute', display: 'none'}, {id: this.popid}, {className: 'dialogBox'});
			if(is_ie) {
				o.style.filter = "progid:DXImageTransform.Microsoft.shadow(direction=135,color=#666666,strength=3);";
			}
		}
		$(this.popid).style.width = config.width + 'px';
		this.popups[this.popid].dragObj = [];
		if(is_ie) {
			var iframe = this.getIframe();
			if(!iframe) {
				var iframe = $('appendParent').create('iframe', {position: 'absolute', display: 'none', filter: 'alpha(opacity=0)'});
				config.showMask ? this.popups['iframe'][config.maskId] = iframe : this.popups[this.popid]['iframe'] = iframe;
			}
			
			if (!config.showMask) {
				this.popups[this.popid].dragObj.push(iframe);
			}
		}
		this.popups[this.popid].dragObj.push(this.popid);
	},

	getIframe: function () {
		return this.popups[this.popid].config.showMask ? this.popups['iframe'][this.popups[this.popid].config.maskId] : this.popups[this.popid]['iframe'];
	},
	
	setHtml: function () {
		var config = this.popups[this.popid].config;
		var html = '<div id="' + this.popid + 'Title" class="dialogHead"><input style="float:right;cursor:auto" type="image" id="' + this.popid + 'Close" src="' + IMG_URL + 'win-clo.gif" border="0" align="absmiddle" title="关闭"/><span>' + config.title + '</span></div>';
		html += '<table width="100%" height="100%" class="dialogBody">';
		html += '<tr height="'+ config.height + '"><td id="' + this.popid + 'Body">';
		if (config.contentType == 1) {
			html += '<iframe id="' + this.popid + 'iframe" name="' + this.popid + 'iframe" width="100%" height="100%" frameborder="0"></iframe>';
		} else if (config.contentType == 3) {
		//	html += '<img src="' + IMG_URL + 'loading.gif" valign="middle"> 数据下载中，请稍候...';
		} else {
			html += config.content;
		}
		html += '</td></tr></table>';
	//	this.html = html;
		$(this.popid).innerHTML = html;
	},
	
	display: function() {
	//	$('r').innerHTML += '\n ' + (new Date().getTime() - this.startTime);
		var config = this.popups[this.popid].config;
		if (config.showMask && $(config.maskId)) {
			with ($(config.maskId).style) {
				display = '';
				zIndex = ++common.maxZIndex;
				height = Math.max(body.scrollHeight, body.clientHeight) + 'px';
			}
		}
		var pos = this.popups[this.popid].primal;
	
		if(is_ie) {
			if (config.showMask) {
				with (this.popups['iframe'][config.maskId].style) {
					display = '';
					left = '0px';
					top = '0px';
					width = body.scrollWidth + 'px';
					height = body.scrollHeight + 'px';
					zIndex = ++common.maxZIndex;
				}
			} else {
				with (this.popups[this.popid]['iframe'].style) {
					display = '';
					left = pos.left + 'px';
					top = pos.top + 'px';
					width = pos.width + 'px';
					height = pos.height + 'px';
					zIndex = ++common.maxZIndex;
				}
			}
		}
		
		with ($(this.popid).style) {
			display = '';
			left = pos.left + 'px';
			top = pos.top + 'px';
			zIndex = ++common.maxZIndex;
//			filter = 'alpha(opacity=0)';
//			opacity = 0;
		}
		/*
		is_ie && ($(this.popid).filters.alpha.opacity = 0);
		$(this.popid).to({opacity:100}, {func: function () {
			if(is_ie) {
				$(this.popid).style.filter = "progid:DXImageTransform.Microsoft.shadow(direction=135,color=#666666,strength=3)";
			}
		}, obj: this}, 5);
		*/
		// 点击后激活当前窗口
		$(this.popid).on({'click': this.focus.bind(this, this.popid)});
		
		$(this.popid + 'Title') && $(this.popid + 'Title').drag(this.popups[this.popid].dragObj);
		$(this.popid + 'Close') && ($(this.popid + 'Close').onclick = this.close.bind(this, this.popid));
		
		if (config.contentType == 1) {
			$(this.popid + 'iframe').src = config.content;
		}
		// ajax 获取显示内容
		else if (config.contentType == 3) {
			var me = this;
			var x = new Ajax({loadingId: this.popid + 'Body', loadingAlign: 0, recvType: 'XML', lang_loading: '数据加载中，请稍候...'});
			x.get(config.content, function (s) {
				common.eval(s);
				$(me.popid + 'Body').innerHTML = s;
				
				// 调整位置
				//*
				$(me.popid).to({
					left: pos.left + (pos.width - $(me.popid + 'Body').offsetWidth) / 2,
					top: Math.max(10, (body.clientHeight - $(me.popid).offsetHeight) / 2 + body.scrollTop),
					width: $(me.popid + 'Body').offsetWidth + 5
				}, '', 5);
				//*/
			});
		}
		
		if (config.flash) {
		//	this.popups[this.popid].flashBox.style.display = 'none';
		}
		if (config.timeOut) {
			this.popups[this.popid]['timer'] = this.close.defer(this, config.timeOut, this.popid);
		}
	//	$('r').innerHTML += '\n ' + (new Date().getTime() - this.startTime).toString();
	},
	
	show: function (config) {
		this.popid = '_pop_dialog_' + (common.isUndefined(config.key) ? this.config.key : config.key) + '_';
		
		if (!this.popups[this.popid]) {
			this.popups[this.popid] = {
				config: {}
			};
		}
		
		common.extend(this.popups[this.popid].config, this.config);
		common.extend(this.popups[this.popid].config, config || {});
		config = this.popups[this.popid].config;
		
		this.build();
		this.setHtml();
		
		var popobj = $(this.popid);
		popobj.style.display = '';
		/*
		*	因内容过多而可能使对话框超出config中设置的高度，因此要用offsetHeight才能得到真正的高度
		*	先将对话框显示，计算出来其宽度和高度后，再关闭
		*/
		var pos = this.popups[this.popid].primal = {
			left: config.left ? config.left : (body.clientWidth - popobj.offsetWidth) / 2,
			top: (config.top ? config.top : (body.clientHeight - popobj.offsetHeight) / 2) + body.scrollTop,
			width: popobj.offsetWidth,
			height: popobj.offsetHeight
		}
		popobj.style.display = 'none';
		
		if (config.flash) {
		/*
			var primal;
			var ctrl = $(this.popups[this.popid].config.ctrl);
			if (ctrl) {
				primal = ctrl.getPos();
			} else {
				primal = {
					width: 10,
					height: 10,
					left: (body.clientWidth - 10) / 2,
					top: (body.clientHeight - 10) / 2 + body.scrollTop
				}
			}

			var box = this.popups[this.popid].flashBox;
			if (!box) {
				this.popups[this.popid].flashBox = box = $('appendParent').create('div', {position: 'absolute'}, {}, {className: config.flashClassName});
			} else {
				box.style.display = '';
			}
			common.extend(box, {primal: primal});
			with(box.style) {
				width = primal.width + 'px';
				height = primal.height + 'px';
				left = primal.left + 'px';
				top = primal.top + 'px';
				zIndex = ++common.maxZIndex;
			}
			
			box.to({width: pos.width, height: pos.height, left: pos.left, top: pos.top}, {func: function () {this.display();}, obj: this});
			popobj.style.filter = 'alpha(opacity=0)';
			popobj.style.opacity = 0;
			popobj.to({opacity:100});
			*/
			this.display();
		} else {
			this.display();
		}
		return this.popid;
	},
	
	resize: function (o, w, h) {
		if (o.indexOf('_pop_dialog_') < 0) {
			o = '_pop_dialog_' + o + '_';
		}
		if(is_ie && !this.popups[o].config.showMask) {
			this.popups[o]['iframe'].to({width: w, height: h});
		}
		$(o).to({width: w, height: h});
	},
	
	focus: function (o) {
		this.popid = o;
	},
	
	close: function (o) {
		if (o.indexOf('_pop_dialog_') < 0) {
			o = '_pop_dialog_' + o + '_';
		}
		this.popid = o;
		if (this.popups[o].config.flash) {
		/*	//alert($(o).offsetLeft);
			with (this.popups[o].flashBox.style) {
				width = $(o).offsetWidth + 'px';
				height = $(o).offsetHeight + 'px';
				left = $(o).offsetLeft + 'px';
				top = $(o).offsetTop + 'px';
			}
			*/
		}
		
		this.hide(o);
		
		if (this.popups[o].config.flash) {
		//	var box = this.popups[o].flashBox;
		//	box.style.display = '';
		//	var primal = this.popups[o].flashBox.primal;
		//	box.to({width: primal.width, height: primal.height, left: primal.left, top: primal.top}, {func: function () {setTimeout(function () {box.style.display = 'none';}, 100);}, obj: this});
		}
		/*
			解决点击关闭按钮后，可能造成的文字无法选择问题
		*/
		document.ondragstart = document.onselectstart = document.onselect = null;
	},

	hide: function (o) {
		$(o).style.display = 'none';
		if (this.popups[o].config.showMask) {
			$(this.popups[o].config.maskId).style.display = 'none';
		}
		if(is_ie) {
			iframe = this.getIframe();
			iframe.style.display = 'none';
		}
		this.popups[o]['timer'] && clearTimeout(this.popups[o]['timer']);
		this.popups[o]['outTimer'] && clearTimeout(this.popups[o]['outTimer']);
		this.popups[o]['intervalTimer'] && clearInterval(this.popups[o]['intervalTimer']);
	},
	
	forCallBack: function (popid) {
		this.popups[popid].config.callBack(this.popups[popid].config);
		this.close(popid);
	}
}

var popup = new _Popup();

/*
@	(string)	msg			消息内容
@	(number)	buttons		消息类型，不同类型数值可以相加
							按钮：
								0、只会显示确定按钮，默认
								1: 确定和取消按钮
							图标：
								0：提示图标，默认
								4：询问图标
								8: 成功图标
								16: 失败图标
							默认按钮：
								0：第一个按钮为默认按钮
								32：第二个按钮为默认按钮，仅当按钮类型为1时有效
							显示全屏蒙布：
								0：不显示
								512：显示
@	(string)	title		对话框标题
@	(array)		funs		绑定事件
@	(number)	time		显示时间，单位：秒，显示时间过后会自动点击默认按钮
*/
function msgbox(msg, buttons, title, func, time, k) {
	var ico;
	!buttons && (buttons = 0);
	!title && (title = '系统提示');
	!func && (func = {});
	common.isUndefined(time) && (time = 0);
	common.isUndefined(k) && (k = 'msgbox');

	var html = '<table width="100%" height="100%"><tr><td style="width:25%; height:80px;"><img src="'+ IMG_URL;
	if (buttons & 16) {
		html += 'err';
	} else if (buttons & 8) {
		html += 'right';
	} else if (buttons & 4) {
		html += 'ask';
	} else {
		html += 'info';
	}
	html += '.gif"></td><td align="left">' + msg + '</td></tr>';
	html += '<tr><td colspan="2" height="50">';
	html += '<input rel="button" type="button" value="  确定  ">';
	if (buttons & 1) {
		html += ' <input rel="button" type="button" value="  取消  ">';
	}
	html += '</td></tr></table>';
	
	var showMask = buttons & 512 ? true : false;
	popid = popup.show({key: k, maskId: '_pop_mask_' + k, showMask: showMask, contentType: 2, content: html, title: title, width: 350, height: 100});
	var inputs = $(popid + 'Body').find('>input[rel=button]');
	var defButton;
	if (buttons & 32 && inputs[1]) {
		defButton = inputs[1];
	} else {
		defButton = inputs[0];
	}
	defButton.focus();
	
	inputs[0].onclick = function () {
		popup.close(popid);
		func.ok && func.ok();
	}
	if (inputs[1]) {
		inputs[1].onclick = function () {
			popup.close(popid);
			func.cancel && func.cancel();
		}
	}
	if (time) {
		oTitle = $(popid + 'Title').find('>span')[0];
		oTitle.innerHTML = title + ' (' + time + ')';
		popup.popups[popid]['outTimer'] = setTimeout(function () {defButton.click();}, time * 1000);
		popup.popups[popid]['intervalTimer'] = setInterval(function () {oTitle.innerHTML = title + ' (' + (--time) + ')'}, 1000);
	}
	return popid;
}