//----------------------------------------------------------------------------
// Code for handling the menu bar and active button.
//----------------------------------------------------------------------------


// -----  declare globals here ------
var sbActiveButton = null;
var sbActiveButtonParentColor = null;
var sbActiveButtonParentBgrd = null;


// ---------------------------  begin function declarations -----------------------------------------------


function retrieveComputedStyle(element, styleProperty) {
    //  2009-02-06 S. Davies: added this 
    var computedStyle = null;

    if  ( typeof element.currentStyle != "undefined" )
    {
        //  IE
        computedStyle = element.currentStyle;
    }
    else
    {
        // W3C
       computedStyle =  document.defaultView.getComputedStyle(element, null);
    }
    
    return computedStyle[styleProperty]
}


// ----------------------------------------------------------------------------

function sbButtonClick(event, menuId) {
      //  2009-02-06 S. Davies: modified this  to call retrieveComputedStyle()
      var button = null;
      var elMC = null;

      var computedStyle = null;
	  var targetElement = null;

      var sbActiveMenuColor = null;
      var sbActiveMenuBgrd = null;	  

      // a NYAHSA mod: capture reference to the 'Main Content' div, or an alternate, to conditionally alter its overflow property
      elMC = document.getElementById("mainContent");
      if (elMC == null) elMC = document.getElementById("mainContentRight");


      // Get the target button element.
      if (browser.isIE)
        button = window.event.srcElement;
      else
        button = event.currentTarget;
    
    
      // Blur focus from the link to remove that annoying outline.
      button.blur();
    
      // Associate the named menu to this button if not already done.
      // Additionally, initialize menu display.
      if (button.menu == null) {
          button.menu = document.getElementById(menuId);

          // as of 20090206, the menuInit function is global and external to this module
          if (button.menu.isInitialized == null) menuInit(button.menu);
      }
    
      // [MODIFIED] Added for activate/deactivate on mouseover.
    
      // Set mouseout event handler for the button, if not already done.
      if (button.onmouseout == null)
        button.onmouseout = sbButtonOrMenuMouseout;
    

      // Exit if this button is the currently active one.
      if (button == sbActiveButton)  return false;
    
      // [END MODIFIED]
    
      // Reset (i.e. ,hide) the currently active button's menu, if any.
      if (sbActiveButton != null) sbActiveButton.menu.style.display = "none";
    
    
      // Activate this button and display its sub-menu, unless it was the currently active one.
      if (button != sbActiveButton) 
      {
            sbActiveButton = button;
            sbActiveButton.menu.style.display = "block";
        
            //2009-02-04 -S. Davies
            if (sbActiveButton.menu.parentNode != null)
            {
                /*
                capture the background color of the Parent in a global for future restore,
                before changing it to the 'submenu' b'grd color
                */
    
                // capture style properties of the 'menu' of the 'active button' (active button is a link in this case)
                sbActiveMenuBgrd = retrieveComputedStyle(sbActiveButton.menu, "backgroundColor"); 
                sbActiveMenuColor = retrieveComputedStyle(sbActiveButton.menu, "color"); 
    
                // capture style properties of the active button's parent and persist globally for future restore
                sbActiveButtonParentBgrd = retrieveComputedStyle(sbActiveButton.parentNode, "backgroundColor"); 
                sbActiveButtonParentColor = retrieveComputedStyle(sbActiveButton.parentNode, "color"); 
    
                // set the style properties of the active button's parent to the active button's associated menu
                sbActiveButton.style.backgroundColor = sbActiveMenuBgrd;
    
                // hopefully will work even when the hover  state ends on the originating link, i.e. the 'button'
                sbActiveButton.style.color = sbActiveMenuColor;
                sbActiveButton.style.border = "1px solid silver";
            }
      
            // temporary Gecko bug fix
            if ((!browser.isIE) && (elMC != null)) elMC.style.overflow = "hidden";
      }
      else
      {
        sbActiveButton = null;
      }
      
      return false;
}


//----------------------------------------------------------------------------

function sbButtonMouseover(event, menuId) {
  var button;


// declared in menus.js module
   pausecomp(250);


// [MODIFIED] Added for activate/deactivate on mouseover.

  // Activates this button's menu if no other is currently active.

  if (sbActiveButton == null) {
    sbButtonClick(event, menuId);
    return;
  }

  // [END MODIFIED]

  // Find the target button element.

  if (browser.isIE)
    button = window.event.srcElement;
  else
    button = event.currentTarget;

  // If any other button menu is active, make this one active instead.

  if (sbActiveButton != null && sbActiveButton != button)
    sbButtonClick(event, menuId);
}

//----------------------------------------------------------------------------
// Code to handle the menus and sub menus.
//----------------------------------------------------------------------------

function sbMenuMouseover(event) {
  var menu;

  // Find the target menu element.

  if (browser.isIE)
    menu = getContainerWith(window.event.srcElement, "DIV", "sbMenu");
  else
    menu = event.currentTarget;

  // Close any active sub menu.
  
  if (menu.activeItem != null)
    closeSBSubMenu(menu);
}


//----------------------------------------------------------------------------

function sbMenuItemMouseover(event, menuId) {
  var item, menu, x, y;

  // Find the target item element and its parent menu element.

  if (browser.isIE)
    item = getContainerWith(window.event.srcElement, "A", "sbMenuItem");
  else
    item = event.currentTarget;
    menu = getContainerWith(item, "DIV", "sbMenu");

  // Close any active sub menu and mark this one as active.

  if (menu.activeItem != null)
    closeSBSubMenu(menu);
  menu.activeItem = item;

  // Highlight the item element.

  item.className += " menuItemHighlight";

  // Initialize the sub menu, if not already done.

  if (item.subMenu == null) {
    item.subMenu = document.getElementById(menuId);
    if (item.subMenu.isInitialized == null)
      menuInit(item.subMenu);
  }

  // [MODIFIED] Added for activate/deactivate on mouseover.

  // Set mouseout event handler for the sub menu, if not already done.

  if (item.subMenu.onmouseout == null)
    item.subMenu.onmouseout = sbButtonOrMenuMouseout;

  // [END MODIFIED]

  // show menu
  item.subMenu.style.display = "block";
  // Stop the event from bubbling.

  if (browser.isIE)
    window.event.cancelBubble = true;
  else
    event.stopPropagation();
}


//----------------------------------------------------------------------------

function closeSBSubMenu(menu) {
  if (menu == null || menu.activeItem == null)
    return;

  // Recursively close any sub menus.

  if (menu.activeItem.subMenu != null) {
    closeSBSubMenu(menu.activeItem.subMenu);
    menu.activeItem.subMenu.style.display = "none";
    menu.activeItem.subMenu = null;
  }

  // Deactivate the active menu item.

  removeClassName(menu.activeItem, "menuItemHighlight");
  menu.activeItem = null;
}

// [MODIFIED] Added for activate/deactivate on mouseover. Handler for mouseout
// event on buttons and menus.


//----------------------------------------------------------------------------

function sbButtonOrMenuMouseout(event) {
  var el = null;
  var elMC = null;

  // If there is no active button, exit.
  if (sbActiveButton == null) 
  {
    return;
  }

  // capture reference to the Main Content div to conditionally (subsequently) alter its overflow property
  elMC = document.getElementById("mainContent");
  if (elMC == null)
  {
     elMC = document.getElementById("mainContentRight");
  }
  
  //  Find and capture the element that the mouse is moving to
  if (browser.isIE)
  {
    el = window.event.toElement;
  }
  else if (event.relatedTarget != null)
  {
    el = (event.relatedTarget.tagName ? event.relatedTarget : event.relatedTarget.parentNode);
  }
  

  if (getContainerWith(el, "DIV", "sbMenu") == null) 
  {
     //   Hide the element that the mouse is moving away from
     if (sbActiveButton.menu != null) 
	 {
        sbActiveButton.menu.style.display = "none";

        // temporary Gecko bug fix
        if ((!browser.isIE) && (elMC != null)) elMC.style.overflow = "auto";

        //2009-02-04 -S. Davies
        if (sbActiveButton.menu.parentNode != null)
		{
           sbActiveButton.style.backgroundColor = sbActiveButtonParentBgrd;
           sbActiveButton.style.color = sbActiveButtonParentColor;
           sbActiveButton.style.border = "none";		   
		} //  (sbActiveButton.menu.parentNode != null)
    } // (sbActiveButton.menu != null)
	
    // clear globals
    sbActiveButton = null;
    sbActiveButtonParentBgrd = null;
    sbActiveButtonParentColor = null;
  } // (getContainerWith(el, "DIV", "sbMenu")
}

// [END MODIFIED]
