// Functions to create and work the sub section drop down menus for the
// L2_starter template, and the L3 stock template.
// Updated 2005-01-20

// The number of miliseconds before the sub nav is hidden
var menuTimeOut = 1000;

// Is true if the browser supports the standard DOM
var dom = document.getElementById;

// Will contain an array of sub menu IDs
var menu = new Array();

// Index value when the menu variable gets filled initially
var menuIndice = 0;

// Sentinal variable to determine if the mouse is over the sub menu
var onMenu = false;

// Holds a reference to a sub menu so the sub menu helper function that
// hides the sub menu can access that object.
var globalobj;

// Sentinal variable to determine whether the user agent accessing this
// page is using a computer monitor (screen). This is a crude method for
// determining the CSS media type and is only set to true when an alternate
// style sheet of a certain media is activated.
var mediaScreen = false;

function initializeMenu() {
  if (dom) {
    // Activate the style sheet to format the DHTML menu
    activateStyleSheet("DHTMLmenu");
    
    // Variables to hold references to a DOM node
    var LInode, DIVnode, ULnode, navRoot = document.getElementById("navRoot");
    // Loop variables
    var i=0,j=0,k=0;
    
    // Parse the DOM to add menu IDs to a global array
    for (i=0; i < navRoot.childNodes.length; i++) {
      LInode = navRoot.childNodes[i];
      if (LInode.nodeName == "LI" && LInode.className == "hasSubs") {
        correctIELayering(LInode, 900 - menuIndice);
        for (j=0; j < LInode.childNodes.length; j++) {
          DIVnode = LInode.childNodes[j];
          // The vertical menu, like that used in the L2_starter template
          // gets caught right here
          if (DIVnode.nodeName == "UL" && DIVnode.className == "subs") {
            menu[menuIndice] = DIVnode.getAttribute("id");
            menuIndice++;
          }
          // The horizontal menu, like that in template L3 has an extra DIV
          // tag used to fix a positioning bug in Safari 1.0.x that encases
          // the UL tag that contains the sub menu.
          else if (DIVnode.nodeName == "DIV" && DIVnode.className == "fixSafari") {
            correctIELayering(DIVnode, 900 - menuIndice);
            for (k=0; k < DIVnode.childNodes.length; k++) {
              ULnode = DIVnode.childNodes[k];
              if (ULnode.nodeName == "UL" && ULnode.className == "subs") {
                menu[menuIndice] = ULnode.getAttribute("id");
                menuIndice++;
              }
            }
          }
        }
      }
    }
  }
}

function showMenu(obj) {
  if (dom) {
    var topLevelSection;
    onMenu = true;
    
    // Hide all sub menus, regardless of which one has been moused over
    for (var i = 0; i < menu.length; i++) {
      document.getElementById(menu[i]).style.visibility = "hidden";
    }
    
    // Make the requested sub menu visible
    document.getElementById(obj).style.visibility = "visible";
    topLevelSection = document.getElementById(obj);
    topLevelSection = topLevelSection.parentNode;
    
    // Assign a class name to a top level section name (encased in a <span>
    // tag) so that the SPAN tag can be styled when the user mouses over a
    // sub menu item.
    for (var i = 0; i < topLevelSection.childNodes.length; i++) {
      if (topLevelSection.childNodes[i].nodeName == "SPAN") {
        topLevelSection.childNodes[i].className = "topLevelSection";
      }
    }
    onMenu = true;
  }
}

function hideMenu(obj) {
  globalobj = obj
  if (dom) {
    var topLevelSection;
    onMenu = false;
    
    // Wait before actually hiding the menu, in case the user moves over
    // another sub menu item.
    setTimeout("hideMenuHelper()",menuTimeOut);
    
    topLevelSection = document.getElementById(obj);
    topLevelSection = topLevelSection.parentNode;
    for (var i = 0; i < topLevelSection.childNodes.length; i++) {
      if (topLevelSection.childNodes[i].nodeName == "A" || topLevelSection.childNodes[i].nodeName == "SPAN") {
        topLevelSection.childNodes[i].className = "";
      }
    }
  }
}

function hideMenuHelper() {
  if (dom && onMenu == false && mediaScreen) {
    var obj = globalobj;
    document.getElementById(obj).style.visibility = "hidden";
  }
}

function keepMenu(obj) {
  onMenu = true;
  if (dom) {
  var topLevelSection = document.getElementById(obj);
    topLevelSection = topLevelSection.parentNode;
    for (var i = 0; i < topLevelSection.childNodes.length; i++) {
      if (topLevelSection.childNodes[i].nodeName == "A" || topLevelSection.childNodes[i].nodeName == "SPAN") {
        topLevelSection.childNodes[i].className = "topLevelSection";
      }
    }
  }
}

// Activate Alternate Style Sheet
// cssTitle: The value of the title attribute for the alternate style sheet
// you want to enable
function activateStyleSheet(cssTitle) {
  if (dom) {
    var linkTags = document.getElementsByTagName("link");
    for (var i = 0; i < linkTags.length; i++) {
      if (linkTags[i].getAttribute("title") == cssTitle) {
        linkTags[i].disabled = true; // Required by Internet Explorer
        linkTags[i].disabled = false;
        mediaScreen = true;
      }
    }
  }
}

// Internet Explorer doesn't layer relatively and absolutely positioned
// elements correctly. The top level section further down the menu
// (relatively positioned) would appear above a list of sub sections
// (absolutely positioned and given a higher z-index).
//
// ARGUMENTS:
// node   - A reference to the DOM node that needs the z-index adjusted
// zindex - The z-index to be given to node
function correctIELayering(node, zindex) {
  if (dom && document.all)
    node.style.zIndex = zindex;
}

// L6 - Submenu Nav Bar
// If submenu navigation <ul> is empty of elements (nodeType = 1)
// don't display an empty submenu - remove the container <div>
// by setting it's "display" style to "none".
//
// Look for "elements" specifically (nodeType = 1), because there 
// could be HTML comments (nodeType = 8) in the <ul>, but it would
// still render as nothing in the browser.
function toggleSubMenuNavBarDisplay(searchElement, hideElement, disableDropdowns) {

  if((searchElement == null || hideElement == null) && ! disableDropdowns) {
    initializeMenu();
    return;
  }

  var ul = document.getElementById(searchElement);
  var flag = false;

  if (ul == null) {
    flag = true;
  } else {
    if (ul.hasChildNodes()) {
      flag = true;
      for (i=0; i<ul.childNodes.length; i++) {
        if (ul.childNodes[i].nodeType == "1") {
          flag = false;
          break;
        }
      }
    } else {
      flag = true;
    }
  }
  if (flag) {
    document.getElementById(hideElement).style.display = "none";
  } else {
    if (searchElement == "navRoot" && ! disableDropdowns) initializeMenu();
  }
}

