var navMenu = {

   openMenus : [],
   pressedNav : [],
   timer : null,
   subMenus : ['','about','services','','','graphics','','','','','','','', 'research', '', '', 'surveys'],

   init : function() {

     if (!document.getElementById || !document.getElementsByTagName) { return; }
     var navHolder = document.getElementById('navbar');
     var navItems = navHolder.getElementsByTagName('a');
     var lists = navHolder.getElementsByTagName('ul');

     for (var i=0, allLists = lists.length; i<allLists; i++) {
         lists[i].style.visibility = 'hidden';
     }

     for (var i=0, allNavItems = navItems.length; i<allNavItems; i++) {
         navItems[i].number = i;
         this.addEvent(navItems[i],'mouseover', this.display);
         this.addEvent(navItems[i],'focus', this.display);
         this.addEvent(navItems[i],'mouseout', this.setTimer);
         this.addEvent(navItems[i],'blur', this.setTimer);
     }

   },

   display : function() {

     // a new link has been moused over, so erase the countdown
     if (navMenu.timer) { clearTimeout(navMenu.timer); }

     var menuLvl, menuToShow, num = this.number;

     // determining menu levels, from 1 (global) to 4 (deepest subnav)
     if (num <= 9) { menuLvl = 1; }
     if ((num > 9) && (num <= 15) || (num > 22)) { menuLvl = 2; }
     if ((num > 15) && (num <= 18)) { menuLvl = 3; }
     if ((num > 18) && (num <= 22)) { menuLvl = 4; }

     // stop if the user is mousing over the same item again
     if (navMenu.openMenus[menuLvl] && navMenu.openMenus[menuLvl] == navMenu.subMenus[num]) { return; }

     // shut off any other submenus
     if (navMenu.openMenus[menuLvl]) { navMenu.closeAllMenus(menuLvl); }

     // if there is no item in subMenus[] at that position, then do nothing
     // if there is an item in subMenus[] at that position, change the indicated sub menu's visibility
     if (!navMenu.subMenus[num]) {}
     else {
        menuToShow = document.getElementById(navMenu.subMenus[num]).style;
        menuToShow.visibility = 'visible';
     }

     // assign that open menu to the openMenus[] array, with the position
     // in the array the same as the menu level (1 or 2)
     navMenu.openMenus[menuLvl] = navMenu.subMenus[num];

     // alter visual display if there is no class assigned to that item
     // if there is already a class assigned, then we assume it is "over" and do nothing
     if (this.className) { return; }
     this.className = 'over';

     // replace whatever item is in the pressedNav[] array with the one just activated
     // remove the class from that previous item
     if (navMenu.pressedNav[menuLvl]) { navMenu.pressedNav[menuLvl].className = ''; }
     navMenu.pressedNav[menuLvl] = this;

   },

   // setting a 5 second timer
   setTimer : function() {
     if (navMenu.timer) { clearTimeout(navMenu.timer); }
     navMenu.timer = setTimeout('navMenu.closeAllMenus(1)',5000);
   },

   // shutting down all menus, wiping all "over" classes and emptying out all
   // the items in openMenus[] and pressedNav[]
   closeAllMenus : function(lvl) {

     for (var i=navMenu.openMenus.length - 1; i>=lvl; i--) {
        if (navMenu.openMenus[i]) {
            var menuToHide = document.getElementById(navMenu.openMenus[i]).style;
            menuToHide.visibility = 'hidden';
        }
        navMenu.openMenus[i] = null;
        if (navMenu.pressedNav[i]) {
            navMenu.pressedNav[i].className = '';
            navMenu.pressedNav[i] = null;
        }
     }
  },

  addEvent : function(obj, type, func) {
    if (obj.addEventListener) {obj.addEventListener(type, func, false);}
    else if (obj.attachEvent) {
      obj["e" + type + func] = func;
      obj[type + func] = function() {obj["e" + type + func] (window.event);}
      obj.attachEvent("on" + type, obj[type + func]);
    }
    else {obj["on" + type] = func;}
  }

}

navMenu.init();

