/**
 *
 *    NAME: header.js
 *    AUTHOR: Shanon Levenherz
 *    PURPOSE:  JavaScript file for HEADER.
 *
 *    REQUIRES: WM_checkIn from api.js
 *
 *    VERSION: $Id$
 */

//----------------------------------------------------------------
// 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] = 'aboutNERADiv';
dropDownIdArray[1] = 'PracticeAreaDiv';
dropDownIdArray[2] = 'publicationsDiv';
dropDownIdArray[3] = 'pressroomDiv';
dropDownIdArray[4] = 'careersDiv';

/**
 * This array holds the cached image wrappers.
 */
var imageArray = new Array();

/**
 * These are the Regular Expressions that, when applyed to
 * the image src will signify the image as lit (on) or unlit (off).
 */
var onRE_v1 = /(\_on\_)/;
var onRE_v2 = /(\_on\.)/;
var offRE_v1 = /(\_off\_)/;
var offRE_v2 = /(\_off\.)/;

var imgExt = /(\.(gif|jpg))$/;

/**
 * Helper globals for DHTML menus.
 */
var activeMenuId = '';
var activeMenuImageLit = false;
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);

      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)
{
   var imgObj = eval('document.images.'+imgName);
//   doCachedMouseOver(imgObj);
   activeMenuImageLit = true;
   doMenuDisplay(menuId);
}

//----------------------------------------------------------------|

/**
 * onMouseOut Event Handler for Navigation Bar Images.
 */
function doNavBarMouseOut(imgObj,menuId)
{
   doCachedMouseOut(eval('document.images.'+imgObj));
   activeMenuImageLit = false;
   doMenuHide(menuId);
}

//----------------------------------------------------------------|

/**
 * Handles mouse overs for cached images.
 */
function doCachedMouseOver(imgObj)
{
   var theWrapper = imageArray[imgObj.name];
   if(theWrapper)
   {
      if(imgObj.src != theWrapper.onImage.src)
      {
         imgObj.src = theWrapper.onImage.src;
      }
   }
}

//----------------------------------------------------------------|

/**
 * Handles mouse outs for cached images.
 */
function doCachedMouseOut(imgObj)
{
   var theWrapper = imageArray[imgObj.name];
   if(theWrapper)
   {
      if(theWrapper.initial == 0)
      {
         imgObj.src = theWrapper.offImage.src;
      }
   }
}

//----------------------------------------------------------------|
// END EVENT HANDLERS
//----------------------------------------------------------------|


//----------------------------------------------------------------
// START OBJECTS
//----------------------------------------------------------------

/**
 * This is a simple JavaScript object that stores information
 * about each navigation bar image.
 *
 * onImage  - The lit (ON) image src.
 * offImage - The unlit (OFF) image src.
 * initial  - Either 1 or 0.  1 means that the image was initially ON,
 *            0 means it was initially off.
 */
function ImageWrapper(onImage,offImage,initial)
{
   this.onImage = new Image();
   this.onImage.src = onImage;
   this.offImage = new Image();
   this.offImage.src = offImage;
   this.initial = initial;

   return this;
}

//----------------------------------------------------------------
// END OBJECTS
//----------------------------------------------------------------

//----------------------------------------------------------------
// START METHODS
//----------------------------------------------------------------

/**
 * This is the initialization function for the header.
 * It loads up all the image objects into the ImageWrappers
 * which implicitly caches them.
 */
function initHeader()
{

   imageArray['home'] = createWrapper(document.images.home.src);
   imageArray['knowledge'] = createWrapper(document.images.knowledge.src);
   imageArray['service'] = createWrapper(document.images.service.src);
   imageArray['careers'] = createWrapper(document.images.careers.src);
   imageArray['about'] = createWrapper(document.images.about.src);
   imageArray['help'] = createWrapper(document.images.help.src);
   imageArray['contact'] = createWrapper(document.images.contact.src);

}

//----------------------------------------------------------------|

function createCachedImage(imgName,imgSrc)
{
   imageArray[imgName] = createWrapper(imgSrc);
}

//----------------------------------------------------------------|

/**
 * This is a factored method that takes the initial image,
 * figures out whether it's lit or unlit, then
 * creates an ImageWrapper with the correct INITIAL value.
 */
function createWrapper(anImgSrc)
{
   var wrapper;

   var initial = 0;
   var offSrc, onSrc;
   if(isLit(anImgSrc))
   {
      initial = 1;
      onSrc = anImgSrc;
      if(onRE_v1.test(anImgSrc))
      {
        offSrc = anImgSrc.replace(onRE_v1,"_off_");
      }
      else
      {
        offSrc = anImgSrc.replace(onRE_v2,"_off.");
      }
      wrapper = new ImageWrapper(onSrc,offSrc,initial);
   }
   else
   {
      initial = 0;
      offSrc = anImgSrc;
      if(offRE_v1.test(anImgSrc))
      {
        onSrc = anImgSrc.replace(offRE_v1,"_on_");
      }
      else if(offRE_v2.test(anImgSrc))
      {
        onSrc = anImgSrc.replace(offRE_v2,"_on.");
      }
      else // If there's no 'off' in the name, insert '_on' before the .gif or .jpg.
      {
        onSrc = anImgSrc.replace(imgExt,"_on$1");
      }

      wrapper = new ImageWrapper(onSrc,offSrc,initial);
   }
   return wrapper;
}

//----------------------------------------------------------------|

/**
 * Uses the Regular Expressions to see if an image
 * is lit (ON) or unlit (OFF).
 */
function isLit(anImgSrc)
{

   if(onRE_v1.test(anImgSrc) ||
      onRE_v2.test(anImgSrc)) { return true; }

   else { return false; }
}

/**
 * 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)
   {
      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))
   {
      if(isMouseOverMenu(menuId) || activeMenuImageLit)
      {
         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.');
   //}
}

//----------------------------------------------------------------|

/**
 * 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
