// 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
	ToggleRow.init();
});

/*----------------------------------+
 | 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;
}
/*----------------------------------*/

/*--------------------------------------------------+
 | Tog - Toggle visibility of two opposing elements |
 +--------------------------------------------------*/
var Tog = {
	swap : function (a, b) {
		var a = document.getElementById(a);
		var b = document.getElementById(b);
		
		if (!a || !b) return false;
		
		if (a.className.indexOf("closed") != -1) {
			var oldClass = a.className;
			var newClass = oldClass.replace(/closed/g, "");
			a.className = newClass;
			
			b.className += " closed";
		} else {
			var oldClass = b.className;
			var newClass = oldClass.replace(/closed/g, "");
			b.className = newClass;
			
			a.className += " closed";
		}
	},
	toggle : function (a) {
		var a = document.getElementById(a);
		
		if (!a) return false;
		
		if (a.className.indexOf("closed") != -1) {
			var oldClass = a.className;
			var newClass = oldClass.replace(/closed/g, "");
			a.className = newClass;
		} else {
			a.className += " closed";
		}
	},
	togglePropertyValue : function (a, prop, value1, value2) {
		var a = document.getElementById(a);
		
		if (!a) return false;
		
		if (a[prop].indexOf(value1) != -1) {
			a[prop] = value2;
		} else {
			a[prop] = value1;
		}
	}
};