// Javascript functionality for a menu system for the FTS website

/**
 * Shows a detailed tip for the currently selected menu.
 * Generally, this should be called onmouseover
 * @param string sTip The text of the tip to show.
 * @return void
 * @post The div with id tip is visibile and has the specified text.
 */
function showMenu(event) {
	// hack to get IE to also work:
	if( !event ) {
		event = window.event;
	}
	var sTip;
	if( event.target ) {
		sTip = event.target.title;
	}
	else if( event.srcElement ) {
		sTip = event.srcElement.title;
	}
	else {
		return;
	}
	var oTipBox = document.getElementById("tip");
	// ensure we actually got it...
	if( ! oTipBox ) { return; }
	oTipBox.innerHTML = sTip;
	oTipBox.style.display = "block";
}
/**
 * Hides the detailed tip when nothing is selected.
 * Generally, this should be called from onmouseout
 * @return void
 * @post The div with id tip is not shown and has no text contents.
 */
function hideMenu() {
	var oTipBox = document.getElementById("tip");
	if( ! oTipBox ) { return; }
	oTipBox.innerHTML = "";
	oTipBox.style.display = "none";
}

/**
 * Sets up the mouseover actions for the menu.
 * @return void
 * @post All menu elements have the appropriate events setup.
 */
function setupMenus() {
	var oMenuDiv = document.getElementById("menubar");
	var oMenuList = getFirst(oMenuDiv, "ul");
	if( ! oMenuList ) { return; }
	// now iterate over each child of oMenuList, which should be an li element, the first child of which is the anchor
	var oMenuListItem = getFirst(oMenuList, "li");
	while( oMenuListItem ) {
		var oMenuAnchor = oMenuListItem.firstChild;
		oMenuAnchor.onmouseover = showMenu;
		oMenuAnchor.onmouseout = hideMenu;
		oMenuListItem = getNext(oMenuListItem, "li");
	}
}
/**
 * Iterates through the direct children of oNode until one of type sTag is found.
 * @return Node The first child node of the tag type.
 * @param Node oNode The node to search
 * @param string sTag The tag to look for.
 */
function getFirst(oNode, sTag) {
	var oChild = oNode.firstChild;
	// test if tagName is defined, textNodes don't have this
	if( oChild.tagName ) {
		if( oChild.tagName.toUpperCase() == sTag.toUpperCase() ) {
			return oChild;
		}
	}
	return getNext(oChild, sTag);
}
/**
 * Iterates through the siblings of oNode until one of type sTag is found.
 * @return Node The first sibling of the tag type.
 * @param Node oNode The node who's siblings will be searched.
 * @param string sTag The tag to look for.
 */
function getNext(oNode, sTag) {
	var oSib = oNode.nextSibling;
	while( oSib ) {
		if( oSib.tagName ) {
			if( oSib.tagName.toUpperCase() == sTag.toUpperCase() ) {
				return oSib;
			}
		}
		oSib = oSib.nextSibling;
	}
	return false;
}
