/** *********************************************************************************
*	FILE:		busy.js
*********************************************************************************** */

var Busy = function()
{
	// Singleton class
	if (window.Busy)	return window.Busy;


	/* -- Public  ------------------------------------------------------------- */
	var _public =
	{


		initialize: function()
		{
			// Preload / Cache images
			(_private.busyImage=new Image()).src = "script/css/busy/Busy.gif";
			(_private.busyBackground=new Image()).src = "script/css/busy/BusyBackground.gif";
			_private._loadStyleFile("script/css/Busy.css");
		},


		showBusy : function(message)
		{
			_public.showBusyTimer(message, 250, 1500);
		},


		showBusyTimer : function(message, delayFog, delaySpinner)
		{
			message = (!message || typeof(message)!='string') ? 'Please Wait...' : message;
			_private.pageSize = _private.getPageSize();
			_private.buildWaitUnderlay();
			//_private.enablebusyNavigation(false);

			var waitWidth = 428;
			var waitHeight = 89;
			var waitLeft = _private.pageSize.scrollLeft + parseInt(Math.abs(_private.pageSize.windowWidth-waitWidth)/2);
			var waitTop = _private.pageSize.scrollTop + parseInt(Math.abs(_private.pageSize.windowHeight-waitHeight)/3);

			var pwDiv = $(document.createElement("div"));
			pwDiv = Object.extend(pwDiv, {id:"busy", name:"busy", className:"busy"});
			pwDiv.setStyle({position:"absolute", left:waitLeft+"px", top:waitTop+"px", height:waitHeight+"px", width:waitWidth+"px",zIndex:1000});
			pwDiv.appendChild(_private.busyImage);

			var busyPar = document.createElement("p");
			busyPar.style.marginTop = "0px";
			busyPar.appendChild(document.createTextNode(message));
			pwDiv.appendChild(busyPar);

			var htmlBody = document.getElementsByTagName("body");
			htmlBody[0].appendChild(pwDiv);
			var pwDiv = $('busy');
			Event.observe(pwDiv, 'click', _private.emptyFunction);

			_private.waitOpacity = 0;
			setTimeout(function() { _private.setDivOpacity('_waitunderlay',70)}, delayFog);
			_private.waitTimer = setTimeout(_private.fadeInWait, delaySpinner);
		},


		removeBusy : function()
		{
			// If we are still waiting to show the Please Wait, cancel that action
			if (_private.waitTimer)
			{
				clearTimeout(_private.waitTimer);
				_private.waitTimer = null;
			}

			_private.unhideDropDowns();

			_private.removeWaitUnderlay();
			//_private.enablebusyNavigation(true);

			var pwDiv = $('busy');
			if (pwDiv)
			{
				Event.stopObserving(pwDiv, 'click', _private.emptyFunction);
				pwDiv.style.display = 'none';
				pwDiv.style.zIndex = 0;
				var prnt = pwDiv.parentNode;
				prnt.removeChild(pwDiv);
			}
		},


		// Provides the minimal html to render the Busy within the overlay while loading the content page
		renderHtml : function(message, delayFog, delaySpinner, url)
		{
			var html =
				"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" +
				"<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">" +
				"<head>" +
					"<script type=\"text/javascript\">history.forward()<\/script>" +
					"<script type=\"text/javascript\" src=\"/script/prototype-1.6.0.3.js\"><\/script>" +
					"<script type=\"text/javascript\" src=\"/script/Overlay.js\"><\/script>" +
					"<script type=\"text/javascript\" src=\"/script/Busy.js\"><\/script>" +
					"<script type=\"text/javascript\">" +
						"function pageInit()" +
						"{" +
							"Busy.showBusyTimer('"+message+"',"+(delayFog ? delayFog:0)+","+(delaySpinner ? delaySpinner:200)+");"+
							"window.location.href = '"+url+"';"+
						"}" +
					"Event.observe(document, 'dom:loaded', pageInit);"+
					"<\/script>" +
				"<\/head>" +
				"<body><\/body>" +
				"<\/html>";
			return html;
		}


	};


	/* -- Private ------------------------------------------------------------- */
	var _private =
	{
		busyImage:			null,  // Image object of the spinner	"busy/Busy.gif"
		busyBackground:	null,	// Background image of the busy message "busy/BusyBackground.gif"
		waitOpacity:			0,
		waitTimer:			null,
		pageSize:				null,
		// The fade speed needs to be faster in IE since it's slower to fade
		fadeDelay : (Prototype.Browser.IE ? 6 : 50),


		fadeInWait : function()
		{
			_private.waitTimer = null;

			if (_private.waitOpacity == 0)	_private.hideDropDowns();

			if(_private.waitOpacity < 100)
			{
				_private.waitOpacity += 4;
				_private.setDivOpacity("busy", _private.waitOpacity);
				setTimeout(_private.fadeInWait, _private.fadeDelay);
			}
		},

		buildWaitUnderlay: function()
		{
			var isUnderlayThere = $("_waitunderlay");
			if (!isUnderlayThere)
			{
				var htmlBody = document.getElementsByTagName("body");
				var waitUnderlay = $(document.createElement("div"));
				waitUnderlay = Object.extend(waitUnderlay, {id:"_waitunderlay", name:"_waitunderlay", className:"wait-underlay"});
				waitUnderlay.setStyle({display:'inline', position:'absolute', top:'0px', left:'0px',
										height:_private.pageSize.pageHeight+"px", width:"100%", zIndex:'999'});
				htmlBody[0].appendChild(waitUnderlay);
			}
		},

		removeWaitUnderlay: function()
		{
			var waitUnderlay = $('_waitunderlay')
			if (waitUnderlay)
			{
				Event.stopObserving(waitUnderlay, 'click', _private.emptyFunction);
				waitUnderlay.style.display = 'none';
				waitUnderlay.style.zIndex = 0;
				var prnt = waitUnderlay.parentNode;
				prnt.removeChild(waitUnderlay);
			}
		},


		// Sets the opacity / transparency of an element to a specified opacity.
		// Supports IE, Gecko (Firefox, Netscape), WebKit (Safari, Chrome)
		// argDivName: Either the name of the Element or the Object itself
		// argOpacity: Opacity to set (0 to 100)
		setDivOpacity : function(argDivName, argOpacity)
		{
			// Updated to support all browsers including Safari / Chrome
			var value = parseInt(argOpacity) / 100;
			var div = $(argDivName);
			if (div)
			{
				if (Prototype.Browser.IE)
				{
					div.filters.alpha.opacity = (value*100);
					return;  // Stop at special case for IE to improve performance
				}
			    div.style.opacity = value;
			    div.style.MozOpacity = value;
			    div.style.KhtmlOpacity = value;
			}
		},


		// Hides a list of SELECT elements (use for IE browser only)
		hideDropDowns: function()
		{
			if (!Prototype.Browser.IE) return;
			var	elements = document.getElementsByTagName("select"),
					e, element, description, dLength;
			for (e = elements.length-1; e>=0; e--)
			{
				element = elements[e];
				if (element.style.display != 'none')
				{
					description = (element.selectedIndex >= 0) ? element.options[element.selectedIndex].text : '';
					dLength = Math.ceil(1.1*description.length);
					dLength = (dLength <= 0) ? 1: dLength;
					var newInputelement = document.createElement('input');
					newInputelement = Object.extend(newInputelement,
						{id:"xbusy_"+element.id, readOnly:true, className:'ReadOnly', type:'text', value:description, size:dLength});
					element.parentNode.insertBefore(newInputelement,element.nextSibling);
					newInputelement.style.width = parseInt(element.style.width)+'px';
					newInputelement.style.height = parseInt(element.style.height)+'px';
					element.style.display = 'none';
				}
			}
		},

		// Unhides all the elements that were hidden  (use for IE browser only)
		unhideDropDowns: function()
		{
			if (!Prototype.Browser.IE) return;
			var element, tmpElement, e, elements = document.getElementsByTagName("select");
			for (e = elements.length-1; e>=0; e--)
			{
				element = elements[e];
				tmpElement = $("xbusy_"+element.id);
				if (tmpElement)
				{// Existence of element prefixed by xcal_ indicates the calendar hid the select element
					element.parentNode.removeChild(tmpElement);
					element.style.display = '';
				}
			}
		},

		// Determine the page dimensions
		getPageSize: function(parent)
		{
			parent = parent || document.body;
			var windowWidth, windowHeight, pageHeight, pageWidth;
			if ( parent  != document.body )
			{
				windowWidth = parent.getWidth();
				windowHeight = parent.getHeight();
				pageWidth = parent.scrollWidth;
				pageHeight = parent.scrollHeight;
			}
			else
			{
				var xScroll, yScroll, dde = document.documentElement;
				if (window.innerHeight && window.scrollMaxY)
				{
					xScroll = document.body.scrollWidth;
					yScroll = window.innerHeight + window.scrollMaxY;
				}
				else if (document.body.scrollHeight > document.body.offsetHeight)
				{ // all but Explorer Mac
					xScroll = document.body.scrollWidth;
					yScroll = document.body.scrollHeight;
				}
				else
				{ // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
					xScroll = document.body.offsetWidth;
					yScroll = document.body.offsetHeight;
				}

				// If the ScrollHeight from the document element is taller, use that
				if (dde && dde.scrollHeight > yScroll)
					yScroll = dde.scrollHeight;

				if (self.innerHeight)
				{  // all except Explorer
					windowWidth = self.innerWidth;
					windowHeight = self.innerHeight;
				}
				else if (dde && dde.clientHeight)
				{ // Explorer 6 Strict Mode
					windowWidth = dde.clientWidth;
					windowHeight = dde.clientHeight;
				}
				else if (document.body)
				{ // other Explorers
					windowWidth = document.body.clientWidth;
					windowHeight = document.body.clientHeight;
				}

				// for small pages with total height less then height of the viewport
				if( yScroll < windowHeight )
				{
					pageHeight = windowHeight;
				}
				else
				{
					pageHeight = yScroll;
				}

				// for small pages with total width less then width of the viewport
				if(xScroll < windowWidth)
				{
					pageWidth = windowWidth;
				}
				else
				{
					pageWidth = xScroll;
				}
			}

			var scrOfX = 0, scrOfY = 0;
			if (typeof( window.pageYOffset ) == 'number')
			{
				//Netscape compliant
				scrOfY = window.pageYOffset;
				scrOfX = window.pageXOffset;
			}
			else if (document.body && (document.body.scrollLeft || document.body.scrollTop))
			{
				//DOM compliant
				scrOfY = document.body.scrollTop;
				scrOfX = document.body.scrollLeft;
			}
			else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
			{
				//IE6 standards compliant mode
				scrOfY = document.documentElement.scrollTop;
				scrOfX = document.documentElement.scrollLeft;
			}
			return( {pageWidth: pageWidth ,pageHeight: pageHeight , windowWidth: windowWidth, windowHeight: windowHeight, scrollLeft: scrOfX, scrollTop: scrOfY} );
		},

		// Enables the Please Wait bubble to automatically display after a short delay
		/*
			enablePleaseWaitNavigation: function(argState)
			{
				if (++_private._pleaseWaitTryCount == 5)
				{
					return;
				}
				try
				{
					this.ecmBase.enablePleaseWaitNavigation(argState);
				}
				catch (err)
				{
					if (argState)
					{
						setTimeout(function() { _public.enablePleaseWaitNavigation(true)}, 50);
					}
					else
					{
						setTimeout(function() { _public.enablePleaseWaitNavigation(false)}, 50);
					}
					return;
				}
				_private._pleaseWaitTryCount = 0;
			}
		*/

		//	Enable the inclusion of dependent library components
		_loadStyleFile: function(StyleSheetName)
		{
			var headID = document.getElementsByTagName("head")[0];
			var cssNode = document.createElement('link');
			cssNode.type = 'text/css';
			cssNode.rel = 'stylesheet';
			cssNode.href = StyleSheetName;
			cssNode.media = 'screen';
			headID.appendChild(cssNode);
		},

		emptyFunction : function(evnt)
		{
			Event.stop(evnt);
			return(false);
		}
	};

	return _public;
}();



// When the DOM is constructed, make sure the formMgr initialization is executed.
Event.observe(document, "dom:loaded", Busy.initialize);

