
var DHTMLMask = Class.create();

DHTMLMask.prototype =
{
	/**
	 * Initialisation du masque empéchant les intéractions avec la page sous la fenêtre
	 */
	initMask:function(p_maskName)
	{
		this.gTabbableTags = new Array("A","BUTTON","TEXTAREA","INPUT","IFRAME");
		this.gTabIndexes = new Array();
		this.gHideSelects = false;
		// check to see if this is IE version 6 or lower. hide select boxes if so
		// maybe they'll fix this in version 7?
		var brsVersion = parseInt(window.navigator.appVersion.charAt(0), 10);
		if (brsVersion <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1)
		{
			this.gHideSelects = true;
		}

		this.maskName = p_maskName;

		if($(this.maskName))
		{
			this.maskDiv = $(this.maskName);
		}
		else
		{
			this.maskDiv = document.createElement('div');
			this.maskDiv.id = this.maskName;
			this.maskDiv.className = this.maskName;
			this.maskDiv.innerHTML = "&nbsp;";
			document.body.appendChild(this.maskDiv);
			this.maskDiv.style.zIndex = 10000;
		}
		this.resizeHandler = createMethodReference(this, "updateMaskSize");
		Event.observe(window, "resize", this.resizeHandler);
		this.updateMaskPositionHandler = createMethodReference(this, "updateMaskPosition");
		Event.observe(window, "scroll", this.updateMaskPositionHandler);
	},

	/**
	 * Affichage du masque de la page
	 */
	showMask:function()
	{
		this.disableTabIndexes();

		// If using Mozilla or Firefox, use Tab-key trap.
		if(!document.all)
		{
			this.previousKeyHandler = document.onkeypress;
			document.onkeypress = keyDownHandler;
		}

		// Cache les boites de sélection qui sinon apparaissent par dessus la boite
		// de dialogue
		if(this.gHideSelects == true)
		{
			this.hideSelectBoxes();
		}

		this.updateMaskPosition();
		this.updateMaskSize(null);
		this.maskDiv.style.display = "block";
	},

	/**
	 * Met à jour la position du masque en fonction des scrolls
	 */
	updateMaskPosition:function()
	{
		var maskDiv = $(this.maskName);
		var theBody = document.documentElement;
		var scLeft = getViewportX();
		var scTop = getViewportY();
		maskDiv.style.top = scTop + "px";
		maskDiv.style.left = scLeft + "px";
	},

	/**
	 * Met à jour la taille du masque en fonction de la zone visible du document
	 * dans le navigateur.
	 */
	updateMaskSize:function(event)
	{
		var maskDiv = $(this.maskName);
		var fullWidth = getInnerDocumentWidth();
		maskDiv.style.width = fullWidth + "px";
		var fullHeight = getInnerDocumentHeight();
		maskDiv.style.height = fullHeight + "px";
	},

	// For IE. Restore tab-indexes.
	restoreTabIndexes:function()
	{
		if (document.all)
		{
			var i = 0;
			for (var j = 0; j < this.gTabbableTags.length; j++)
			{
				var tagElements = document.getElementsByTagName(this.gTabbableTags[j]);
				for (var k = 0 ; k < tagElements.length; k++)
				{
					tagElements[k].tabIndex = this.gTabIndexes[i];
					tagElements[k].tabEnabled = true;
					i++;
				}
			}
		}
	},

	/**
	 * Fonction de fermeture de la fenêtre
	 */
	hideMask:function()
	{
		this.restoreTabIndexes();
		if (this.maskDiv == null)
		{
			return;
		}
		this.maskDiv.style.display = "none";

		// display all select boxes
		if (this.gHideSelects == true)
		{
			this.displaySelectBoxes();
		}

		Event.stopObserving(window, "resize", this.resizeHandler);
		Event.stopObserving(window, "scroll", this.updateMaskPositionHandler);
		if(!document.all)
		{
			document.onkeypress = this.previousKeyHandler;
		}
	},

	/**
	* Hides all drop down form select boxes on the screen so they do not appear above the mask layer.
	* IE has a problem with wanted select form tags to always be the topmost z-index or layer
	*/
	hideSelectBoxes:function()
	{
		this.selectList = new Array();
		for(var i = 0; i < document.forms.length; i++)
		{
			for(var e = 0; e < document.forms[i].length; e++)
			{
				if(document.forms[i].elements[e].tagName.toUpperCase() == "SELECT" &&
				document.forms[i].elements[e].style.visibility != "hidden")
				{
					this.selectList.push(document.forms[i].elements[e]);
					document.forms[i].elements[e].style.visibility="hidden";
				}
			}
		}
	},

	/**
	* Makes all drop down form select boxes on the screen visible so they do not reappear after the dialog is closed.
	* IE has a problem with wanted select form tags to always be the topmost z-index or layer
	*/
	displaySelectBoxes:function()
	{
		for(var i=0; i<this.selectList.length; i++)
		{
			this.selectList[i].style.visibility="visible";
		}
	},

	// For IE.  Go through predefined tags and disable tabbing into them.
	disableTabIndexes:function()
	{
		if (document.all)
		{
			var i = 0;
			for (var j = 0; j < this.gTabbableTags.length; j++)
			{
				var tagElements = document.getElementsByTagName(this.gTabbableTags[j]);
				for (var k = 0 ; k < tagElements.length; k++)
				{
					this.gTabIndexes[i] = tagElements[k].tabIndex;
					tagElements[k].tabIndex="-1";
					i++;
				}
			}
		}
	}
}


/**
 * Gestion de la frappe de touche : annule l'évenement si c'est la touche tab
 */
function keyDownHandler(e)
{
	if(getKeyCode(e) == 9)
	{
		e.returnValue = false;
		return false;
	}
}
