/*------------------------------------------------------------------------------

Stuntbox
Global Scripts

------------------------------------------------------------------------------*/

/*- Run On Load --------------------------------------------------------------*/

// window.onload listener to share event with multiple functions
// Based on runOnLoad function from 
// "JavaScript: The Definitive Guide", 5th ed, p.434, example 17-7

runOnLoad.functionsQueue = new Array();
runOnLoad.isLoaded = false;

function runOnLoad(functionCall) {
	if (runOnLoad.isLoaded) functionCall();
	else runOnLoad.functionsQueue.push(functionCall);
}

runOnLoad.run = function() {
	if (runOnLoad.isLoaded) return;
	for (var i = 0; i < runOnLoad.functionsQueue.length; i++) {	
		//
		// runOnLoad.functionsQueue[i]();
		//
		try { runOnLoad.functionsQueue[i](); }		
		catch(functionError) { 
			if (functionError instanceof Error) {
				alert(functionError.name + ": " + functionError.message);
			}
		}
	}
	runOnLoad.isLoaded = true;
	delete runOnLoad.functionsQueue;
	delete runOnLoad.run;
};

if (window.addEventListener) {
	window.addEventListener("load", runOnLoad.run, false);
} else if (window.attachEvent) {
	window.attachEvent("onload", runOnLoad.run)
} else {
	window.onload = runOnLoad.run;
}

//
//
// REGISTER FUNCTIONS WITH runOnLoad HERE
runOnLoad(addHover);
runOnLoad(initLinkRels);
//
//

/*- Add Hover Support --------------------------------------------------------*/

// dynamically adds .hover class to elements for browsers with poor :hover
// pseudo-class support. Only applied to main nav LI elements. 
// Checking for document.defaultView because checking for
// document.defaultView.getComputedStyle will generate an error in IE

function addHover() {
	if (!document.getElementById) return false;
	if (!document.getElementsByTagName) return false;
	if (!document.defaultView) {	
		nav = document.getElementById("navigation");
		if (!nav) return false;
		navListItems = nav.getElementsByTagName("li");
		for (i = 0; i < navListItems.length; i++) {
			var node = navListItems[i];
			if (node.nodeName == "LI") {
				node.onmouseover = function() {
					this.className += " hover";
				};
				node.onmouseout = function() {
					this.className = this.className.replace(" hover", "");
				};
			}
		}
	}
}

/*- Init Link Relationship Attributes ----------------------------------------*/

function initLinkRels() {
	if (!document.getElementsByTagName) return false;
	var anchors = document.getElementsByTagName("a");
	for (var i = 0; i < anchors.length; i++) {
		var anchor = anchors[i];		
		// would have used getAttribute("rel") but cross-browser consistency 
		// on the return value is a mess when the attribute isn't present
		if (anchor.rel != "") {
			switch (anchor.rel) {
				case "popup":
					if (anchor.href.indexOf("popupWidth") != -1) {
						var popupWidth = anchor.href.match(/popupWidth=(\d+)/i)[1];
					} else {
						var popupWidth = 770;
					}
					if (anchor.href.indexOf("popupHeight") != -1) {
						var popupHeight = anchor.href.match(/popupHeight=(\d+)/i)[1];
					} else {
						var popupHeight = 600;
					}
					var attributes = "target=popwin,resizable=yes,width=" + popupWidth + ",height=" + popupHeight;
					var link = anchor.href;
					anchor.onclick = setPopup(link, attributes);
					break;
				case "newwindow":
					anchor.target = "_blank"; 
					break;
				case "newparent":
					anchor.target = "_parent"; 
					break;
			}
		}
	}
}

function setPopup(link, attributes) {
	return function() {
		window.open(link, "popwin", attributes);	
		return false;
	};
}