if (document.getElementById && document.getElementsByTagName) {

var stt = {

  navHolder : document.getElementById('nav'),
  bdy : document.getElementsByTagName('body')[0],
  navItems : null,
  lists : null,
  allNavItems : 0,
  allLists : 0,

  // array for menu levels currently displayed
  openMenus : [],  

  // array tracking which item is pressed at each level
  pressedNav : [],

  // timer for when to close menus
  timer : null,

  // this array will associate anchor sequence with id values of submenus
  subMenus : [],

  init : function() {
 
    if (!this.navHolder) { return; }
 
    this.navItems = this.navHolder.getElementsByTagName('a');
 
    this.lists = this.navHolder.getElementsByTagName('ul');
 
    this.allNavItems = this.navItems.length;
    this.allLists = this.lists.length;
 
    this.util.configEvents();

    // associate anchors with submenus
    this.buildMenuAssociations();

    // disabling the visibility of the sub menus
    for (var i=1; i<this.allLists; i++) {
         this.lists[i].style.visibility = 'hidden';
    }

    // adding a number property to each nav link, to synchronize
    // them with the subMenus[] array
    for (i=0; i<this.allNavItems; i++) {
         this.navItems[i].number = i;
    }
    
    // assign events to outer element holding nav
    this.util.addEvent(this.navHolder, 'mouseover', this.displaySubMenu, false);
    this.util.addEvent(this.navHolder, 'focus', this.displaySubMenu, true);
    this.util.addEvent(this.navHolder, 'focusin', this.displaySubMenu, false);
    this.util.addEvent(this.navHolder, 'mouseout', this.setTimer, false);
    this.util.addEvent(this.navHolder, 'blur', this.setTimer, true);
    this.util.addEvent(this.navHolder, 'focusout', this.setTimer, false);    
    this.util.addEvent(this.navHolder, 'click', this.checkExternal, false);
    this.util.addEvent(window, 'load', this.removeSoldOutOptions, false);
    this.util.addEvent(window, 'load', this.showButtonsBarberNovember, false);    

  },
  
  showButtonsBarberNovember : function() {
  
    var centerMainDiv = document.getElementById('center-main');
    
    if (centerMainDiv) {
    
      var theH1 = centerMainDiv.getElementsByTagName('h1')[0];
      
      if (theH1) {
      
        var h1txt = theH1.firstChild.nodeValue;
      
        if ((h1txt.match('11/26/11') || h1txt.match('11/27/11')) && h1txt.match('Barber Motorsports')) {
          stt.bdy.id=''; 
        }      
      
      }
    
    }
    
  
  },
  
  removeSoldOutOptions : function() {
  
     var soldOutCell = document.getElementById('soldOutTxt');
     var optionsTable = document.getElementById('jwProdTable');
     var topHeading = document.getElementsByTagName('h1')[0];
          
     if (soldOutCell && optionsTable && topHeading) {
     
       var soldOutNames = soldOutCell.firstChild.nodeValue;
       var optionsMenu = optionsTable.getElementsByTagName('select')[0];
       var hdrTxt = topHeading.firstChild.nodeValue;
       
       if (!optionsMenu || !soldOutNames || !hdrTxt) { return; }
       
         if (/Pro School/.test(hdrTxt)) {
 
           if (/Pro/.test(soldOutNames)) { optionsMenu.removeChild(optionsMenu.options[2]);}       
           if (/Licensed/.test(soldOutNames)) { optionsMenu.removeChild(optionsMenu.options[1]);}       
           if (/Intermediate/.test(soldOutNames)) { optionsMenu.removeChild(optionsMenu.options[0]);}
 
         }

         if (/LED Ladies/.test(hdrTxt)) {

           if (/SuperSport/.test(soldOutNames)) { optionsMenu.removeChild(optionsMenu.options[1]);}     
           if (/\bSport/.test(soldOutNames)) { optionsMenu.removeChild(optionsMenu.options[0]);}       

         }
       
         else {

           if (/Pro School/.test(soldOutNames)) { optionsMenu.removeChild(optionsMenu.options[3]);}       
           if (/Advanced/.test(soldOutNames)) { optionsMenu.removeChild(optionsMenu.options[2]);}       
           if (/Intermediate/.test(soldOutNames)) { optionsMenu.removeChild(optionsMenu.options[1]);}       
           if (/Novice/.test(soldOutNames)) { optionsMenu.removeChild(optionsMenu.options[0]);}
       
         }
       
     }
  
  },
  
  // open nav links to other websites in a new window
  checkExternal : function(evt) {
  
    var anchor = stt.util.findTarget(evt, 'a', this);
    if (!anchor || anchor.rel !== 'external') { return; }
    stt.util.stopDefault(evt);
    window.open(anchor.href, '_blank', 'top=5,left=5,width=700,height=500,scrollbars=1,resizable=1,location=1,menubar=1,status=1,toolbar=1');
    
  
  },

  // associate anchor sequence with id values of submenus
  buildMenuAssociations : function() {
  
    this.subMenus[6] = 'programsMenu';
    this.subMenus[7] = 'aboutMenu';  
  
  },

  displaySubMenu : function(evt) {

    // pinpoint the link chosen
    var linkChosen = stt.util.findTarget(evt, 'a', this);

    // if no link was found, stop processing
    if (!linkChosen) { return; }
    
    if (linkChosen.rel === 'external') {linkChosen.title = 'This link to an external website opens in a new window';}

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

    var menuLvl, menuToShow;
    var num = linkChosen.number;

    menuLvl = (num <= 8) ? 1 : 2;

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

    // shut off any other submenus
    if (stt.openMenus[menuLvl]) { stt.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 (stt.subMenus[num]) {
      menuToShow = document.getElementById(stt.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)
    stt.openMenus[menuLvl] = stt.subMenus[num];

    // alter visual display if there is no class assigned to that anchor
    // if there is already a class assigned, then we assume it is "over" and do nothing
    linkChosen.className += ' over';

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

  },

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

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

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

   configEvents : function() {
    
    if (document.addEventListener) {
    
        this.addEvent = function(el, type, func, capture) {
          el.addEventListener(type, func, capture);  
        };
 
        this.stopBubble = function(evt) { evt.stopPropagation(); };

        this.stopDefault = function(evt) { evt.preventDefault(); };

        this.findTarget = function(evt, targetNode, container) {
          var currentNode = evt.target;
          while (currentNode && currentNode !== container) {
            if (currentNode.nodeName.toLowerCase() === targetNode) {
                return currentNode; break;
            }
            else { currentNode = currentNode.parentNode; }
          };
          return false;
        };
    }
    
    else if (document.attachEvent) {
    
        this.addEvent = function(el, type, func) {
          el["e" + type + func] = func;
          el[type + func] = function() { el["e" + type + func](window.event); };
          el.attachEvent("on" + type, el[type + func]);
        };

        this.stopBubble = function(evt) { evt.cancelBubble = true; };

        this.stopDefault = function(evt) { evt.returnValue = false; };

        this.findTarget = function(evt, targetNode, container) {
          var currentNode = evt.srcElement;
          while (currentNode && currentNode !== container) {
            if (currentNode.nodeName.toLowerCase() === targetNode) {
                return currentNode; break;
            }
            else { currentNode = currentNode.parentNode; }
          };
          return false;
        };
    }
   }
  
  }
  
};

stt.init();

}
