// topnav.js for eCommunities
// functionality for dhtml menu on topnav
// code by Chris Nott (cnott@NOSPAM.blastradius.com)

var currentMenuButton = null;
var currentMenu = null;
var currentMenuItem = null;
var itemTimeout = null;
var menuItemTimeout = null;
var timer = null;

var itemDivId = {
   'home' : 'tnNavHome',
   'hall' : 'tnNavHall',
   'residents' : 'tnNavResidents',
   'business' : 'tnNavBusiness',
   'discover' : 'tnNavDiscover',
   'online' : 'tnNavOnline',
   'help' : 'tnNavHelp'
   };
var menuId = {
   'home' : 'tnNavHomeMenu',
   'hall' : 'tnNavHallMenu',
   'residents' : 'tnNavResidentsMenu',
   'business' : 'tnNavBusinessMenu',
   'discover' : 'tnNavDiscoverMenu',
   'online' : 'tnNavOnlineMenu',
   'help' : 'tnNavHelpMenu'
   };
var menuLeft = {
   'home' : 205,
   'hall' : 257,
   'residents' : 320,
   'business' : 387,
   'discover' : 448,
   'online' : 577,
   'help' : 678
   };

function topnavItemOn(section) {
   if (currentMenu != document.getElementById(menuId[section])) {
      
      // turn off current menu
      topnavCurrentItemOff();
      
      // if button is already on, it's the default - set flag to remember not to turn off
      currentMenuButton = document.getElementById(itemDivId[section]).firstChild.firstChild;
      if (currentMenuButton.src.indexOf('_on.') != -1) {
         currentMenuButton.isDefault = true;
      }
      
      // change image of new menu button
      currentMenuButton.src = currentMenuButton.src.replace(/_off/, '_on');
      
      // position menu horizontally and adjust if it's too wide
      currentMenu = document.getElementById(menuId[section]);
      if (currentMenu.offsetWidth < 780 && menuLeft[section] + currentMenu.offsetWidth > 780) {
         menuLeft[section] = 780 - currentMenu.offsetWidth;
      }
      currentMenu.style.left = menuLeft[section] + 'px';
      
      // show new menu (set top position to in-screen value)
      currentMenu.style.visibility = 'visible';
      
      // add click-to-hide-menu handler to document
      document.onmouseup = topnavCurrentItemOff;
   }
}

function topnavItemOff(section) {
	timer = setTimeout('topnavCurrentItemOff()', 1500);
}

function killDelay() {
  clearTimeout(timer);
}

function topnavCurrentItemOff() {
   if (currentMenuButton != null) {
      
      // change image of current menu button (check default flag)
      if (currentMenuButton.isDefault != true) {
         currentMenuButton.src = currentMenuButton.src.replace(/_on/, '_off');
      }
      currentMenuButton = null;
      
      // hide current menu
      currentMenu.style.visibility = 'hidden';
      currentMenu = null;
      
      // remove click-to-hide-menu handler
      document.onmouseup = function() {}
   }
}
