﻿var Mask = function () 
{
	this.zIndexNow = 100;
	this.maskDiv = null;
	this.indexArr = [];
	this.show = function () 
	{
		this.zIndexNow += 10;
		if (this.indexArr.length == 0)
		{
			var maskHeight = document.documentElement.scrollHeight > getWindowSize()[1] ? document.documentElement.scrollHeight : getWindowSize()[1];
			this.maskDiv = createElement("div");
			this.maskDiv.onclick = function () 
			{
				var event = arguments[0] || window.event;
				event.cancelBubble = true;
			};
			this.maskDiv.ondblclick = function ()
			{
			    if (layer != null)
			        layer.closeLayer();
			}
			this.maskDiv.className = "mask";
			this.maskDiv.style.width = "100%";			
			this.maskDiv.style.height = maskHeight + "px";
			document.body.appendChild(this.maskDiv);
		}
		this.maskDiv.style.zIndex = this.zIndexNow - 1;
		this.indexArr.push(this.zIndexNow - 1);
		hideSelects(this.zIndexNow);
	};
	this.clean = function () 
	{
		showSelects(this.zIndexNow);
		this.indexArr.pop();
		if (this.indexArr.length > 0)
		{
			this.maskDiv.style.zIndex = this.indexArr[this.indexArr.length - 1];
			this.zIndexNow = this.indexArr[this.indexArr.length - 1] + 1;
		}
		else 
		{
            if (this.zIndexNow != 100)
			    document.body.removeChild(this.maskDiv);
			this.zIndexNow = 100;
		}
	};
};

function getWindowSize() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) 
	{
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} 
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
	{
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
	{
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return([myWidth,myHeight]);
}

function createElement(type) 
{
	return document.createElement(type);
}

function hideSelects(zIndexNow) 
{
	if (navigator.appVersion.indexOf("MSIE 6.0") != -1) 
	{
		var selects = document.getElementsByTagName("select");
		var length = selects.length;
		for (var i = 0; i < length; i++) 
		{
			if (selects[i].zIndexNow != zIndexNow && selects[i].style.visibility != "hidden") 
			{
				selects[i].style.visibility = "hidden";
				selects[i].zIndexNow = zIndexNow;
			}
		}
	}
}

function showSelects(zIndexNow) 
{
	if (navigator.appVersion.indexOf("MSIE 6.0") != -1) 
	{
		var selects = document.getElementsByTagName("select");
		var length = selects.length;
		for (var i = 0; i < length; i++) 
		{
			if (selects[i].zIndexNow == zIndexNow) 
			{
				selects[i].style.visibility = "";
				selects[i].zIndexNow = -1;
			}
		}
	}
}
