/**
 * LOADING SCREEN
 * Functions to show a loading screen, and locking the page
 
 BASICLY TAKEN FROM:
 * POPUP WINDOW CODE
 * Used for displaying DHTML only popups instead of using buggy modal windows.
 *
 * By Seth Banks (webmaster at subimage dot com)
 * http://www.subimage.com/
 *
 * Up to date code can be found at http://www.subimage.com/dhtml/subModal
 *
 * This code is free for you to use anywhere, just keep this comment block.
 */

/**
 * Loading WINDOW CODE
 */

// Popup code
var gLoadingMask = null;
var gLoadingContainer = null;
var gLoadingIsShown = false;

 /**
	* @argument width - int in pixels
	* @argument height - int in pixels
	* @argument url - url to display
	* @argument returnFunc - function to call when returning true from the window.
	*/

function showLoading( text ) { //(url, width, height, returnFunc) {
	gLoadingMask = document.getElementById("loadingMask");
	gLoadingContainer = document.getElementById("loadingContainer");
	gLoadingFrame = document.getElementById("loadingFrame");

    var width = 350;
    var height = 141;

	gLoadingIsShown = true;
	gLoadingMask.style.display = "block";
	gLoadingContainer.style.display = "block";
	// calculate where to place the window on screen
	centerLoadingWin(width, height);
	
	gLoadingContainer.style.width = width + "px";
	gLoadingContainer.style.height = (height) + "px";
	
	document.getElementById( "loadingText" ).innerHTML = text;
}

//
function centerLoadingWin(width, height) {
	if (gLoadingIsShown == true) {
		if (width == null || isNaN(width)) {
			width = gLoadingContainer.offsetWidth;
		}
		if (height == null) {
			height = gLoadingContainer.offsetHeight;
		}
		
		var fullHeight = getViewportHeight();
		var fullWidth = getViewportWidth();
		gLoadingMask.style.height = fullHeight + "px";
		gLoadingMask.style.width = fullWidth + "px";
	
		gLoadingContainer.style.top = ((fullHeight - (height)) / 2) + "px";
		gLoadingContainer.style.left =  ((fullWidth - width) / 2) + "px";
		//alert(fullWidth + " " + width + " " + gPopupContainer.style.left);
	}
}
addEvent(window, "resize", centerLoadingWin);


/**
 * @argument callReturnFunc - bool - determines if we call the return function specified
 * @argument returnVal - anything - return value 
 */
function hideLoading() {
	gLoadingIsShown = false;
	if (gLoadingMask == null) {
		return;
	}
	gLoadingMask.style.display = "none";
	gLoadingContainer.style.display = "none";
}
