// Add event handler to body when window loads
function addLoadEvent(func) {
	var oldonload = window.onload;
	
	if (typeof window.onload != "function") {
		window.onload = func;
	} else {
		window.onload = function () {
			oldonload();
			func();
		}
	}
}

addLoadEvent(function () {
	Hover.init(Hover.collections);
	Callouts.fix();
});

/*------------------------------------------------------------------------+
 | Callouts - Adjust widths of callouts depending on size of image within |
 +------------------------------------------------------------------------*/
var Callouts = {
	fix : function() {
		// Check for functionality
		if (!document.getElementById || !document.getElementsByTagName) return false;
		
		var bin = document.getElementById("content-primary");
		var arrBins = bin.getElementsByTagName("*");
		var classRE = /call-[lr]/gi;
		
		// Set div width = largest image width
		for (var i = 0; i < arrBins.length; i++) {
			if (classRE.test(arrBins[i].className)) {
				var images = arrBins[i].getElementsByTagName("img");
				
				if (images.length >= 1) {
					var maxWidth = images[0].offsetWidth;
					
					for (var j = 0; j < images.length; j++) {
						var curWidth = images[j].offsetWidth;
						if (curWidth > maxWidth) maxWidth = curWidth;
					}
					
					if (maxWidth > 100) {
						arrBins[i].style.width = maxWidth + "px";
					}
				}
			}
		}
		
		return false;
	}
};


/*-----------------------------------------+
 | Hover - Add :hover functionality for IE |
 +-----------------------------------------*/
var Hover = {
	// Create two-dimensional array of identifiers for hover effect
	// [id/class] [child nodes] [unique]
	collections : new Array(
		new Array("nav", "li", true)
	),
	
	// Find all elemnts specified in array (IE only)
	init : function(collections) {
		if (document.all && document.getElementById && document.getElementsByTagName) {
			for (var i = 0; i < collections.length; i++) {
				var list = collections[i];
				var name = list[0];
				var delimiter = list[1];
				var unique = list[2];
				var children = new Array();
				
				if (unique) {
					// Unique element, find by ID
					var parent = document.getElementById(name);
					
					if (parent) {
						children = parent.getElementsByTagName(delimiter);
						Hover.addBehaviors(children);
					}
				} else {
					// Not unique, find by class
					var parents = document.getElementsByTagName("*");
					
					for (var j = 0; j < parents.length; j++) {
						if (parents[j].className.indexOf(name) > -1) {
							children = parents[j].getElementsByTagName(delimiter);
							Hover.addBehaviors(children);
						}
					}
				}
			}
		}
	},
	
	// Add class of "over" to elements when mouse hovers over them, remove when mouse stops hovering
	addBehaviors : function(collection) {
		for (var j = 0; j < collection.length; j++) {
			var node = collection[j];
			
			node.onmouseover = function() { this.className += " over"; };
			node.onmouseout = function() { this.className = this.className.replace(" over", ""); };
		}
	}
};