// 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
	Hover.init(Hover.collections);
	ToggleRow.init();
});


/*-----------------------------------------+
 | 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];
			
			if (node.className.indexOf("current") == -1) {
				node.onmouseover = function() { this.className += " over"; }
				node.onmouseout = function() { this.className = this.className.replace(" over", ""); }
			}
		}
	}
};

/*----------------------------------+
 | Collapse - Add collapse/expand functionality |
 +----------------------------------*/
var ToggleRow = {
	// Initialize popup triggers
	init : function() {
		if (!document.getElementById || !document.getElementsByTagName)
			return false;
		
		// Find all anchors
		var anchors = document.getElementsByTagName("a");
		
		for (var i = 0; i < anchors.length; i++) {
			var curAnchor = anchors[i];
			
			// Only work with anchors with class "expand" or "hide"
			if (curAnchor.className.indexOf("expand") != -1) {
				curAnchor.onclick = ToggleRow.show;
			} else if (curAnchor.className.indexOf("hide") != -1) {
				curAnchor.onclick = ToggleRow.hide;
			}
		}
	},
	
	// Show popup
	show : function () {
		// Find and position popup based on ID(s) in class value
		var classes = this.className.split(" ");
		for (var j = 0; j < classes.length; j++) {
			if (classes[j].indexOf("popup") != -1) {
				var trList = getElementsByClassNameForTag(classes[j], "tr");
				if (!trList) return false;
				
				for(var i = 0; i < trList.length; i++)
				{
				    var p = trList[i];
				
				    // Toggle "closed" class on popup
				    if (p.className.indexOf("closed") > -1) {
					    var oldClass = p.className;
					    var newClass = oldClass.replace(/closed/g, "");
					    p.className = newClass;
				    } else {
					    p.className += " closed";
				    }			   
				}
    					
			    // Replace trigger's text (expand -> hide) or vice versa
			    var oldText = this.childNodes[0];
			    var newText = (oldText.nodeValue.toLowerCase() == "show options") ? document.createTextNode("Hide options") : document.createTextNode("Show options");
			    var temp = this.replaceChild(newText, oldText);				
			}
		}
		
		// Replace trigger's class name to change icon
		var oldClass = this.className;
		var newClass = (oldClass.indexOf("expand") > -1) ? oldClass.replace(/expand/g, "hide") : oldClass.replace(/hide/g, "expand");
		this.className = newClass;
		
		return false;
	},
	
	// Hide popup
	hide : function () {
		var par = this.parentNode.parentNode;
		
		// Add closed class back to popup container
		par.className += " closed";
		
		// Switch "hide" class back to "expand"
		var anchors = par.getElementsByTagName("a");
		for (var k = 0; k < anchors.length; k++) {
			if (anchors[k].className.indexOf("hide") != -1) {
				var oldClass = anchors[k].className;
				var newClass = oldClass.replace(/hide/g, "expand");
				anchors[k].className = newClass;
			}
		}
		
		return false;
	}
};

getElementsByClassNameForTag = function(className, tagName) {
    var children = document.body.getElementsByTagName(tagName);
    var elements = new Array();
    for(i = 0; i < children.length; i++)
    {
        var child = children[i];
        if (child.className.match(new RegExp("(^|\\s)" + className + "(\\s|$)")))
            elements.push(child);
    }
    return elements;
}

/* HOME PAGE PIC RANDOMIZER */
// The Central Randomizer 1.3 (C) 1997 by Paul Houle (houle@msc.cornell.edu)
// See:  http://www.msc.cornell.edu/~houle/javascript/randomizer.html

rnd.today=new Date();
rnd.seed=rnd.today.getTime();

function rnd() {
        rnd.seed = (rnd.seed*9301+49297) % 233280;
        return rnd.seed/(233280.0);
};

function rand(number) {
        return Math.ceil(rnd()*number);
};

