﻿/*****************************************************************/
/********  CONSTANTS: Change to match your settings  *************/
/*****************************************************************/

var gWaitingScreenImageSrc;
//var gWaitingScreenBackgroundImageSrc = "image/WaitingScreen/BGGray.png"
var gWaitingScreenBackgroundImageSrc;

gWaitingScreenImageSrc=gWaitingScreenBackgroundImageSrc = (typeof(_rootFolderRelation) == "undefined" ? "" : _rootFolderRelation);

gWaitingScreenImageSrc           += "images/WaitingScreen/loading-transparent.gif";
gWaitingScreenBackgroundImageSrc += "images/WaitingScreen/BGWhite.png";

/*****************************************************************/
/*****************************************************************/


//===============================================================//
//  
// The script contains the global functions "ShowWaitingScreen()"
// and "HideWaitingScreen()" 
//  
// There are two backgroung images, one for gray background and
// one for white, set the constants above to change            
//  
//===============================================================//
//===============================================================//


// global vars
var gImgWaitingScreenSymbol = new Image();
var gObjWaitingScreen = null;

gImgWaitingScreenSymbol.src = gWaitingScreenImageSrc;


function gSetWaitingScreenInstance()
{
	if (gObjWaitingScreen == null) gObjWaitingScreen = new classWaitingScreen();
	return gObjWaitingScreen;
}


function gShowWaitingScreen()
{
	gSetWaitingScreenInstance();
	gObjWaitingScreen.Show();
}	


function gHideWaitingScreen()
{
	gObjWaitingScreen.Hide();
}	


function gWaitingScreenSetSize()
{
	if (gObjWaitingScreen != null) gObjWaitingScreen.SetSize();
}


/**************************************************************/
/*       C L A S S      W A I T I N G     S C R E E N         */
/**************************************************************/

function classWaitingScreen()
{
	// constants
	this.constIdDivBackground = "WaitingScreenBackground";
	this.constIdDivSymbol     = "WaitingScreenSymbol";

	// vars
	this.bSelectsTags = false;
	this.orgOnkeypressFunc = null;


	/**************************************************************/
	/*        S H O W     W A I T I N G     S C R E E N           */
	/**************************************************************/

	classWaitingScreen.prototype.Show = function()
	{

		if ($(this.constIdDivBackground) == null) this.Init();

		this.SetSize();

		$(this.constIdDivBackground).style.display = "block";
		$(this.constIdDivBackground).style.visibility = "visible";
		$(this.constIdDivSymbol).style.display     = "block";
		$(this.constIdDivSymbol).style.visibility  = "visible";

		if (gIsBrowserMozilla())   // If using Mozilla or Firefox, use Tab-key trap.
		{
			this.orgOnkeypressFunc = document.onkeypress;
			document.onkeypress = this.OnKeyPress;  //gOnWaitingScreenKeyPress;
		}

		this.HideUnderlyingElements();

		gAddEvent(window, "resize", gWaitingScreenSetSize);
		gAddEvent(window, "scroll", gWaitingScreenSetSize);
	}


	
	
	/**************************************************************/
	/*        H I D E     W A I T I N G     S C R E E N           */
	/**************************************************************/
	
	classWaitingScreen.prototype.Hide = function()
	{
		gRemoveEvent(window, "resize", gWaitingScreenSetSize);
		gRemoveEvent(window, "scroll", gWaitingScreenSetSize);
	
		if (gIsBrowserMozilla())   // If using Mozilla or Firefox, reset Tab-key trap.
		{
			document.onkeypress = this.orgOnkeypressFunc;
		}

		$(this.constIdDivBackground).style.display = "none";
		$(this.constIdDivBackground).style.visibility = "hidden";
		this.HideSymbol();
		
		this.ShowUnderlyingElements();
	}

	
	classWaitingScreen.prototype.HideSymbol = function()
	{
		$(this.constIdDivSymbol).style.display     = "none";
		$(this.constIdDivSymbol).style.visibility  = "hidden";
	}
	
	/**************************************************************/
	/*        I N I T     W A I T I N G     S C R E E N           */
	/**************************************************************/

	classWaitingScreen.prototype.Init = function()
	{
		var body, divBackground, divSymbol;

		body = document.getElementsByTagName('BODY').item(0);

		divBackground = document.createElement('DIV');
		divBackground.id = this.constIdDivBackground;

		divSymbol = document.createElement('DIV');
		divSymbol.id = this.constIdDivSymbol;
		divSymbol.innerHTML = '<img src="' + gWaitingScreenImageSrc + '" border=0>';

		if (gIsBrowserFirefox() || gIsBrowserOpera())
			divBackground.style.backgroundImage = "url(" + gWaitingScreenBackgroundImageSrc + ")";

		body.appendChild(divBackground);
		body.appendChild(divSymbol);

		// check to see if this is IE version 6 or lower. hide select boxes if so maybe they'll fix this in version 7?
		this.bSelectsTags = gIsBrowserIE(); // && parseFloat(window.navigator.userAgent.substring(window.navigator.userAgent.indexOf("MSIE")+4)) <= 6
	}
	
	
	/**************************************************************/
	/*         U T I L I T I E S                                  */
	/**************************************************************/
	
	classWaitingScreen.prototype.SetSize = function()
	{
		var divBackground, divSymbol, clientArea;
		
		divBackground = $(this.constIdDivBackground);
		divSymbol     = $(this.constIdDivSymbol);

		clientArea = gGetClientAreaSize();

		divBackground.style.left   = clientArea.left   + "px";
		divBackground.style.top    = clientArea.top    + "px";
		divBackground.style.width  = clientArea.width  + "px";
		divBackground.style.height = clientArea.height + "px";
	
		divSymbol.style.left = clientArea.left + (clientArea.width-gImgWaitingScreenSymbol.width)/2   + "px";
		divSymbol.style.top  = clientArea.top  + (clientArea.height-gImgWaitingScreenSymbol.height)/2 + "px";
	}
	
	
	/********************************************************************/
	/*     S H O W / H I D E   U N D E R L Y I N G  E L E M E N T S     */
	/********************************************************************/
	
	classWaitingScreen.prototype.HideUnderlyingElements = function()
	{
		this.HideObjects();
		if (this.bSelectsTags == true) this.DisableDocumentSelectBoxes();
	}
	
	classWaitingScreen.prototype.ShowUnderlyingElements = function()
	{
		this.ShowObjects();
		if (this.bSelectsTags == true) this.RestoreDocumentSelectBoxes();
	}
	
	classWaitingScreen.prototype.HideObjects = function()
	{
		if (gIsBrowserIE())
		{
			var i, aObj = document.getElementsByTagName("OBJECT");
	
			for (i=0; i<aObj.length; i++)
			{
				aObj[i].orgVisibilityVal = aObj[i].style.visiblity;
				aObj[i].style.visibility = "hidden";
			}
		}
	}
	
	classWaitingScreen.prototype.ShowObjects = function()
	{
		if (gIsBrowserIE())
		{
			var i, aObj = document.getElementsByTagName("OBJECT");
	
			for (i=0; i<aObj.length; i++)
			{
				if (gIsBrowserIE()) aObj[i].disabled = aSelect[i].orgDisabledVal;
			}
		}
	}

	
	classWaitingScreen.prototype.DisableDocumentSelectBoxes = function()
	{
		var i, aSelect = document.getElementsByTagName("SELECT");
	alert("DisableDocumentSelectBoxes: " + aSelect.length);
		for (i=0; i<aSelect.length; i++)
		{
			//aSelect[i].orgVisibilityVal = aSelect[i].style.visiblity;
			//if (gIsBrowserIE()) aSelect[i].style.visibility = "hidden";

			aSelect[i].orgDisabledVal = aSelect[i].disabled;
			aSelect[i].disabled = true;
		}
	}
	
	classWaitingScreen.prototype.RestoreDocumentSelectBoxes = function()
	{
		var i, aSelect = document.getElementsByTagName("SELECT");
		
		for (i=0; i<aSelect.length; i++) 
		{
			//aSelect[i].style.visibility = aSelect[i].orgVisibilityVal;
			aSelect[i].disabled = aSelect[i].orgDisabledVal;
		}
	}
	
	// Tab key trap. iff popup is shown and key was [TAB], suppress it.
	// @argument e - event - keyboard event that caused this function to be called.
	classWaitingScreen.prototype.OnKeyPress = function(e)
	{
	    if (e.keyCode == 9) return false;
	}
}
