/**
 *
 *    NAME: header.js
 *    AUTHOR: Shanon Levenherz
 *    PURPOSE:  JavaScript file for HEADER.
 *
 *    REQUIRES: WM_checkIn from api.js
 *
 *    VERSION: $Id: header.js,v 1.5 2003/04/22 16:13:31 build Exp $
 SHG Jan 5 2004: some updates for onmousemove event.
 */

//----------------------------------------------------------------
// START GLOBALS
//----------------------------------------------------------------


var NS4 = (document.layers) ? 1 : 0;
var NS6 = (!document.all && document.getElementById) ? true : false;
var IE = (document.all) ? 1 : 0;

/**
 * This array holds the names of all DHTML dropdown menu DIVs.
 */
var dropDownIdArray = new Array();
dropDownIdArray[0] = 'knowledgeDiv';
dropDownIdArray[1] = 'serviceDiv';
dropDownIdArray[2] = 'aboutmercerDiv';

var activeMenuId = '';
var currentMouseX = -1;
var currentMouseY = -1;

//----------------------------------------------------------------
// END GLOBALS
//----------------------------------------------------------------

//----------------------------------------------------------------|
// START EVENT HANDLERS
//----------------------------------------------------------------|

if(NS4 || NS6)
{
   document.captureEvents(Event.MOUSEMOVE);
}
document.onmousemove = recordMousePosition;

//----------------------------------------------------------------|

/**
 * This is the onMouseMove event handler.
 * It records the current mouse position
 * in the GLOBALS: currentMouseX and currentMouseY
 */
function recordMousePosition(e)
{
   // Get the Event position.
   var e_x, e_y;
   if(IE)
   {
      e_x = event.clientX;
      e_y = event.clientY;
   }
   else
   {
      e_x = e.pageX;
      e_y = e.pageY;
   }
   currentMouseX = e_x;
   currentMouseY = e_y;
}

//----------------------------------------------------------------|

/**
 * Tests whether the mouse is currently
 * over a menu.  Uses the GLOBALS
 * currentMouseX and currentMouseY
 * as the (x,y) coords.
 * Returns true/false.
 */
function isMouseOverMenu(menuId)
{
   var x_min,x_max,y_min,y_max;

   var found = false;
   var theObj;
   if(IE)
   {
      theObj = document.all[menuId];
   }
   else if(NS4)
   {
      theObj = document.layers[menuId];
   }
   else
   {
      theObj = document.getElementById(menuId);
   }

   if(theObj)
   {
      if(NS4)
      {
         x_min = theObj.left;
         x_max = x_min + theObj.clip.width;
         y_min = theObj.top;
         y_max = y_min + theObj.clip.height;
      }
      else
      {
         // This is to handle the scrolling issues... luckily, Netscape doesn't have this problem.
         var pixelsScrolledDown = (IE) ? document.body.scrollTop : 0 ;
         var pixelsScrolledAcross = (IE) ? document.body.scrollLeft : 0 ;

         x_min = parseInt(theObj.offsetLeft) - pixelsScrolledAcross;
         x_max = x_min + parseInt(theObj.offsetWidth);

         y_min = parseInt(theObj.offsetTop) - pixelsScrolledDown;
         y_max = y_min + parseInt(theObj.offsetHeight);
      }

      //alert(x_min+'---'+x_max+'\n'+y_min+'---'+y_max);
	 // alert(currentMouseX+'---'+currentMouseY);
      if( ((x_min <= currentMouseX) && (currentMouseX <= x_max)) &&
          ((y_min <= currentMouseY) && (currentMouseY <= y_max)))
      {
         found = true;
      }
   }

   return found;
}

//----------------------------------------------------------------|

/**
 * onMouseOver Event Handler for Navigation Bar Images.
 */
function doNavBarMouseOver(imgName,menuId)
{
  doMenuDisplay(menuId);
}

//----------------------------------------------------------------|

/**
 * onMouseOut Event Handler for Navigation Bar Images.
 */
function doNavBarMouseOut(imgObj,menuId)
{
  doMenuHide(menuId);
}


//----------------------------------------------------------------
// END OBJECTS
//----------------------------------------------------------------

//----------------------------------------------------------------
// START METHODS
//----------------------------------------------------------------

/**
 * Displays a DHTML menu (and hides all other menus).
 */
function doMenuDisplay(menuId)
{
   // make sure the menu exists.
   var menuObj;
   if(NS4 || IE)
   {
      menuObj = WM_checkIn(menuId);
   }
   else { menuObj = document.getElementById(menuId); }

   if(menuObj && (menuObj.id != activeMenuId))
   {
      doMenuHideAllExcept(menuId);
      doMenuVisibility(menuId,'on');
   }
   activeMenuId = menuId;
}

//----------------------------------------------------------------|

/**
 * Hides a DHTML menu.
 */
function doMenuHide(menuId)
{
   var menuObj;
   if(NS4 || IE)
   {
      menuObj = WM_checkIn(menuId);
   }
   else { menuObj = document.getElementById(menuId); }
   if(menuObj)
   {
      //alert(menuId);
      self.setTimeout('monitorMouseForMenu(\''+menuId+'\')',250);
   }
}

//----------------------------------------------------------------|

/**
 * Recursive function that monitors whether the mouse is
 * over the active menu.  Keeps calling itself every 500ms
 * as long as the mouse is over the active menu.  When the mouse
 * moves out of the active menu, the menu is hidden
 * and the function returns.
 */
function monitorMouseForMenu(menuId)
{
   var menuObj;
   if(IE)
   {
      menuObj = document.all[menuId];
   }
   else if(NS4)
   {
      menuObj = document.layers[menuId];
   }
   else
   {
      menuObj = document.getElementById(menuId);
   }

   var activeMenuObj;

   if(IE)
   {
      activeMenuObj = document.all[activeMenuId];
   }
   else if(NS4)
   {
      activeMenuObj = document.layers[activeMenuId];
   }
   else
   {
      activeMenuObj = document.getElementById(activeMenuId);
   }

   var hide = true;
   if(activeMenuObj && (menuObj.id == activeMenuObj.id))
   {
		//alert(isMouseOverMenu(menuId));
      if(isMouseOverMenu(menuId))
      {
        //alert(menuId);
         self.setTimeout('monitorMouseForMenu(\''+menuId+'\')',250);
         hide = false;
      }
      else
      {
         activeMenuId = '';
      }
   }

   if(hide)
   {
      doMenuVisibility(menuId,'off');
   }
}
//----------------------------------------------------------------|

/**
 * Factored method to handle DHTML menu visibility.
 * Called by doMenuDisplay() and doMenuHide().
 */
function doMenuVisibility(menuId,vis)
{
   var visible = "visible", hidden = "hidden";
   if(NS4)
   {
      visible = "show"; hidden = "hide";
   }

   var theObj;
   if(NS4 || IE)
   {
     theObj = WM_checkIn(menuId);
   }
   else { theObj = document.getElementById(menuId).style; }

   if(theObj)
   {
      if(vis == 'off')
      {
         if(theObj.visibility == visible)
         {
            theObj.visibility = hidden;
         }
      }
      else
      {
         if(theObj.visibility == hidden)
         {
            theObj.visibility = visible;
         }
      }
   }
   //else
   //{
   //  alert('There was an error while displaying the popup menus.\nMENU['+menuId+']\nPlease contact the System Administrator.');
   //}
}

//----------------------------------------------------------------|

/**
 * Ripped this off WebMonkey...
 * http://hotwired.lycos.com/webmonkey/reference/javascript_code_library/wm_ckin_full/?tw=reference&category=dhtml
 */
function WM_checkIn(WM_id) {

/*
WM_checkIn()
Takes the ID of a positioned HTML element and returns an object reference.

Source: Webmonkey Code Library
(http://www.hotwired.com/webmonkey/javascript/code_library/)

Author: Taylor
Author Email: taylor@wired.com
Author URL: http://www.taylor.org/

Usage: WM_checkIn('id')
*/

  if(!WM_id) return;
  // First we initialize all the variables.
  var theObj,ss,sr,i,j,WM_layers=new Array();
  // This chunk handles the IE portion of the checkIn code.
  if (document.all) {
    // This checks to see if the inline style declaration has
    // a position property associated with it. If not, it will
    // scan the global stylesheets for the ID.
    if((document.all[WM_id].style.position != 'absolute') && (document.all[WM_id].style.position != 'relative')){
      // This little loop I'm very proud of, because it's kinda
      // slick and I wrote it all myself. It loops through all
      // global stylesheets and all the rules in each stylesheet,
      // tests for the selected ID, then returns that as the object.
      for (ss=0 ; ss < document.styleSheets.length; ss++) {
        for (sr=0 ; sr < document.styleSheets(ss).rules.length; sr++) {
          if (document.styleSheets(ss).rules(sr).selectorText == '#' + WM_id) {
            theObj = document.styleSheets(ss).rules(sr).style;
            break;
          }
        }
      }
    } else {
      // This works the same as in the light version, so you can
      // use inline styles.
      theObj = document.all[WM_id].style;
    }
  } else if(document.layers) {
    // Now we're in Netscapeland. The main problem here
    // is finding the object in a maze of hierarchy.
    // I wish I could say that I'm proud of this code,
    // because it's really slick. Unfortunately, I ripped
    // it off from Macromedia Dreamweaver's drag layer code
    // (with permission, of course :-)
    // Dreamweaver/Configuration/Behaviors/Actions/Drag Layer.htm
    // It works wonderfully and solves the problem.
    WM_layers = new Array();
    with (document) {
      for (i=0; i<layers.length; i++) WM_layers[i]=layers[i]; {
        for (i=0; i<WM_layers.length; i++) {
          if (WM_layers[i].document && WM_layers[i].document.layers) {
            for (j=0; j<WM_layers[i].document.layers.length; j++) {
              WM_layers[WM_layers.length] = WM_layers[i].document.layers[j];
            }
            if(WM_layers[i].name == WM_id){
              // So if the code matches the name of the layer,
              // return the reference.
              theObj = WM_layers[i];
            }
          }
        }
      }
    }
  }
  return theObj;
}

//----------------------------------------------------------------------------------------------



//----------------------------------------------------------------|

/**
 * Helper method to hide all DHTML menus except
 * the one that's specified.  If no params are passed,
 * ALL DHTML menus are hidden.
 */
function doMenuHideAllExcept(exceptionId)
{
   if(!exceptionId)
   {
      exceptionId = -1;
      activeMenuId = '';
   }
   for(var i=0;i<dropDownIdArray.length;i++)
   {
      if(exceptionId != dropDownIdArray[i])
      {
         doMenuVisibility(dropDownIdArray[i],'off');
      }
   }
}

//----------------------------------------------------------------
// END METHODS
//----------------------------------------------------------------

// END header.js
