/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Travis Beckham :: http://www.squidfingers.com | http://www.podlob.com
version date: 06/02/03 :: If want to use this code, feel free to do so,
but please leave this message intact. (Travis Beckham) */

// Node Functions

if(!window.Node){
  var Node = {ELEMENT_NODE : 1, TEXT_NODE : 3};
}

function checkNode(node, filter){
  return (filter == null || node.nodeType == Node[filter] || node.nodeName.toUpperCase() == filter.toUpperCase());
}

function getChildren(node, filter){
  var result = new Array();
  var children;
  if (node && node.childNodes)
    children = node.childNodes;

  for(var i = 0; children && i < children.length; i++){
    if(checkNode(children[i], filter)) result[result.length] = children[i];
  }
  return result;
}

function getChildrenByElement(node){
  return getChildren(node, "ELEMENT_NODE");
}

function getFirstChild(node, filter){
  var child;
  var children;
  if (node.childNodes)
    children = node.childNodes;

  for(var i = 0; children && i < children.length; i++){
    child = children[i];
    if(checkNode(child, filter)) return child;
  }
  return null;
}

function getFirstChildByText(node){
  return getFirstChild(node, "TEXT_NODE");
}

function getNextSibling(node, filter){
  for(var sibling = node.nextSibling; sibling != null; sibling = sibling.nextSibling){
    if(checkNode(sibling, filter)) return sibling;
  }
  return null;
}
function getNextSiblingByElement(node){
        return getNextSibling(node, "ELEMENT_NODE");
}

// Menu Functions & Properties

var activeMenu = null;
var activeSubMenu = null;

function showMenu() {
  if(activeMenu){
    activeMenu.className = "";
    getNextSiblingByElement(activeMenu).style.display = "none";
  }
  if(this == activeMenu){
    activeMenu = null;
  } else {
    // hide the active menu item if another main item is selected
    if (activeSubMenu && (this != activeSubMenu.parentNode.parentNode.firstChild))
    {
      activeSubMenu.parentNode.style.display = "none";
      activeSubMenu.parentNode.parentNode.firstChild.className = "";
    }

    this.className = "active";
    getNextSiblingByElement(this).style.display = "block";
    activeMenu = this;
  }
  return false;
}

function initMenu(){
  var menus, menu, subMenuUl, noSubMenus = false, text, a, i;
  menus = getChildrenByElement(document.getElementById("menu"));
  
  // Get the active sub menu for menus that have sub menus
  activeSubMenu = getActiveSubMenu (menus, true);

  // For IE - have to process menu items w/o sub menus differently
  if (!activeSubMenu)
  {
    subMenuUl = getChildrenByElement(document.getElementById("subMenuUl"));
    activeSubMenu = findSelectedSubMenu(subMenuUl, this.location.href);
    noSubMenus = true;  // for later
    if (activeSubMenu && activeSubMenu.firstChild)  // only set this style active for menus w/o submenus
    {
      activeSubMenu.firstChild.className = "active";
    }
  }
  
  // hide all the sub menus
  for(i = 0; i < menus.length; i++){
    menu = menus[i];
    text = getFirstChildByText(menu);
    a = document.createElement("a");
    menu.replaceChild(a, text);
    a.appendChild(text);
    a.href = "#";
    a.onclick = showMenu;
    a.onfocus = function(){this.blur()};

    if (((!activeSubMenu || activeSubMenu.parentNode.parentNode.firstChild != a) 
        && menu.tagName != "UL") || (noSubMenus && menu.tagName != "UL"))  // For Firefox vs. IE
      getNextSiblingByElement(a).style.display = "none";
  }

  // only do this when main menu with submenus is present
  if (!noSubMenus)
  {
      displayActiveSubMenu(activeSubMenu);
  }


}

function getActiveSubMenu(mainMenuItems, hasSubMenus)
{
  var retVal, url, currentMainMenuItem, subMenuItems, currentSubMenuItem;
  url = this.location.href;
  for(i = 0; i < mainMenuItems.length; i++)
  {
    currentMainMenuItem = mainMenuItems[i];
    if (hasSubMenus)
    {
      subMenuItems = getChildrenByElement(getFirstChild(currentMainMenuItem, "ELEMENT_NODE"));
    }
    // handle the bottom section of the menus
    else
    {
      subMenuItems = getChildrenByElement(currentMainMenuItem);
    }

    // see if sub menu is found, and return if so (helps with performance)
    retVal = findSelectedSubMenu(subMenuItems, url);
    if (retVal)
    {
      return retVal;
    }
  }
  
  return retVal;  
}

function findSelectedSubMenu(subMenuItems, url)
{
   var currentSubMenuItem, subMenuUrl;
   for(j = 0; j < subMenuItems.length; j++)
    {
      currentSubMenuItem = subMenuItems[j];
      
      if (currentSubMenuItem && currentSubMenuItem.firstChild.href) 
        subMenuUrl = currentSubMenuItem.firstChild.href;

      if (url == subMenuUrl)
      {
        return currentSubMenuItem;
      }
    }
}

function displayActiveSubMenu(activeSubMenu)
{
  if (activeSubMenu && activeSubMenu.firstChild)
  {
    activeSubMenu.firstChild.className = "active";
    activeSubMenu.parentNode.parentNode.firstChild.className = "active";
  }
}

function getHtmlPageName (urlString)
{
  var indexOfLastSlash, htmlPageName;
  indexOfLastSlash = urlString.lastIndexOf('/');
  htmlPageName = urlString.substring(indexOfLastSlash);
  return urlString;
}

if(document.createElement) window.onload = initMenu;
