/**
*
* 软件著作权: 中华网科技公司
* 系统名称: 游戏频道互动社区系统
* 相关文档: 游戏频道互动社区系统设计文档
* 作者: Jason(武小川)
* 程序名称: dialog.js
* 生成文件时间：Mon May 19 16:05:47 CST 2008
* 程序功能: 显示提示窗口light-box, 改编自http://www.leigeber.com/2008/04/custom-javascript-dialog-boxes/
*			在源程序得基础上, 采用类得方式,在prototype框架下重写, 使之更具有通用性.
*			增加了callBack参数, 在关闭窗口得时候, 调用传入得script语句.
*			去处了autoHide得功能.
* 修改历史
* 修改日期      修改者      BUG小功能修改申请单号
* 2008-07-15    Jason     增加了函数fireFunction
*/
var WRAPPER = 'baseDiv';

var showDialog = {
	init: function(title,message,type,callBack) {
		this.title = title;
		this.message = message;
		this.type = type;
		this.params = null;
		if (callBack) {
			this.callback = callBack;
		} else {
			this.callback = {};
		}
		this.prepareDiv();
	},
	prepareDiv: function() {
		if(!$('dialog')) {
			dialog = document.createElement('div');
			dialog.id = 'dialog';
			dialogheader = document.createElement('div');
			dialogheader.id = 'dialog-header';
			dialogtitle = document.createElement('div');
			dialogtitle.id = 'dialog-title';
			dialogclose = document.createElement('div');
			dialogclose.id = 'dialog-close'
			dialogcontent = document.createElement('div');
			dialogcontent.id = 'dialog-content';
			dialogmask = document.createElement('div');
			dialogmask.id = 'dialog-mask';
			document.body.appendChild(dialogmask);
			document.body.appendChild(dialog);
			dialog.appendChild(dialogheader);
			dialogheader.appendChild(dialogtitle);
			dialogheader.appendChild(dialogclose);
			dialog.appendChild(dialogcontent);;
		} else {
			$('dialog-mask').style.visibility = "visible";
			$('dialog').style.visibility = "visible";
		}
		$('dialog').setOpacity = .00;
		$('dialog').addClassName(this.type+"container");
		$('dialog-close').addClassName(this.type+"close");

		var offsets = document.viewport.getScrollOffsets();
		var width = this.pageWidth()
		var height = this.pageHeight();
		var left = offsets.left;
		var top = offsets.top;

		var dialogwidth = $('dialog').getWidth();
		var dialogheight = $('dialog').getHeight();

		var topposition = top + (height / 3) - (dialogheight / 2);
		var leftposition = left + (width / 2) - (dialogwidth / 2);
		
		if (topposition < 0) {
			topposition = Math.abs(topposition);
		}
		//不能太往上了
		if (topposition < 100) {
			topposition = 100;
		}

		$('dialog').setStyle({
			top: topposition + "px",
			left: leftposition + "px"
		});
		$('dialog-header').addClassName(this.type+"header");
		$('dialog-title').innerHTML = this.title;
		$('dialog-content').addClassName(this.type);
		$('dialog-content').innerHTML = this.message;

		$('dialog-mask').setStyle({
			height: $(WRAPPER).getHeight() + 'px'
		});

		$('dialog-close').setStyle({
			visibility: "visible"
		});
		this.closeHandle = showDialog.hideDialogEvent.bindAsEventListener(this.assembleCloseObject(this.callback));
		Event.observe($('dialog-close'), 'click', this.closeHandle);
	},
	hideDialogEvent: function (e) {
		$('dialog').style.visibility = "hidden";
		$('dialog-mask').style.visibility = "hidden";
		$('dialog-close').style.visibility = "hidden";
//		if (this.afterClose) {
//			var afterClose = "<script>"+this.afterClose+"</script>";
//			afterClose.evalScripts();
//		}
		Event.stopObserving($('dialog-close'), 'click', this.closeHandle);
	},
	hideDialog: function() {
		$('dialog').style.visibility = "hidden";
		$('dialog').setStyle({
			visibility: "hidden",
			top:  "0px",
			left: "0px"
		});
		$('dialog-mask').style.visibility = "hidden";
		$('dialog-close').style.visibility = "hidden";
		if (this.callback.afterClose) {			
			var args = Array.prototype.slice.call(arguments);
			fireFunction(this.callback.afterClose, args);
		}
		Event.stopObserving($('dialog-close'), 'click', this.closeHandle);
	},
	pageWidth: function () {
		return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
	},

	pageHeight: function () {
		return window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
	},
	assembleCloseObject: function (obj) {
		var closeObject = {afterClose: null};
		if (obj.afterClose) {
			closeObject.afterClose = obj.afterClose;
		}
		return closeObject;
	}
};

/**
 * 判断传入的参数func是否是Function, 
 * 如果是, 则采用Function.apply的方法调用,并且传入变量
 * 如果不是, 则采用evalScripts的方式调用
 * 
 * @author Jason
 */
function fireFunction(func, args) {
	if (Function.prototype.isPrototypeOf(func)) {
		func.apply(this, args);
	} else {
		var scripts = "<script>"+func+"</script>";
		scripts.evalScripts();
	}
}