
// MENU FUNCTIONALITY

function fShowMenu(sSubMenu, iSubMenuLevel, oMenuLink) {

		//debug("<br>* fShowMenu('" + sSubMenu + "'," + iSubMenuLevel + ",obj)")
		// cancel all timed menu closes
		fCancelTimeout();
		
		// close all sub menus at this level and below
		fCloseMenus(iSubMenuLevel)
		// check whether any sub menu needs opening
		if (sSubMenu.length > 0) {
		    oSubMenu = document.getElementById(sSubMenu);
			
			// increment actual level
			actualLevel++;
			
			// calculate width required for wrapping
			oSubMenu.style.display = "block"; // next line requires block display to calculate width
			var iSpaceNeeded = (((fMenuGetDistanceFrom(oMenuLink, "Left")) + oMenuLink.offsetParent.offsetWidth + submenuOffsetH) + oSubMenu.offsetWidth)
			oSubMenu.style.display = "none"; // set back to none to prevent flicker in firefox
			
			// highlight parent link
			oMenuLink.className = "a_MenuOpen";
			
			//do positioning
			if (iSubMenuLevel == 1) {
			    oSubMenu.style.left = (oMenuLink.offsetLeft + menuOffsetH) + "px";
			    oSubMenu.style.top = (oMenuLink.offsetParent.offsetHeight + menuOffsetV) + "px";
			}
			else {
				// wrap menu back towards the right if it goes off the edge of the screen
				if (iSpaceNeeded < document.body.offsetWidth) {
				    oSubMenu.style.left = (oMenuLink.offsetParent.offsetWidth + submenuOffsetH) + "px";
				}
				else {
				    oSubMenu.style.left = (oMenuLink.offsetParent.offsetWidth - submenuOffsetH) + "px";
				};
				oSubMenu.style.top = (oMenuLink.offsetTop + submenuOffsetV) + "px";
			}
			
			// set the correct style
			if (oSubMenu.getAttribute("fixedstyle") != "true") {
				if (arMenuStyle[actualLevel] == "") {
					oSubMenu.className = submenuDefaultStyle;
				}
				else {
					oSubMenu.className = arMenuStyle[iSubMenuLevel];
				};
			};
			
			// show the menu
            oSubMenu.style.display = "block";

			// add menu details to array
			fAddMenuDetails(sSubMenu, iSubMenuLevel, oMenuLink.id)
			
			// iframe control
			oIframe = document.getElementById("ifr_" + actualLevel);
			if (!oIframe) {
				// iframe does not exist for this menu, so build a new one
				oIfrCont = document.getElementById('div_Iframes')
				oIfrCont.innerHTML += '<iframe id="ifr_' + actualLevel + '" src="' + iframeBlankURL + '" frameBorder="0" scrolling="no"></iframe>'
				oIframe = document.getElementById("ifr_" + actualLevel);
			}
			if (oIframe) {
				oIframe.style.display = "none";
				oIframe.style.top = (fMenuGetDistanceFrom(oSubMenu, "Top")) + "px";
				oIframe.style.left = (fMenuGetDistanceFrom(oSubMenu, "Left")) + "px";
				oIframe.style.width = oSubMenu.offsetWidth + "px";
				oIframe.style.height = oSubMenu.offsetHeight + "px";
				oIframe.style.display = "block";
			}
		}		
	}
	
	function fAddMenuDetails(menuid, level, parentid) {
		arOpenMenuID[arrayTotal + 1] = menuid;
		arOpenMenuLevel[arrayTotal + 1] = level;
		arOpenMenuParentID[arrayTotal + 1] = parentid;
		arrayTotal++;
	}
	function fRemoveMenuDetails(array) {
		arOpenMenuID[array] = "";
		arOpenMenuLevel[array] = "";
		arOpenMenuParentID[array] = "";
		arrayTotal--;
	}
	function fResetArray() {
		arrayTotal = -1;
		currentLevel = 1;
	}
	function fCloseMenus(level) {
		//debug("fCloseMenus(" + level + ")")
		for (i=arrayTotal; i>=0; i--) {
			if (arOpenMenuLevel[i] >= level) {
				fDisableMenu(arOpenMenuID[i], arOpenMenuParentID[i], i);
			};
		};
		if (level == 1) {
			fResetArray();
		}
	}

	function fHideMenu() {
		//debug("<br># fHideMenu>" + arguments.callee.caller.toString())
		fCancelTimeout(lastTimeout);
		lastTimeout = window.setTimeout("fCloseMenus(1);", menuCloseDelay);
		//debug("setTimeout=" + lastTimeout)
	}
	function fDisableMenu(sSubMenu, sParent, iArrayID) {
		document.getElementById(sSubMenu).style.display = "none";
		fRemoveMenuDetails(iArrayID);
		if (sParent != "") {
			var oLastMenuLink = document.getElementById(sParent)
			oLastMenuLink.className = "a_Parent";
		};
		oIframe = document.getElementById("ifr_" + actualLevel);
		if (oIframe) {
			oIframe.style.display = "none";
		};
		actualLevel--;
	}
	function fCancelTimeout() {
		//debug("fCancelTimeout(" + lastTimeout + ")")
		window.clearTimeout(lastTimeout);
	}
	function fMenuGetDistanceFrom(obj, where) {
		//gets the distance of an object from the top/left of the page
	    var count = 0;
	    //loop until you get to the menu item, which will always have an id starting with 'div_Menu'
	    while (obj && obj.id.indexOf("div_Menu") != 0) {
			switch (where) {
				case "Top":
					count = count + obj.offsetTop;
					break
				case "Right":
					count = count + obj.offsetRight;
					break
				case "Bottom":
					count = count + obj.offsetBottom;
					break
				case "Left":
					count = count + obj.offsetLeft;
					break
				default:
					count = 0;
					break
}
			obj = obj.offsetParent;
		}
		return count;
	}
	
	
	
// DEBUGGING TOOLS //

	function fShowOpenMenus() {
		var poo = "";
		for (i=0; i<=arrayTotal; i++) {
			var poo = poo + arOpenMenuID[i] + "/" + arOpenMenuLevel[i] + "/" + arOpenMenuParentID[i] + "<br>";
		}
		fDebug(poo);
	}
	
	function fDebug(stuff) {
		document.getElementById('div_Status').innerHTML += stuff;
	}

	function debug(msg) {
		document.getElementById("div_SearchResults").innerHTML += msg + " ";
	
	}
	// JAVSCRIPT That adds mousover events to menu to reduce HTML in code
	/*startList = function() {
	    navRoot = document.getElementById("div_CategoriesMenu");
	    for (i = 0; i < navRoot.childNodes.length; i++) {
	        divNode = navRoot.childNodes[i];
	        if (divNode.nodeName == "UL") {
	            divNode.onmouseover = function() { fCancelTimeout(); }
	            divNode.onmouseout = function() { fHideMenu(); }
	            if (divNode.className == '') { divNode.className = submenuDefaultStyle; }

	            var spanNodeId = 0;
	            if (divNode.childNodes[0].nodeType != 1) { spanNodeId = 1 };

	            for (j = 0; j < divNode.childNodes[spanNodeId].childNodes.length; j++) {
	                aNode = divNode.childNodes[spanNodeId].childNodes[j];

	                if (aNode.nodeName == "A") {
	                    if (!aNode.id || aNode.id == '') {
	                        aNode.setAttribute("id", "A" + i + j);
	                    }
	                    if (aNode.className != 'a_Parent') {
	                        aNode.onmouseover = function() {
	                            level = this.className;
	                            fShowMenu('', level, this);
	                        }
	                    }
	                }
	            }
	        }
	    }
	}

	function addLoadEvent(func) {
	    var oldonload = window.onload;
	    if (typeof window.onload != 'function') {
	        window.onload = func;
	    } else {
	        window.onload = function() {
	            if (oldonload) {
	                oldonload();
	            }
	            func();
	        }
	    }
	}
	addLoadEvent(startList); */
    
