/* In this code we create a class that holds the path of an image and the text of it's credit
*  Then we define an array called "Picures" and initialize it to the paths and credits of all photo's 
*  used on the web page that we want preloaded. The order does not really matter.  The way to use this
*  file is to copy and paste if into the <script> area of the header of the HTML file.
*
*  The idea is that when we call "preLoadImage()" the constructor for an Image object is called and when
*  the "src" property of that object is loaded with a URL of an actual image, the Web Browser downloads
*  that image from the network and saves it in it's own cache.  Then when we later want to display that
*  image it is readily available. I am assuming it does not matter that the local variable "loadImage"
*  is does not exist once the function "preLoadImage()" returns.
*
*  The procedure is to load this javascript code. Then to initialize the Pictures array in the body
*  of the "script" section of the header. And finally to call the function preLoadImage.
*/

var goldenrod
if (!goldenrod) goldenrod = {}

goldenrod.globalcontext = this;

/*Importing classes
var Animate = goldenrod.Animate;
var Dom = goldenrod.Dom;
var Utility = goldenrod.Utility
var Eventshow = goldenrod.Eventshow
*/
goldenrod.preLoad = new Object; 

goldenrod.preLoad.Export = true;

goldenrod.preLoad.ModReport = function() {
	if(!goldenrod.preLoad.Export) alert(goldenrod.preLoad.ModReport.Message);
	}
	
goldenrod.preLoad.ModReport.Message = "April 10, 2010 \n" +
				   "Mod - The Dom javascript file must be included before the Preload file\n\n" + 
				   "Mod - The preload file sets up function arrays for onLoad, onResize, and UnLoad\n" +
				   "      These are installed and will run. The Dom.DPI_Init function is run for all pages.\n" +
				   "      Global variables for ViewportHeight and ViewportWidth are created and maintained by\n" + 
				   "      initializing them in an onLoad function and keeping them up to date in an onResize function\n\n" +
				   "Mod - \n" +
				   "      \n\n" +
				   "Mod - \n" + 
				   "      \n\n" +
				   "Mod - \n" +
				   "      \n" + 
				   "      \n" + 
				   "      \n\n";


goldenrod.preLoad.image_data = function(path, credit, title) {
	this.path=path; 
	if(credit == null) credit = ""; 
	this.credit = credit;
	if(title == null) title = ""; 
	this.title = title;
	}



goldenrod.preLoad.preLoadImage  = function(Pics) {

	var i, p = Pics.length;

	var loadImage = new Array();

	for (i = 0; i < p; i++){
		loadImage[i] = new Image();
		loadImage[i].src = Pics[i].path;
	};
	
}

/**************************************************************************************************
preLoad.Message - This code is for standardized shroud messages
**************************************************************************************************/

goldenrod.preLoad.Message = {};

goldenrod.preLoad.Message.shroud  = null;
goldenrod.preLoad.Message.msg_board = null;
goldenrod.preLoad.Message.opacity = 65;
goldenrod.preLoad.Message.upTo100 = new goldenrod.Animate.State(null, null, null, null, 100);
goldenrod.preLoad.Message.Faded  = new goldenrod.Animate.State(null, null, null, null, goldenrod.preLoad.Message.opacity);
goldenrod.preLoad.Message.Off  = new goldenrod.Animate.State(null, null, null, null, 0);

goldenrod.preLoad.Message.Shroud_on = new goldenrod.Animate.Event("shroud", goldenrod.preLoad.Message.Off, goldenrod.preLoad.Message.Faded, 1000, null, null );
goldenrod.preLoad.Message.Msg_board_up  = new goldenrod.Animate.Event("msg_board", goldenrod.preLoad.Message.Off, goldenrod.preLoad.Message.upTo100, 100, null, null );

goldenrod.preLoad.Message.eventlist = [new goldenrod.Animate.Occurence(goldenrod.preLoad.Message.Msg_board_up, 0), 
	   						 new goldenrod.Animate.Occurence(goldenrod.preLoad.Message.Shroud_on, 0)];
	
goldenrod.preLoad.Message.Reveal = new goldenrod.Eventshow("goldenrod.preLoad.Message.Reveal", goldenrod.preLoad.Message.eventlist, 0, 0, 1);

goldenrod.preLoad.Message.Show = function(){
	this.msg_board.style.visibility = "visible";
	this.msg_board.style.display = "block";
	this.shroud.style.display = "block";
	this.Reveal.run();
};

goldenrod.preLoad.Message.Hide = function(){
	this.msg_board.style.visibility = "hidden";
	this.msg_board.style.display = "none";
	goldenrod.Dom.setStyleOpacity(this.msg_board, 0);
	this.shroud.style.display = "none";
	goldenrod.Dom.setStyleOpacity(this.shroud, 0);
	this.Reveal.loopCount = 0;
	this.Reveal.totalTime = 0;
}

goldenrod.preLoad.Message.Setup = function(bColor, shroudTimeup, msgTimeup){
	if(bColor) this.shroud.style.backgroundColor = bColor;
	if(shroudTimeup) this.Shroud_on.duration = shroudTimeup;
	if(msgTimeup) this.Msg_board_up.duration = msgTimeup;
}



goldenrod.preLoad.initMessage = function(){
	goldenrod.preLoad.Message.shroud = document.createElement("div");
	goldenrod.preLoad.Message.shroud.id = "shroud";
	goldenrod.preLoad.Message.shroud.style.display = "none";
	document.body.appendChild(goldenrod.preLoad.Message.shroud);
	goldenrod.preLoad.Message.msg_board = document.getElementById('msg_board');
}





/**************************************************************************************************
preLoad.funcArray - This is a class that facilitates registering a list of functions that call be 
					executed one after the other 
**************************************************************************************************/
goldenrod.preLoad.funcArray = function() {
	this.List = new Array(); 
}

/**************************************************************************************************
preLoad.funcArrayItem - This is the type of object that the funcArray Array will hold.
					It is simply a function and a possible context.
**************************************************************************************************/
goldenrod.preLoad.funcArrayItem = function(f,t) {
	this.function_name = f; this.this_name = t;
}

/**************************************************************************************************
preLoad.funcArray.add - This is a prototype method that adds the function and context to the
		funcArray List. It return the new length of List.
**************************************************************************************************/
goldenrod.preLoad.funcArray.prototype.add = function(f,t) {
	var i,x;
	if(t == null) t = goldenrod.globalcontext;	//If t is not defined then use the global context.
	for(i = 0; i<this.List.length; i++) {
				//Do not add a function that is already there
		if( (this.List[i].function_name == f) && (this.List[i].this_name == t) ) 
			return this.List.length;
	}
	
	x = new goldenrod.preLoad.funcArrayItem(f,t);
	return this.List.push(x);
}

/**************************************************************************************************
preLoad.funcArray.replace - This is a prototype method that replaces one function with another 
	   and alters the context as well.
	   Replace f with g.
	   Returns true is the replace was successful returns false if the item was not found.
**************************************************************************************************/
goldenrod.preLoad.funcArray.prototype.replace = function(f,g,t) {
	var i,x;
	if(t == null) t = goldenrod.globalcontext;	//If t is not defined then use the global context.
	for(i = 0; i<this.List.length; i++) {
		if((this.List[i].function_name == f) && (this.List[i].this_name == t) ) {
			
			x = new goldenrod.preLoad.funcArrayItem(g,t);
			this.List.splice(i,1,x)	;
			return true;
		}
	}
	
	return false;
}
	
/**************************************************************************************************
preLoad.funcArray.add_func - This is a prototype method that adds the function and context to the
		funcArray List. It return the new length of List.
**************************************************************************************************/
goldenrod.preLoad.funcArray.prototype.run = function() {
	var i,x
	for(i=0; i<this.List.length; i++) {
		
	
		x = this.List[i];
		x.function_name.call(x.this_name); //The value of "this" for the invoked function is the array!
		}
}

/**************************************************************************************************
Create standard onLoad, OnResize, and onUnLoad function arrays and install them.
**************************************************************************************************/
goldenrod.onLoad_handler = new goldenrod.preLoad.funcArray();
goldenrod.onLoad_handler.add(goldenrod.Dom.featureTEST);


if(goldenrod.listMenu && goldenrod.listMenu.setHandlers) goldenrod.onLoad_handler.add(goldenrod.listMenu.setHandlers);

goldenrod.onResize_handler = new goldenrod.preLoad.funcArray();
goldenrod.unLoad_handler = new goldenrod.preLoad.funcArray();

window.onload = function() { goldenrod.onLoad_handler.run();}
window.onresize = function() { goldenrod.onResize_handler.run();}
window.onunload = function() {goldenrod.unLoad_handler.run();}

/**************************************************************************************************
Maintanin viewPortHeight and viewPortWidth in pixels.
**************************************************************************************************/
goldenrod.ViewportWidth = null;
goldenrod.ViewportHeight = null;


goldenrod.preLoad.initPortSize = function(){
	var shroud, msg_board;
	goldenrod.Dom.Viewport.setPointY( goldenrod.Dom.getViewportHeight() );
	goldenrod.ViewportHeight = goldenrod.Dom.Viewport.getPointY()
	goldenrod.Dom.Viewport.setPointX ( goldenrod.Dom.getViewportWidth() );
	goldenrod.ViewportWidth = goldenrod.Dom.Viewport.getPointX()
	shroud = document.getElementById("shroud");
	msg_board = document.getElementById("msg_board")
	if(shroud){
		shroud.style.width = goldenrod.ViewportWidth + "px";
		shroud.style.height = goldenrod.ViewportHeight + "px";
	}
	if(msg_board){
		goldenrod.Utility.centerElt(msg_board);
	}
}

goldenrod.onLoad_handler.add(goldenrod.preLoad.initMessage);
goldenrod.onLoad_handler.add(goldenrod.preLoad.initPortSize);

goldenrod.onResize_handler.add(goldenrod.preLoad.initPortSize);


//myAlert = function() {}

goldenrod.myAlertCount = 0;

goldenrod.myAlert = function(msg) {
	
	var elt = document.getElementById("alert");
	if(elt == null) return;
	if (goldenrod.myAlertCount < 25 )
		{elt.innerHTML = elt.innerHTML + "<br/>" + myAlertCount +": " + msg; myAlertCount++; }
		else {elt.innerHTML = msg; myAlertCount = 0} 
	}



/*********************************************************************************
*This function dump out all the properties and their values of a particular HTML 
*element.  
*str = is a string that identifies the element whose properties we will 
*elt = is the element itself
**********************************************************************************/
goldenrod.alt_dump_properties = function(str, elt) {

	var properties = str + " properties: <br/>";
	for( var propname in elt) {
		properties += propname + ": " + elt[propname] + "<br/>"
	}

//I want to write this so that it opens a scrollable window
}

/*********************************************************************************
*This function dump out all the properties and their values of a particular HTML 
*element.  
*str = is a string that identifies the element whose properties we will 
*elt = is the element itself
**********************************************************************************/
goldenrod.dump_properties = function(str, elt) {

	var properties = str + " properties: \n";
	for( var propname in elt) {
		properties += propname + ": " + elt[propname] + "\n"
	}

	alert(properties); 
}
/*********************************************************************************
*This function sets the minimum width of the <body> element to 3/4's of the total available 
*screen width
**********************************************************************************/
goldenrod.min_width = function() {
	var X = window.screen.availWidth;
	var b = document.getElementsByTagName('body')
	b[0].style.minWidth = Math.round(X * 3/4) + "px" ;
	//alert("The min width of the <body> elt is: " + b[0].style.minWidth + ". ")
}

goldenrod.check_DOM = function() {
	var x = "does"
	if (document.addEventListener == undefined) x="doesnt"

	alert("This browser " + x + " have the addEventListener interface.")
}


