/*
 * PVL UK
 *
 * Script: navigation.js
 * Navigation rollover effect
 *
 * This script sets up event handlers so that when an item in the main
 * navigation menu has the mouse over it, all previous items have the
 * 'prehover' class. When the mouse is moved to another item or out of
 * the list, the class is removed as appropriate. The visual effect is
 * a highlight line that extends all the way from the top of the list
 * to the item with the mouse over it.
 *
 * By Andy Smith, Preview Graphics
 * www.preview.co.uk
 */
 
/* Global variables */
pvlnav = {};

/* Initialise */
function initPVLNav(e)
{
  var contchildren;

  pvlnav.active = (document.getElementById ? true : false);
  if (!pvlnav.active) return;

  /* Get list of navigation items (children of the first child of the
     #page-main-nav element - done this way because if IE CSS bugs are
     sorted out the navigation will revert to a UL rather than DIVs. */
  pvlnav.navcontainer = document.getElementById('page-main-nav');
  if (pvlnav.navcontainer)
      contchildren = childElements(pvlnav.navcontainer);
  if (pvlnav.navcontainer && (contchildren.length > 0))
      pvlnav.navlist = contchildren[0];
  if (pvlnav.navlist)
      pvlnav.items = childElements(pvlnav.navlist);
  if (pvlnav.items)
    {
      pvlnav.links = [];
      for (var i = 0; i < pvlnav.items.length; i++)
        {
	  itemlinks = pvlnav.items[i].getElementsByTagName('A');
	  if (itemlinks.length > 0) pvlnav.links[i] = itemlinks[0];
	}
    }
  if (!pvlnav.links)
    {
      pvlnav.active = false;
      return;
    }
    
  for (var i = 0; i < pvlnav.links.length; i++)
    {
      addEvent(pvlnav.links[i], 'mouseover', navHoverHandler(i));
    }
  addEvent(pvlnav.navlist, 'mouseout', navHoverHandler(-1));
}

/* Return an event handler function that will set the 'prehover' class
   on all navigation items before the one specified (starting from 0),
   and remove the class from the specified item and all after it. If
   the item number is negative, the class will be removed from all
   items. */
function navHoverHandler(itemIndex)
{
  return function (e)
    {
      for (var i = 0; i < pvlnav.links.length; i++)
        {
	  setClass(pvlnav.items[i], 'prehover', i < itemIndex);
        }
    }
}

addEvent(window, 'load', initPVLNav);
