﻿// ------------------------------------------------
//
// 定义一个使用层模拟弹出窗口的对象，此窗口在层中放一个
// iframe 对象，然后在这里面显示一个指定的 Url 地址
// 
// ------------------------------------------------

// 已经显示的窗口
var _shownWindows = [];
var _layerWindowIndex = 0;
var _layerWindowZIndex = 1100;

var _attachedBodyResize = false;

var _oldOverflowX = '';
var _oldOverflowY = '';
/*
parameters:
	title:
	width:
	height:
	url:
	parent:
	callback:
*/
function LayerWindow(parameters){

	this.title = parameters.title;
	this.width = parameters.width || 500;
	this.height = parameters.height || 350;
	this.url = parameters.url;
	this.callback = parameters.callback;
	
	this.windowClass = parameters.windowClass;
	this.captionClass = parameters.captionClass;
	this.titleClass = parameters.titleClass;
	this.shadowClass = parameters.shadowClass;

	this._initialized = false;
	this._winHtmlObj = null;

	this._contentPanel = null;
	this._slPanel = null;
}

LayerWindow.prototype.get_windowClass = function(){
	return this.windowClass;
}
LayerWindow.prototype.set_windowClass = function(css){
	this._windowClass = css;
}
LayerWindow.prototype.get_captionClass = function(){
	return this.captionClass;
}
LayerWindow.prototype.set_captionClass = function(css){
	this.captionClass = css;
}
LayerWindow.prototype.get_titleClass = function(){
	return this.titleClass;
}
LayerWindow.prototype.set_titleClass = function(css){
	this.titleClass = css;
}
LayerWindow.prototype.get_shadowClass = function(){
	return this.shadowClass;
}
LayerWindow.prototype.set_shadowClass = function(css){
	this.shadowClass = css;
}
LayerWindow.prototype.get_contentPanel = function() {
	return this._contentPanel;
}
LayerWindow.prototype.get_slPanel = function() {
	return this._slPanel;
}

LayerWindow.prototype.show = function(){
	_showLayerWindowShadow(this.get_shadowClass());
	this._initialize();

	this._winHtmlObj.style.display = 'block';
	var cx = ((document.body.offsetWidth - this._winHtmlObj.offsetWidth) / 2) + document.body.scrollLeft;
	var cy = ((document.body.offsetHeight - this._winHtmlObj.offsetHeight) / 2) + document.body.scrollTop;
	this._winHtmlObj.style.left = cx;
	this._winHtmlObj.style.top = cy;
	this._winHtmlObj.focus();

	_shownWindows.push(this);

	if(!_attachedBodyResize){
		_attachedBodyResize = true;
		window.attachEvent('onresize', adjuestWindowsPosition);
	}

	if(_shownWindows.length == 1) {
		hideSelects();
	}
}

LayerWindow.prototype.hide = function(){
	_shownWindows.pop();
	this._winHtmlObj.style.display = 'none';

	_hideLayerWindowShadow();
}

LayerWindow.prototype.dispose = function(){

}

LayerWindow.prototype._initialize = function(){
	if(this._initialized)
		return ;

	this._initialized = true;
	
	// 主窗口
	var container = document.createElement('div');
	this._winHtmlObj = container;
	container.className = this.get_windowClass();
	container.style.width = this.width;
	container.style.height = this.height;
	container.style.zIndex = _layerWindowZIndex++;
	document.body.appendChild(container);

	// 标题栏
	var caption = document.createElement('div');
	caption.className = this.get_captionClass();
	caption.innerHTML = '<table border=0 cellpadding=0 cellspacing=0 width="100%"><tr>'
		+ '<td class="' + this.get_titleClass() + '">' + this.title + '</td>'
		+ '<td style="text-align:center;cursor:hand;width:25px"><img src="images/dialog_close_box.gif" style="cursor:hand;" title="关闭窗口" onclick="closeCurrentLayerWindow(null)" /></td>'
		+ '</tr></table>';
	container.appendChild(caption);

	var bbar = document.createElement('div');
	bbar.className = 'bbar';

	var btnOK = document.createElement('input');
	btnOK.type = 'button'; btnOK.value = '确定'; btnOK.className = 'btnOK';
	btnOK.onclick = function() {
		var ss = [];
		var win = getCurrentLayerWindow();
		for(var i = 0; i < win.get_slPanel().children.length; i++) {
			ss[i] = win.get_slPanel().children[i].dataitem;
		}
		notifyParentLayerWindow(ss, true);
	}
	bbar.appendChild(btnOK);

	var btnClear = document.createElement('input');
	btnClear.type = 'button'; btnClear.value = '清除'; btnClear.className = 'btnOK';
	btnClear.onclick = LayerWindow.deleteSelectedItems;
	bbar.appendChild(btnClear);
	container.appendChild(bbar);

	var slbar = document.createElement('div');
	slbar.className = 'slbar';
	this._slPanel = slbar;
	container.appendChild(slbar);

	// 内容元素
	var cfrm = document.createElement('div');
	cfrm.style.height = container.offsetHeight - caption.offsetHeight - (parseInt(container.currentStyle.borderWidth) || 0) * 2 - 2 - 80;
	this._contentPanel = cfrm;
	container.appendChild(cfrm);
}

LayerWindow.deleteSelectedItems = function() {
	var win = getCurrentLayerWindow();
	var c = win.get_slPanel();
	for(var i = c.children.length - 1; i >= 0; i--) {
		c.children[i].dataitem.checked = false;
		Menu.setItemDefaultCls(c.children[i].dataitem, false);
		c.removeChild(c.children[i]);
	}
}

function _showLayerWindowShadow(css){
	
	var shadow = document.getElementById('_layerWindowShadow');
	if(shadow == null){
		shadow = document.createElement('div');
		shadow.id = '_layerWindowShadow';
		shadow.className = css;
		shadow.style.zIndex = _layerWindowZIndex - 1;
		shadow.oncontextmenu = function(){ return false; }
		document.body.appendChild(shadow);
	}

	if(shadow.style.display == 'none'){
		_oldOverflowX = document.body.style.overflowX;
		_oldOverflowY = document.body.style.overflowY;
	}

	// Debugger.debug("show:overflowX:{0}, overflowY:{1}, overflow:{2}", _oldOverflowX, _oldOverflowY,document.body.style.overflow);
	document.body.style.overflowX = 'hidden';
	document.body.style.overflowY = 'hidden';

	if(_shownWindows.length != 0){
		var topWin = _shownWindows[_shownWindows.length - 1];
		var zidx = topWin._winHtmlObj.style.zIndex;
		topWin._winHtmlObj.style.zIndex = shadow.style.zIndex;
		shadow.style.zIndex = zidx;
	}


	shadow.style.width = document.body.offsetWidth;
	shadow.style.height = document.body.offsetHeight;
	shadow.style.left = document.body.scrollLeft;
	shadow.style.top = document.body.scrollTop;
	shadow.style.display = 'block';
}

function _hideLayerWindowShadow(){
	var shadow = document.getElementById('_layerWindowShadow');
	if(shadow == null)
		return;

	if(_shownWindows.length == 0){
		document.body.style.overflowX = _oldOverflowX;
		document.body.style.overflowY = _oldOverflowY;
		shadow.style.display = 'none';
		// Debugger.debug("hide:overflowX:{0}, overflowY:{1}", _oldOverflowX, _oldOverflowY);
		showSelects();
		return ;
	}

	var win = _shownWindows[_shownWindows.length - 1];
	var shadowZIndex = shadow.style.zIndex;
	shadow.style.zIndex = win._winHtmlObj.style.zIndex;
	win._winHtmlObj.style.zIndex = shadowZIndex;
	/*
	shadow.style.zIndex = win.;
	win.set_zIndex(shadowZIndex);
	*/
}

function adjuestWindowsPosition(){
	var shadow = document.getElementById('_layerWindowShadow');
	if(shadow){
		shadow.style.width = document.body.offsetWidth;
		shadow.style.height = document.body.offsetHeight;
		shadow.style.left = document.body.scrollLeft;
		shadow.style.top = document.body.scrollTop;
	}

	for(var i = 0; i < _shownWindows.length; i++){
		var win = _shownWindows[i];
		var cx = ((document.body.offsetWidth - win._winHtmlObj.offsetWidth) / 2) + document.body.scrollLeft;
		var cy = ((document.body.offsetHeight - win._winHtmlObj.offsetHeight) / 2) + document.body.scrollTop;
		win._winHtmlObj.style.left = cx;
		win._winHtmlObj.style.top = cy;
	}
}

function getCurrentLayerWindow() {
	if(_shownWindows.length == 0)
		return ;

	return _shownWindows[_shownWindows.length - 1];
}

function notifyParentLayerWindow(obj, bClose){
	if(_shownWindows.length == 0)
		return ;

	var win = _shownWindows[_shownWindows.length - 1];
	
	if(bClose)
		win.hide();
	
	if (win.callback)
		win.callback.call(null, obj);
}

function closeCurrentLayerWindow() {
	if (_shownWindows.length == 0)
		return;

	var win = _shownWindows[_shownWindows.length - 1];
	win.hide();
	showSelects();
}

function createLayerWindow(parameters) {

	if (!parameters.windowClass)
		parameters.windowClass = 'w_main';
		
	if (!parameters.captionClass)
		parameters.captionClass = 'm_caption';
		
	if (!parameters.titleClass)
		parameters.titleClass = 'm_title';
		
	if (!parameters.shadowClass)
		parameters.shadowClass = 'm_shadow';
	
	var win = new LayerWindow(parameters);
	win.show();
	return win;
}

var _selects = null;
function hideSelects() {
	_selects = [];
	var sls = document.all.tags('select');
	var index = 0;
	for(var i = 0; i < sls.length; i++) {
		if(sls[i].style.display != 'none') {
			_selects[index++] = sls[i];
			sls[i].style.display = 'none';
		}
	}
}
function showSelects() {
	if(LayerWindow._showSelects) {
		LayerWindow._showSelects();
		return ;
	}
	if(_selects != null && _selects.length != 0) {
		for(var i = 0; i < _selects.length; i++) {
			_selects[i].style.display = '';
		}
		_selects = null;
	}
}
