// window onLoad registration (from Javacript 5th edition David Flanagan p434)
//modified to pass function parameters

function runOnLoad(f,param) {
	//for listing pages
	//alert("runonload: param = " + param);

	if(runOnLoad.loaded) {
		f(param);
	} else {
		runOnLoad.funcs.push(f);
		runOnLoad.params.push(param);
	}
}

runOnLoad.funcs = [];
runOnLoad.params = [];
runOnLoad.loaded = false;

runOnLoad.run = function () {
	var output="";
	if(runOnLoad.loaded) return;
	//alert(runOnLoad.funcs.length);
	for (var i = 0; i < runOnLoad.funcs.length; i++) {
		//alert(runOnLoad.funcs[i]);
		try {runOnLoad.funcs[i](runOnLoad.params[i]); }
		catch(e) { }
		output += runOnLoad.funcs[i].name + "( " + runOnLoad.params[i] + ")\n";
	}
	//alert("runOnLoad.run loaded: \n" + output);
	runOnLoad.loaded = true;
	delete runOnLoad.funcs;
	delete runOnLoad.params;
	delete runOnLoad.run;
		
};

//Generic event object (DOM & IE
						
function Event(e) {
	
	if(!e.target) {
		this.target = e.srcElement;
	} else {
		
		this.target = e.target;
	}
	this.type = e.type;
	this.clientX = e.clientX;
	this.clientY = e.clientY;
	this.id = this.target.id;
	//log(e,1,"Event","this.id");
	//alert(this.id);

}

//Generic addEventListener

function eventListenerAdd(theElement, theEvent, theHandler, capturePropagation) {
	
	if(window.addEventListener) {
		//alert("DOM: " + theElement);
		theElement.addEventListener(theEvent, theHandler, capturePropagation);
	} else if (window.attachEvent) {
		//alert("IE: " + theElement);
		theElement.attachEvent(("on"+theEvent), theHandler);
		
	}
}

// window onLoad registration (from Javacript 5th edition David Flanagan p434)
if(window.addEventListener) {
	window.addEventListener("load", runOnLoad.run, false);
} else if (window.attachEvent) {
	window.attachEvent("onload", runOnLoad.run);
} else {
	window.onload = runOnLoad.run;
}

/*

function qw
A more robust alert mechanism that will check the type of the input parameter and output nice text for strings, objects, arrays etc
level - integer - 
the level of logging 
1 logs a function calls
2 is a common alert for debugging
3 is for failures. exceptions, etc,
4 is live user alerts.
*/
var loglevel = 1;
var logWindow; //stores the log window object
var inputSave = "";
var levelSave = 1;
var callingFunctionSave;
function log(input, level, callingFunction, additionalTag) {
	inputSave = input;
	levelSave = level;
	if(callingFunction) {
		callingFunctionSave = callingFunction;
		if(additionalTag) callingFunctionSave += ": " + additionalTag;
	} else {
		if(additionalTag) callingFunctionSave = additionalTag;
	}
	
	
	if(level < loglevel) return;
	if(logWindow && logWindow.closed == false) {
		//alert("window.log exists");
		
		//logWindow.focus();
		logWindow.writeLog();
		
	} else {
		//alert("window.log DOESNT exist:" + window.logg);
		logWindow = window.open("/log.html","logg", "width=600,height=800,location=no,menubar=no,resizable=yes,scrollbars=yes,toolbar=no,top=100,left=1000");
	}

	//the rest of the logging is handled by the function writeLog() in the file logging.js which is loaded by log.html
	//that file accesses the global vars set here, inputSave and levelSave
	//we do this because we have to wait for the log window to load before we can add elements to it
	//so it makes sense to have the log window's onload event call a function stored for that window
	//alert(typeof(input));
		
	
}



function highlightPageNav() {
	if(thisPageNavName) {
		var nav = document.getElementById(thisPageNavName);
		nav.style.border="1px solid #afbc22";
		
	}
}

runOnLoad(highlightPageNav);
