/**
 * Shows preloader icon while page is in loading state.
 * 
 * <p>Requires global <code>$(String id)</code> function that returns element by id. See {@link #getProgressBar()}</p>
 * 
 * @requires cb.js
 */

// Uncomment this this if the path to the progress bar image is sticky
// Other wise you have to define it in
// var IMAGES_PATH = "./";
var PROGRESS_BAR_IMAGE_URI = "/images/preloader.gif";
var PROGRESS_BAR_ID = "preloader";

var ns4up = (document.layers) ? 1 : 0;
var ie4up = (document.all) ? 1 : 0;

/**
 * Gets current document width
 * 
 * @return Width of the document
 */
function getDocumentWidth() {
	var width;
	
	if (ns4up) {
		width = self.innerWidth;
	} else if (ie4up) {
		width = document.body.clientWidth;
	}
	
	return width;
}

/**
 * Gets current document height
 *
 * @return Height of the document
 */
function getDocumentHeight() {
	var height;
	
	if (ns4up) {
		height = self.innerHeight;
	} else if (ie4up) {
		height = document.body.clientHeight;
	}
	
	return height;
}

function getProgressBar() {
	return $(PROGRESS_BAR_ID);
}

/**
 * Hides progress bar
 */
function hideProgressBar() {
	var progressBar = getProgressBar();

	if (progressBar != null) {
		progressBar.style.visibility = "hidden";
	}
}

/**
 * Shows progress bar image while page is loading
 *
 * @param imageBasePath Base of the progress bar image or set IMAGES_PATH global configuration variable. Ex: http://www.server.com/module
 * @param imageUri Uri of the progress bar image relative to imageBasePath. 
 * The better is to set PROGRESS_BAR_IMAGE_URI global configuration variable. Ex: images/progress_bar.gif
 */

function showProgressBar(imageBasePath, imageUri) {
	var IMAGE_WIDTH = 189;
	var IMAGE_HEIGHT = 23;
	var top = (typeof PROGRESS_BAR_TOP == 'undefined') ? (getDocumentHeight() - IMAGE_HEIGHT)/2 : PROGRESS_BAR_TOP;
	var left = (typeof PROGRESS_BAR_LEFT == 'undefined') ? (getDocumentWidth() - IMAGE_WIDTH)/2 : PROGRESS_BAR_LEFT;
	var imageSrc;

	if (imageUri == null) {
		imageUri = PROGRESS_BAR_IMAGE_URI;
	}

	if (imageBasePath != null) {
		imageSrc = imageBasePath;
	} else if (typeof IMAGES_PATH != 'undefined') {
    	imageSrc = IMAGES_PATH + "/" + PROGRESS_BAR_IMAGE_URI;
    } else {
		imageSrc = PROGRESS_BAR_IMAGE_URI;
	}

	document.write('<span id="' + PROGRESS_BAR_ID + '" style="visibility: hidden;z-index: 1; position: absolute; top: ' + top + 'px; left: ' + left +'px;"><img src="' + imageSrc + '" ></span>');

	var progressBar = getProgressBar();

	if (progressBar != null) {
		progressBar.style.visibility = "visible";
	}
}


// Overloading page onload method to automatic invocation
// onload = hideProgressBar;
attachToEvent(window, "onload", hideProgressBar);

onhelp = function() {
	alert("Help is not avaible yet!");
	
	return false;
}
