/*
 * Dynamic Menu
 * Written by Andy Peatling - http://www.cssdev.com/
 * April 1, 2006.
 */
//addLoadEvent(collapseMenu);
//addLoadEvent(prepareMenu);

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
function openMainMenuPath(path) {
	var liIdPrefix="m";
	var ulIdPrefix="s";

	var pathArray = path.split(",");
	for (var item in pathArray)
	{
		pathArray[item]="'"+liIdPrefix+pathArray[item]+"'";
	}

	for(var i=pathArray.length-2;i<pathArray.length;i++) {
		var selectedMenu = eval("document.getElementById("+pathArray[i]+")");
		if(selectedMenu) {
			selectedMenu.className = "menuSelected";
		}
	}
}
function openMenuPath(node,path) {
	if(!path) {
		return false;
	} else {
		var liIdPrefix="m";
		var ulIdPrefix="s";
		var pathArray = path.split(",");
		for (var item in pathArray)
		{
			pathArray[item]="'"+ulIdPrefix+pathArray[item]+"'";
		}
	}
	if (!document.getElementById) return false;
	if (!document.getElementById("subMenu")) return false;
	if (!node) node = document.getElementById("subMenu");
	if (node.childNodes.length > 0) {
		for (var i=0; i<node.childNodes.length; i++) {
			var child = node.childNodes[i];
			for(var a=0;a<pathArray.length-2;a++) {
				var x = eval("document.getElementById("+pathArray[a]+")");
				if(x) {
					if (child.id == x.id ) {
						if(child.nodeName == "UL") {
							child.style.display = "inline";
						}
						if(child.nodeName == "LI") {
							var oldClasses = String(child.className);
							oldClasses = oldClasses.replace(/menuNotSelected|menuSelected/g,"");
							child.className = "menuSelected "+oldClasses;
						}
					}
				}
			}
			openMenuPath(child,path);
		}
	}
}
function collapseMenu(node) {
	if (!document.getElementById) return false;
	if (!document.getElementById("subMenu")) return false;
	if (!node) node = document.getElementById("subMenu");
	if (node.childNodes.length > 0) {
		for (var i=0; i<node.childNodes.length; i++) {
			var child = node.childNodes[i];
			if (child.nodeName == "UL" && child.style.display != "inline") {
					child.style.display = "none";
			}
			collapseMenu(child);
		}
	}
}
function prepareMenu() {
	if (!document.getElementById || !document.getElementsByTagName) return false;
	if (!document.getElementById("subMenu")) return false;
	var links = document.getElementById("subMenu").getElementsByTagName("a");
	for (var i=0; i<links.length; i++) {
		links[i].onclick = function() {
			toggleMenu(this.parentNode.getElementsByTagName("UL")[0], this.href);
			//return false;
		}
	}
}
function toggleMenu(node, link) {
	if (!document.getElementById) return false;
	if (!link) return false;
	if (!node) location.href = link.href;
	// Collapse all nodes, and only show clicked node (when clicking top level of menu)
	if (node.parentNode.parentNode.id == "subMenu") {
		hideTopLevels();
	}
	if (node.style.display == "") {
		Effect.Fade(node, {duration: 0.3});
	} else {
		Effect.Appear(node, {duration: 0.3});
	}
}
function hideTopLevels() {
	if (!document.getElementById) return false;
	if (!(node = document.getElementById("subMenu"))) return false;	
	if (node.childNodes.length > 0) {
		for (var i=0; i<node.childNodes.length; i++) {
			var child = node.childNodes[i];
			for(var j=0; j<child.childNodes.length; j++) {
				var grandchild = child.childNodes[j];
				if (grandchild.nodeName == "UL") {
					if (grandchild.style.display == '') {
						Effect.Fade(grandchild, {duration: 0.2});
					}
				}
			}
		}
	}
}
