// 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 () {
	// Code to run on page load
	// Add hover functionality
	Callouts.fix();
	DocLinks.init();
	Hover.init(Hover.collections);
	Rater.init();
});


/*------------------------------------------------------------------------+
 | 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");
		var arrBins = bin.getElementsByTagName("*");
		var classRE = /call-[lr]/gi;
		
		// Set div width = 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) {
					arrBins[i].style.width = images[0].offsetWidth + "px";
				}
			}
		}
		
		return false;
	}
};

/*----------------------------------------------+
 | DocLinks - Add icon after links to documents |
 +----------------------------------------------*/
var DocLinks = {
	init : function() {
		// Find all links
		var links = document.getElementsByTagName("a");
		
		for (var i = 0; i < links.length; i++) {
			var theLink = links[i];
			var address = theLink.href.toLowerCase();
			
			// Check if link points to files with common extensions
			var matches = address.match(/\.(doc|pdf|xls|ppt)/);
			
			if (matches) {
				// Using "match" always returns two results (not sure why)
				var ext = matches[0].substr(1, 3);
				
				// Create new image and insert it
				var newImg = document.createElement("img");
				newImg.alt = "(" + ext.toUpperCase() + ")";
				newImg.className = "icon";
				newImg.src = "../images/icon-" + ext + ".gif";
				newImg.title = newImg.alt;
				
				if (theLink.getElementsByTagName("img").length <= 0)
					theLink.parentNode.insertBefore(newImg, theLink);
				
				// Make link open in new window/tab
				theLink.onclick = function () {
					window.open(this.href);
					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),
		new Array("cal", "tr", 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];
			
			if (node.className.indexOf("current") == -1) {
				node.onmouseover = function() { this.className += " over"; }
				node.onmouseout = function() { this.className = this.className.replace(" over", ""); }
			}
		}
	}
};


/*----------------------------------+
 | Rater - Add stickiness to raters |
 +----------------------------------*/
var Rater = {
	// Initialize all raters
	init : function() {
		var lists = document.getElementsByTagName("ol");
		
		for (var i = 0; i < lists.length; i++) {
			if (lists[i].className.indexOf("rater") != -1) {
				var theRater = lists[i];
				
				// Set "current" class based on which INPUT is checked
				var inputs = theRater.getElementsByTagName("input");
				
				for (var j = 0; j < inputs.length; j++) {
				    if (inputs[j].checked) {
				        for (var k = j; j > -1; j--)
				            inputs[j].parentNode.parentNode.className = "current";					
					    break;
					}
				}
				
				// Find all labels
				var ratings = theRater.getElementsByTagName("label");
				
				for (var j = 0; j < ratings.length; j++) {
					var theRating = ratings[j];
					
					Event.observe(theRating, "click", function() { Rater.setRating(this); });
					theRating.onmouseover = function() { Rater.highlight(this); };
					theRating.onmouseout = function() { Rater.reset(this); };
				}
			}
		}
	},
	
	// Select radio button and highlight
	setRating : function(rating) {
		// Needed because LABELs don't work in Safari 1.x
		// Conditional used because IE doesn't recognize "rating" as LABEL element
		if (rating.nodeType == 1) rating.getElementsByTagName("input")[0].checked = true;
	},
	
	// Highlight all ratings up to current one
	highlight : function(rating) {
		// Get all LABELs in rater
		var siblings = rating.parentNode.parentNode.getElementsByTagName("label");
		var foundMyself = false;
		
		// Set "current" class on all ratings leading up to one being hovered
		for (var i = 0; i < siblings.length; i++) {
			if (siblings[i] === rating) foundMyself = true;
			siblings[i].parentNode.className = (!foundMyself) ? "current" : "";
			rating.parentNode.className = "current";
		}
	},
	
	// Reset rater
	reset : function(rating) {
		// Check for selected radio button. If there is one, reset highlights
		var inputs = rating.parentNode.parentNode.getElementsByTagName("input");
		var ratingChosen = false;
		var curRating = false;
		for (var i = 0; i < inputs.length; i++) {
			if (inputs[i].checked) {
				ratingChosen = true;
				var curRating = inputs[i].parentNode;
			}
		}
		
		if (ratingChosen) {
			// Reset highlights to previously selected rating
			Rater.highlight(curRating);
		} else {
			// Reset all ratings
			var siblings = rating.parentNode.parentNode.getElementsByTagName("label");
			for (var i = 0; i < siblings.length; i++) siblings[i].parentNode.className = "";
		}
	}
};

/*--------------------------------------------------+
 | Tog - Toggle visibility of two opposing elements |
 +--------------------------------------------------*/
var Tog = {
	swap : function (a, b) {
		a = document.getElementById(a);
		b = document.getElementById(b);
		
		if (!a || !b) return false;
		
		if (a.className.indexOf("closed") != -1) {
			oldClass = a.className;
			newClass = oldClass.replace(/closed/g, "");
			a.className = newClass;
			
			b.className += " closed";
		} else {
			oldClass = b.className;
			newClass = oldClass.replace(/closed/g, "");
			b.className = newClass;
			
			a.className += " closed";
		}
	},
	toggle : function (a) {
		a = document.getElementById(a);
		
		if (!a) return false;
		
		if (a.className.indexOf("closed") != -1) {
			oldClass = a.className;
			newClass = oldClass.replace(/closed/g, "");
			a.className = newClass;
		} else {
			a.className += " closed";
		}
	},
	togglePropertyValue : function (a, prop, value1, value2) {
		a = document.getElementById(a);
		
		if (!a) return false;
		
		if (a[prop].indexOf(value1) != -1) {
			a[prop] = value2;
		} else {
			a[prop] = value1;
		}
	}
};
/*--------------------------------------------------*/

var remote;
function launchWin(helpURL, size)
{
	// size string should have the format of 'width=#,height=#'
	// this avoids having to change all the function calls to launchHelp()
	var firstEqual = size.indexOf("=")+1;
	var comma = size.indexOf(",")+1;
	var secondEqual =  size.lastIndexOf("=")+1;
	var sizeLen = size.length;
	
	var w = parseInt(size.substring(firstEqual, comma));
	var h = parseInt(size.substring(secondEqual, sizeLen));
	
	var xPos = (screen.height-h)/2;
	var yPos = (screen.width-w)/2;
	remote = window.open(helpURL, "CMH", size+",scrollbars=1,resizable=1,left="+yPos+",top="+xPos);
	remote.focus();
}