var _Popup = new Class();
_Popup.prototype={
	popups: [],
	
	initialize:function (config){
		this.config = Object.extend({
			key: 'msg',
			contentType: 2,				//内容方式：1、contnet作为一个URL，2、自定义HTML，3、提示框，4、确认框
			showOverlay: true,			//是否显示全屏蒙布
			overlayId: '_pop_overlay_',
			title: '系统提示', 			//标题
			content: '',				//内容
			callBack: null,				//回调函数
			width: 300,
			height: 0,
			left: 0,					//位置，为0时为居中
			top: 0,
			timeout: 0					//对话框显示时间
		},
		config||{});
	},
	
	set: function (i) {
		Object.extend(this.config, i || {});
	},
	
	build: function () {
		if (this.popups[this.ctrlid].config.showOverlay) {
			if (!$(this.config.overlayId)) {
				var o = document.createElement('div');
				o.id = this.config.overlayId;
				o.className = 'popOverlay';
				o.style.display = 'none';
				document.body.appendChild(o);
			}
		}
		if (!$(this.ctrlid)) {
			var o = document.createElement('div');
			o.id = this.ctrlid;
			o.className = 'dialogBox';
			o.style.display = 'none';
			document.body.appendChild(o);
			if(is_ie) {
				o.style.filter = "progid:DXImageTransform.Microsoft.shadow(direction=135,color=#CCCCCC,strength=2)";
			}
		}
		this.popups[this.ctrlid]['dragObj'] = [];
		if(is_ie && is_ie < 7) {
			var iframe = this.getIframe();
			if(!iframe) {
				iframe = document.createElement('iframe');
			//	iframe.id = this.ctrlid + 'iframe';
				iframe.style.display = 'none';
				iframe.style.position = 'absolute';
				iframe.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
				document.body.appendChild(iframe);
				this.popups[this.ctrlid].config.showOverlay ? this.popups['iframe'] = iframe : this.popups[this.ctrlid]['iframe'] = iframe;
			}
			
			if (!this.popups[this.ctrlid].config.showOverlay) {
				this.popups[this.ctrlid].dragObj.push(iframe);
			}
		}
		this.popups[this.ctrlid].dragObj.push(this.ctrlid);
	},
	
	getIframe: function () {
		return this.popups[this.ctrlid].config.showOverlay ? this.popups['iframe'] : this.popups[this.ctrlid]['iframe'];
	},
	
	setPosition: function() {
		if (this.popups[this.ctrlid].config.showOverlay) {
			var o = $(this.config.overlayId);
			Object.extend(o.style, {
				position: 'absolute',
				zIndex: ++maxZIndex,
				left: 0,
				top: 0,
				width: body.scrollWidth + 'px',
				height: body.scrollHeight + 'px',
				display: ''
			});
		} 
		var o = $(this.ctrlid);
		o.style.display = '';
//		o.innerHTML = this.html;
		var oLeft = this.config.left ? this.config.left : (body.clientWidth - this.config.width) / 2;
		var oTop = (this.config.top ? this.config.top : (body.clientHeight - o.offsetHeight) / 2) + body.scrollTop;
		Object.extend(o.style, {
			position: 'absolute',
			zIndex: ++maxZIndex,
			left: oLeft + 'px',
			top: oTop + 'px'
		});
		o.focus();
		if(is_ie && is_ie < 7) {
			if (this.popups[this.ctrlid].config.showOverlay) {
				Object.extend(this.popups['iframe'].style, {
					left: 0,
					top: 0,
					width: body.scrollWidth + 'px',
					height: body.scrollHeight + 'px',
					display: ''
				});
			} else {
				Object.extend(this.popups[this.ctrlid]['iframe'].style, {
					display: 'none',
					left: oLeft + 'px',
					top: oTop + 'px',
					width: $(this.ctrlid).offsetWidth + 'px',
					height: $(this.ctrlid).offsetHeight + 'px',
					display: ''
				});
	//		$('r').value = oLeft + ' ' + o.offsetWidth + ' ' + oTop + ' ' + o.offsetHeight;
			}
		}
	},
	
	show: function (config) {
		Object.extend(this.config, config || {});
		this.ctrlid = '_pop_dialog_' + this.config.key + '_';
//		alert(this.popups[this.ctrlid]);
		if (!this.popups[this.ctrlid]) {
			this.popups[this.ctrlid] = {
				config: {},
				ctrlid: this.ctrlid
			};
		}
		Object.extend(this.popups[this.ctrlid].config, this.config);
	//	alert(this.popups[this.ctrlid].config.width);
	//	this.popups[this.ctrlid]['ctrlid'] = this.ctrlid;
	//	this.popups[this.ctrlid]['callBack'] = this.config.callBack;

		this.build();

		var html = '<table width="' + this.config.width + '" class="tab">';
		html += '<tr id="' + this.ctrlid + 'Title"><td class="dialogHead"><span style="float:right;cursor:auto"><input type="image" id="' + this.ctrlid + 'Close" src="' + IMG_URL + 'win-clo.gif" border="0" align="absmiddle" title="关闭"/></span>' + this.config.title + '</td></tr>';
		html += '<tr><td align="center" height="' + this.config.height + '" class="dialogBody">';
		if (this.config.contentType == 1) {
			html += '<iframe id="' + this.ctrlid + 'iframe" name="' + this.ctrlid + 'iframe" width="100%" height="100%" frameborder="0"></iframe>';
		} else {
			html += this.config.content;
			if (this.config.contentType > 2) {
				html += '<div style="margin: 10px">';
				html += '<input id="' + this.ctrlid + 'YES" type="button" value="  确定  ">';
				if (this.config.contentType == 4) {
					html += ' <input id="' + this.ctrlid + 'NO" type="button" value="  取消  ">';
				}
				html += '</div>';
			}
			
		}
		html += '</td></tr></table>';
	//	this.html = html;
		$(this.ctrlid).innerHTML = html;
		this.setPosition();
		drag.init(this.ctrlid + 'Title', this.popups[this.ctrlid]['dragObj']);
		Event.observe($(this.ctrlid + 'Close'), "click", this.close.bindAsEventListener(this),false);
		
		if (this.config.contentType == 1) {
			$(this.ctrlid + 'iframe').src = this.config.content;
		} else if (this.config.contentType == 3) {
			Event.observe($(this.ctrlid + 'YES'), "click", this.close.bindAsEventListener(this),false);
		} else if (this.config.contentType == 4) {
			Event.observe($(this.ctrlid + 'YES'), "click", this.forCallBack.bindAsEventListener(this),false);
			Event.observe($(this.ctrlid + 'NO'), "click", this.close.bindAsEventListener(this),false);
		}
		
		if (this.config.timeout) {
			this.popups[this.ctrlid]['timer'] = this.hide.timeOut(this.config.timeout, this.ctrlid, this);
		}
		this.initialize();
	},
	
	close: function (event, k) {
		if (event) {
			var d = Event.element(event).id;
			d = d.substring(0, d.lastIndexOf('_') + 1);
		} else {
			var d = '_pop_dialog_' + k + '_';
		}
	//	alert(this.popups[d].config.width);
		this.hide(d, this);
	},
	
	hide: function (o, t) {
		if (isUndefined(t)) t = this;
		var showOverlay = t.popups[o].config.showOverlay;
		
		$(o).style.display = 'none';
		if (showOverlay) {
			$(t.config.overlayId).style.display = 'none';
		}
		if(is_ie && is_ie < 7) {
			iframe = showOverlay ? t.popups['iframe'] : t.popups[o]['iframe'];
			iframe.style.display = 'none';
		}
		if (t.popups[o]['timer']) {
			clearTimeout(t.popups[o]['timer']);
		}
	},
	
	forCallBack: function (event) {
		var d = Event.element(event).id;
		d = d.substring(0, d.lastIndexOf('_') + 1);
		this.popups[d].config.callBack(this.popups[d].config);
		this.hide(d, this);
	},
	
	resize: function () {
		
	}
};

var popup = new _Popup();
