// JavaScript Document

function Menu() {
	this.menuContainer = document.getElementById("menu1");	
	
	if (document.getElementById('trace')) {document.getElementById('trace').value = ""};
}


function trace(str) {
	var debug = false;
	
	if (debug) {
		console.log(str);
	}
}

Menu.prototype.show = function(name, name2) {
	// pass id of the menu element to show
	
	if (name && (name =="[nothing]")) {
		return 0;	
	}
	

	
	var targetNode = document.getElementById(name) || document.getElementById(name2);
	trace(targetNode);

	if (!targetNode) {
		// so we can't find a node based on what was passed in, try to find one based on the current url instead...	
		var url = window.location.pathname;
		trace(url);
		var parent = document.getElementById("menuContainer");
		trace(parent);
		var linkElement = findLink(parent, url);
		trace("link Element: " + linkElement);
		
		if (linkElement) {
			targetNode = linkElement.parentNode;
		}
	}
	
	trace(targetNode);

	if (!targetNode) {return 0};
	
	// if we get here targetNode is an LI element making up part of a menu at an unknown level.
	// unless you clicked on a heading, in which case the target might be something else, like a ul tag...


	targetNode.style.display="block";	// make sure our target is visible.
	if (targetNode.nodeName == "LI") {
		targetNode.parentNode.style.display="block";
	}
	
	
	if (targetNode.nodeName == "H2") {		// if our target happens to be a heading, make it white.
		targetNode.style.display="block";
		targetNode.style.backgroundColor="#FFFFFF";
	} else {
		
		// so it's not a heading, so work backwards until we find one...
		var ourUl = targetNode;
		while (  (ourUl.nodeName != "UL") && (ourUl.parentNode.id != "menuContainer")  ) {
			ourUl = ourUl.parentNode
		}
		
		// look for a previous H2 tag
		for (child = ourUl; child != null; child = child.previousSibling) {
			trace(child.nodeName);
			if (child.nodeName == "H2") {
				child.style.backgroundColor="#FFFFFF";
				break;
			}
		}
		
	}
	
	
	
	
	// does our target node have a submenu? i.e. a new ul tag
	
	
//	for (child = targetNode.firstChild; child != null; child = child.nextSibling) {
//		if (child.nodeName == "UL") {
//			child.style.display = "block";
//		}
//	}

	while (targetNode.parentNode.id != "menuContainer") {
		targetNode.style.display="block";
		if (targetNode.nodeName == "LI") {
			makeLinksInLiWhite(targetNode);
		}
		targetNode = targetNode.parentNode;
	}


}

function makeLinksInLiWhite(li) {
	// make all the links that might be in this li element turn white
	var child = li.firstChild;
	while (child) {
		if (child.nodeName == "A") {
			child.style.fontWeight="bold";
			child.style.backgroundColor="#FFFFFF";
		}
		child = child.nextSibling;
	}
}




function findLink(node, linkPath) {
	
	linkPath = linkPath.replace(/^\//, "");
	
	for (var childIndex=0; childIndex<node.childNodes.length; childIndex++) {
			var child = node.childNodes[childIndex];
			//trace(child);
			var foundChild = null;
			
			if (child.nodeType ==1) {
				// we've got an object... maybe it's got more children...
				if (child.nodeName == "A") {	// maybe this is the link we are looking for...
					//trace(child.pathname);
					// good old IE doesn't include the leading / but FF does.
					var childPath = child.pathname.replace(/^\//, "");
					
					//trace("checking: " + childPath + " : " + linkPath);
					if (childPath == linkPath) {
						//trace("found it! returning " + child);
						foundChild = child;
					}

				} else {
					foundChild = findLink(child, linkPath);
				}
				
			} else {
				//trace(child.nodeValue);
			
			}
			
			if (foundChild) {
				return foundChild;
			}
			
	}
	
	return null;
	
}

