/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *  *
 * File:	background.js														*
 * Classes: n/a																	*
 * Author:  Keith Borgmann														*
 * Date:	2009-05-14															*
 * Version: 1.1.1																*
 *																				*
 * Update Notes:																*
 *  background.js now supports mutliple "layers" of backgrounds.				*

	*** ALL FOLLOWING COMMENTS NEED TO BE UPDATED. ***


 * Description:																	*
 *   Creates a transparent 'background' that disables access to most of the		*
 *	  page.  All objects at z-index:2; or greater will still be accessible.		*

	//TODO: update this note

 *																				*
 * Methods List:																*
 *	createBackground();	   //only needs to be called once (do it onload)		*
 *	activateBackground(fn);   //fn - function called if the layer is clicked	*
 *	deactivateBackground();   //Close the background & calls fn					*
 *																				*
 * Dependencies:																*
 *	compatibility.js(1.1.0), background.css, IMG_BACKGROUND (see below)			*
 *																				*
 * Notes:																		*
 *	Only supports one background at a time, and always at z-index:1;			*
 Works best with <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

 	//TODO: update this note

 *																				*
 * BEFORE USING:																*
 *	Copy Files:																	*
 *		../common/js/background.js												*
 *		../common/css/background.css											*
 *		../common/img/transparent.gif											*

	//TODO: update this note


 *																				*
 *	Add to html/jsp file:														*
 *		<script language='JavaScript' src='inc/background.js'></script>			*
 *		<link href='css/background.css' rel='stylesheet' type='text/css'>		*
 *																				*
 *	Add to window.onload:														*
 *		createBackground();														*

	//TODO: update this note


 *																				*
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

//TODO: validate full compatibility for mozilla & ns


//CONSTANTS//////
var BACKGROUNDID   = "_div_background_v100_";	//unique id for the div
var BACKGROUND_CLASS = "divBackground";			//class name (see background.css)
var IMG_BACKGROUND = "../common/img/transparent.gif";	//a transparent gif.



/////////////////////////////////////////////////////////////
//Class: ActiveBackground
//Desc:  Holds info about a layered instance of a background.
//		 Used Internally by the static methods.
//

//Constructor: ActiveBackground
//Desc:   Create a new ActiveBackground instance, and activates it.
//Params: strFunction - onclick/oncomplete code.
function ActiveBackground(strFunction) {

	//Instance Members
	this.functionDone = strFunction;
	this.layer = ActiveBackground.layer+1;

	ActiveBackground.float();

	//Set global reference.
	ActiveBackground.layers[this.layer-1] = this;

	//Prepare onclick
	var back = ActiveBackground.background;
	eval("back.onclick = function() { "+this.functionDone+"; return false;}");
}

//Method: deactivate
//Desc:   Deactivate a layered background
//Params: blnCallDone - if true, call the onclick/oncomplete code.
ActiveBackground.prototype.deactivate = function(blnCallDone) {
	if (ActiveBackground.layer==this.layer) {
		ActiveBackground.sink();
		if (blnCallDone)
			eval(this.functionDone);
	}

	var currentLayer = this.layer;

	//clear the global reference
	ActiveBackground.layers[currentLayer-1] = null;
	currentLayer--;

	if (currentLayer > 0) {
		var back = ActiveBackground.background;
		var bg = ActiveBackground.layers[currentLayer-1];
		//Prepare new onclick
		eval("back.onclick = function() { "+bg.functionDone+"; return false;}");
	}
}

//Static method: Sink background down one layer
ActiveBackground.sink = function() {
	if (ActiveBackground.layer!=0) {
		var back = ActiveBackground.background;

		ActiveBackground.layer--;
		if (ActiveBackground.layer==0)
			back.style.display = "none";
		else
			back.style.zIndex = ActiveBackground.layer;

	} else {
		alert("Cannot sink background: already at level 0");
	}
};

//Static method: Float background up one layer
ActiveBackground.float = function() {
	var back = ActiveBackground.background;
	ActiveBackground.layer++;
	back.style.zIndex = ActiveBackground.layer;
	if (ActiveBackground.layer==1)
		back.style.display = "";
};

//Static method: Move the div in front of the background
ActiveBackground.toForeground = function(div) {
	div.style.zIndex = ActiveBackground.layer+1;
}
//End of Class
/////////////////////////////////////////////////////////////





//Global Functions/////////////////////////////////////////////////////////////
//TODO: move this to util.js
function createDiv(strDivName, objPreceedingChild) {
	var dv = document.createElement('div');
	if (strDivName!=null)
		dv.setAttribute('id', strDivName);
	insertElement(dv, objPreceedingChild);
	return dv;
}

//Inserts an html element to the document body, or after the specified element
//TODO: move this to util.js
function insertElement(objInsert, objPreceedingChild) {
	if (objPreceedingChild==null)
		document.body.appendChild(objInsert);
	else {
		var parent = objPreceedingChild.parentNode;
		var next = objPreceedingChild.nextSibling;
		if (next==null)		
			parent.appendChild(objInsert);
		else
			parent.insertBefore(objInsert, next);
	}
}
///////////////////////////////////////////////////////////////////////////////


//Creates a 'background' that encompases the entire screen
//Only needs to be called once, but multiple calls to not create errors.
//Optionally set a new transparent image file.
function createBackground(img) {
	if (img) 
		IMG_BACKGROUND = img;

	if( element(BACKGROUNDID)==null ) {
		var back = createDiv(BACKGROUNDID);

		back.className = BACKGROUND_CLASS;
		back.style.display = "none";
		back.functionDone = "";
		back.style.background = "url("+IMG_BACKGROUND+") 0 0 repeat";

		if (typeof(ActiveBackground.layer)=="undefined") {
			ActiveBackground.layers = new Array();
			ActiveBackground.layer = 0;
			ActiveBackground.background = back;
		}
	}
	
}

//returns a reference to the background div
function activateBackground(strFunction) {
	var back = element(BACKGROUNDID);
	if( back==null ) {
		createBackground();		//try to create the background
		back = ActiveBackground.background;
	}
	resizeBackground();
	var current = new ActiveBackground(strFunction);	//new instance
	return back;
}
//Remove the current-layer background (drop one);
function deactivateBackground(blnCallDone) {
	try {
		var layer = ActiveBackground.layer;
		ActiveBackground.layers[layer-1].deactivate(blnCallDone);
	} catch(e) {
		//alert("Error: "+e.message);
	}
}
//resize the background.
function resizeBackground(toZero) {
	var back = ActiveBackground.background;
	if (toZero) {
		back.style.width = "0px";
		back.style.height = "0px";
	} else {
		var size = getBrowserSize();
		if (document.viewport) {
			var width = Math.max( document.viewport.getWidth(), size.width);
			var height = Math.max( document.viewport.getHeight(), size.height);
		} else {
			var width = size.width;
			var height = size.height;
		}

		
		back.style.width = width+"px";
		back.style.height = height+"px";


		//alert("Doc: "+dw+"x"+dh+"\nBrows: "+ size.width+"x"+size.height);
	}
}



//////////////////////////////////////////////////////////////////

//Code for onDemand loading.  Must be the last lines in the file.
if ( typeof(onDemandJSLoaded)=="undefined") var onDemandJSLoaded = new Object();
onDemandJSLoaded["background.js"] = true;
if ( typeof(onDemandCallback)=="function" ) onDemandCallback();