// JavaScript Document

/**
 *
 * Dialog tools
 *
 * @author D.Y <dongyong@linekong.com>
 *
 * @date 2008-04-24
 *
 */
var Dialog = function() {
	
	this.init();

	this._mask = null;	
	this._mask_alpha = 3;
	this._mask_interval = null

	this._close = null;
	
	this.show_state = false;

	this.x = null;
	this.y = null;

};

Dialog.prototype.xy = function() {
	if (document.documentElement && (document.documentElement.scrollTop || document.documentElement.scrollLeft)) {
		var dY = document.documentElement.scrollTop;
		var dX = document.documentElement.scrollLeft;
	}else if (document.body){
		var dY = document.body.scrollTop;
		var dX = document.body.scrollLeft;
	}else{
		var dY = document.documentElement.scrollTop;
		var dX = document.documentElement.scrollLeft;
	}
	return [dX, dY];
}

Dialog.prototype.mask = function(state) {
	var o = this;

	if(state == 'show') {

		if(this._mask === null || this._mask === undefined) {

			this._mask = document.createElement('iframe');
			this._mask.id = '_mask';
			this._mask.allowTransparency = true;
			this._mask.style.position = "absolute";
			if(window.ActiveXObject) this._mask.src = "/common/js/dialog.html";
			this._mask.style.background = '#000';
			this._mask.style.top = "0px";
			this._mask.style.left = "0px";
			this._mask.style.border = "0px";
			this._mask.style.filter = "alpha(opacity=" + (this._mask_alpha * 10) + ")";
			this._mask.style.opacity = (this._mask_alpha * 0.1);
			this._mask.style.zIndex = 997;
			this._mask.style.height = document.documentElement.scrollHeight + "px";			
			this._mask.style.width = document.documentElement.scrollWidth + "px";

			document.body.appendChild(this._mask);
			
		}else{

			this._mask.style.display = 'block';
			
		}

		this._mask_interval = setInterval(function() {
			if(o._mask == undefined) return false;
			o._mask.style.height = document.documentElement.scrollHeight + "px";
			o._mask.style.width = document.documentElement.scrollWidth + "px";
		}, 300);

	}else if(state == 'hide') {

		if(this._mask !== undefined && this._mask !== null) {
			this._mask.style.display = 'none';
		}

	}

}

Dialog.prototype.show = function() {
	var o = this;

	if(arguments[0] == undefined) {
		return false;
	}

	var _content = arguments[0];
	
	var _callback	= null;
	var _title		= null;

	if(arguments[1] != undefined) {
		if(typeof(arguments[1]) == 'function') {
			_callback = arguments[1];
		}else{
			_title = arguments[1];
		}
	};

	if(arguments[2] != undefined) {
		if(typeof(arguments[2]) == 'function') _callback = arguments[2];
	}
	
	if(this.show_state) return false;
	this.mask('show');	

	var content = document.createElement("div");
	content.style.padding = '7px';
	content.style.position = 'relative';
	content.style.border = '1px solid #666666';
	content.style.styleFloat = 'left';
	
	if(typeof(_content) != "object") {
		content.innerHTML = _content;
	}else{
		content.appendChild(_content);
	}

	if(_title != null) {
		var _head = document.createElement("div");
		_head.style.padding = '7px';
		_head.style.background = '#999';
		_head.style.color = '#FFF';
		_head.style.borderTop = _head.style.borderLeft = _head.style.borderRight = '1px solid #666666';
		var title = document.createElement("div");
		title.innerHTML = _title;
		title.style.cssFloat = 'left';
		title.style.styleFloat = 'left';
		var _close = document.createElement("div");
		_close.innerHTML = '关闭 X';
		_close.style.cursor = 'pointer';
		_close.style.cssFloat = 'right';
		_close.style.styleFloat = 'right';
		_close.onclick = function() { o.close(); }
		var _clear = document.createElement("div");
		_clear.style.clear = 'both';
		_head.appendChild(title);
		_head.appendChild(_close);
		_head.appendChild(_clear);
		this._dialog.appendChild(_head);
	}

	this._dialog.appendChild(content);
	document.body.appendChild(this._dialog);

	this._dialog.style.width = content.offsetWidth + "px";

	this.setPosition(this.x, this.y);

	this._dialog_bg.style.width = this._dialog.offsetWidth + 14 + "px";
	this._dialog_bg.style.height = this._dialog.offsetHeight + 14 + "px";
	this._dialog_bg.style.top = this._dialog.offsetTop - 7 + "px";
	this._dialog_bg.style.left = this._dialog.offsetLeft - 7 + "px";
	this._dialog_bg.className = 'radius7';
	document.body.appendChild(this._dialog_bg);

	this.show_state = true;

	if(_callback != null) _callback();

}


Dialog.prototype.setPosition = function(x, y) {
	var _xy = this.xy();

	if(x != null) {
		this._dialog.style.left = x + "px";
	}else{
		this._dialog.style.left = (document.documentElement.clientWidth / 2) - (parseInt(this._dialog.offsetWidth) / 2)  + _xy[0] + "px";
	}

	if(y != null) {
		this._dialog.style.top = y + "px";
	}else{
		this._dialog.style.top = (document.documentElement.clientHeight / 2) - (parseInt(this._dialog.offsetHeight) / 2) + "px";
	}
}

Dialog.prototype.init = function() {
	this._dialog = document.createElement('div');
	this._dialog.id = '_dialog';	
	this._dialog.style.position = "fixed";
	this._dialog.style.background = '#FFF';
	this._dialog.style.zIndex = 999;
	
	this._dialog_bg = document.createElement('div');
	this._dialog_bg.id = '_dialog_bg';
	this._dialog_bg.style.position = "fixed";
	this._dialog_bg.style.background = '#000';
	this._dialog_bg.style.zIndex = 998;
	this._dialog_bg.style.filter = "alpha(opacity=30)";
	this._dialog_bg.style.opacity = 0.3;
}

Dialog.prototype.close = function() {
	if(!this.show_state) return false;
	document.body.removeChild(this._dialog);
	document.body.removeChild(this._dialog_bg);
	this.mask('hide');
	this.show_state = false;
	delete this._dialog;
	delete this._dialog_bg;
	this.init();
}

var dialog = new Dialog();
