//*****************************************************************************
// Do not remove this notice.
//
// Copyright 2000-2004 by Mike Hall.
// See http://www.brainjar.com for terms of use.
//*****************************************************************************

// Modified by Andrew Graham at HSW 2004 for compatibility with IE5
// because array.push() function isn't supported in IE5.
// Also, in order to degrade gracefully, events are ignored if
// browser is an older version or it's a Mac.
// Well, it seemed easier than rewriting the whole thing.

// 2004.10.18 AG = fixed problem - now javascript events fire upon page load
// 2005.02.01 AG = now works again in firefox, and includes IFRAME fix
// 2005.02.02 AG = added brainjar's code for mouseover/mouseout - far more efficient!
// 2005.02.02 AG = removed "onload" event as it clashed with page's "body onload()" tag
// 2005.02.16 DB = Added delay so the menu sticks around for a bit after the mouseout's
// 2005.07.06 DB = Commented out unused (and undeclared) variable mbStart
//                  Added semi-colons for compression.
// 2005.11.16 AG = if left hand navigation is selected then menus fly out to the right
//					rather than below the main menu
// 2006.03.29 AG = fixed positioning bug of submenus in some versions of IE

//2009-07-03, ET : updated to take site specific menu delay time
//----------------------------------------------------------------------------
// Code to determine the browser and version.
//----------------------------------------------------------------------------

function Browser() {

var ua, pl, s, i;

  this.isIE      = false;  // Internet Explorer
  this.isOP      = false;  // Opera
  this.isNS      = false;  // Netscape
  this.supported = true;
  this.version   = null;
  this.docloaded = false;

  ua = navigator.userAgent;
  pl = navigator.platform;

  if ((i = pl.indexOf("Mac")) >= 0) {
	this.supported = false;
	return;
	}
  
  s = "Opera";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isOP = true;
    this.version = parseFloat(ua.substr(i + s.length));
	if (this.version < 7)
		this.supported = false;
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
	if (this.version < 6)
		this.supported = false;
    return;
  }

  // Treat any other "Gecko" browser as Netscape 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
	if (this.version < 5)
		this.supported = false;
    return;
  }
  
	this.supported = false;

};


var browser = new Browser();
/*
// variable to note if document is loaded
window.onload = setloaded;

function setloaded() {
	browser.docloaded = true;
//	alert("page loaded");
}
*/

//----------------------------------------------------------------------------
// Code for handling the menu bar and active button.
//----------------------------------------------------------------------------

var activeButton = null;

// Capture mouse clicks on the page so any active button can be
// deactivated.
//alert(menudelay);

var timeoutdelay = menudelay;	// Timeout before menu disappears in milliseconds.
var mbStart = "";
var mbHideMenuTimer = null; // Sets up timeout vars.
var mbDHFlag = false;       // DoHide Flag, set if a menu needs to be hidden

/* [MODIFIED] This code commented out, not needed for activate/deactivate
   on mouseover.

if (browser.supported)
{
if (browser.isIE)
  document.onmouseover = pageMouseover;
else
  document.addEventListener("mouseover", pageMouseover, true);
}

function pageMouseover(event) {

  var el;

  // If there is no active button, exit.

  if (activeButton == null)
    return;

  // Find the element that was clicked on.

  if (browser.isIE)
    el = window.event.srcElement;
  else
    el = (event.target.tagName ? event.target : event.target.parentNode);

  // If the active button was clicked on, exit.

  if (el == activeButton)
    return;

  // If the element is not part of a menu or button, reset and clear the active
  // button, but only after 2 seconds.

  if (getContainerWith(el, "DIV", "menuBar") == null && getContainerWith(el, "DIV", "menu") == null)
  	{ clearscreen();}
}

function clearscreen() {
  { resetButton(activeButton); activeButton = null;}
}

[END MODIFIED] */


function buttonClick(event, menuId) {

if (browser.supported && browser.docloaded) {

  var button;
  
  mb_clearTimeout(); // Clears timeout since we're displaying a new menu.

  // 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);
    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 = buttonOrMenuMouseout;

  // Exit if this button is the currently active one.

  if (button == activeButton)
    return false;

  // [END MODIFIED]

  
  // Reset the currently active button, if any.

  if (activeButton != null)
    resetButton(activeButton);

  // Activate this button, unless it was the currently active one.

  if (button != activeButton) {
    depressButton(button);
    activeButton = button;
  }
  else
    activeButton = null;

  return false;
 }
};

function buttonMouseover(event, menuId) {

if (browser.supported) {

  var button;
  
  // [MODIFIED] Added for activate/deactivate on mouseover.

  // Activates this button's menu if no other is currently active.

  if (activeButton == null) {
    buttonClick(event, menuId);
    return;
  }

  // [END MODIFIED]
  
  mb_clearTimeout(); // Clears timeout since we're displaying a new menu.
  
  // 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 (activeButton != null && activeButton != button)
    buttonClick(event, menuId);
}
};

function depressButton(button) {

  var x, y;
  mb_clearTimeout(); // Clears timeout since we're displaying a new menu.
  // Update the button's style class to make it look like it's
  // depressed.

  button.className += " menuButtonActive";
  
  // [MODIFIED] Added for activate/deactivate on mouseover.

  // Set mouseout event handler for the button, if not already done.

  if (button.onmouseout == null)
    button.onmouseout = buttonOrMenuMouseout;
  if (button.menu.onmouseout == null)
    button.menu.onmouseout = buttonOrMenuMouseout;

  // [END MODIFIED]

  // Position the associated drop down menu under the button and
  // show it.
  
  /* modified by AG - submenu appears either to the right, or below, depending on whether it's a top or lhs menu */
  /* 29.03.2006 AG - fixed positioning bug in IE */ 
 
  x = getPageOffsetLeft(button);
  if (getContainerWith(button, "DIV", "menuBar") == null) {
 	 y = getPageOffsetTop(getContainerWith(button, "TD", "")); 
  } else {
	 y = getPageOffsetTop(button);
  }
  
  
  if (getContainerWith(button, "DIV", "menuBar") != null) {
	 y = y + button.offsetHeight; 
  } else {
	 x = x + button.offsetWidth;
  }


  // For IE, adjust position.

  if (browser.isIE) {
    x += button.offsetParent.clientLeft;
    y += button.offsetParent.clientTop;
  }
  
  
 //AG: if the dropdown menu doesn't fit on screen, then adjust x value accordingly
  if (button.menu != document.getElementById('emptyMenu'))
  {
  // Adjust position to fit in view.

  var maxX;

  if (browser.isIE) {
    maxX = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft) +
      (document.documentElement.clientWidth != 0 ? document.documentElement.clientWidth : document.body.clientWidth);
  }
  if (browser.isOP) {
    maxX = document.documentElement.scrollLeft + window.innerWidth;
  }
  if (browser.isNS) {
    maxX = window.scrollX + window.innerWidth;
  }
  maxX -= button.menu.offsetWidth;

  if (x > maxX)
    x = Math.max(0, maxX);

  button.menu.style.left = x + "px";
  button.menu.style.top  = y + "px";
  button.menu.style.visibility = "visible";
}


  // For IE; size, position and show the menu's IFRAME as well.

  if (button.menu.iframeEl != null)
  {
    button.menu.iframeEl.style.left = button.menu.style.left;
    button.menu.iframeEl.style.top  = button.menu.style.top;
    button.menu.iframeEl.style.width  = button.menu.offsetWidth + "px";
    button.menu.iframeEl.style.height = button.menu.offsetHeight + "px";
    button.menu.iframeEl.style.display = "";
  }

};

function resetButton(button) {

  // Restore the button's style class.

  removeClassName(button, "menuButtonActive");

  // Hide the button's menu, first closing any sub menus.

  if (button.menu != null) {
    closeSubMenu(button.menu);
    button.menu.style.visibility = "hidden";
	

    // For IE, hide menu's IFRAME as well.

    if (button.menu.iframeEl != null)
      button.menu.iframeEl.style.display = "none";
	
  }
};

//----------------------------------------------------------------------------
// Code to handle the menus and sub menus.
//----------------------------------------------------------------------------

function menuMouseover(event) {

if (browser.supported) {
  var menu;

  mb_clearTimeout(); // Clears timeout since we're displaying a new menu.

  // Find the target menu element.

  if (browser.isIE)
    menu = getContainerWith(window.event.srcElement, "DIV", "menu");
  else
    menu = event.currentTarget;

  // Close any active sub menu.

  if (menu.activeItem != null)
    closeSubMenu(menu);
}
};

function menuItemMouseover(event, menuId) {

if (browser.supported) {

  var item, menu, x, y;
  
  mb_clearTimeout(); // Clears timeout since we're displaying a new menu.

  // Find the target item element and its parent menu element.

  if (browser.isIE)
    item = getContainerWith(window.event.srcElement, "A", "menuItem");
  else
    item = event.currentTarget;
  menu = getContainerWith(item, "DIV", "menu");

  // Close any active sub menu and mark this one as active.

  if (menu.activeItem != null)
    closeSubMenu(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 = buttonOrMenuMouseout;

  // [END MODIFIED]

  // Get position for submenu based on the menu item.

  x = getPageOffsetLeft(item) + item.offsetWidth;
  y = getPageOffsetTop(item);

  // Adjust position to fit in view.

  var maxX, maxY;

  if (browser.isIE) {
    maxX = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft) +
      (document.documentElement.clientWidth != 0 ? document.documentElement.clientWidth : document.body.clientWidth);
    maxY = Math.max(document.documentElement.scrollTop, document.body.scrollTop) +
      (document.documentElement.clientHeight != 0 ? document.documentElement.clientHeight : document.body.clientHeight);
  }
  if (browser.isOP) {
    maxX = document.documentElement.scrollLeft + window.innerWidth;
    maxY = document.documentElement.scrollTop  + window.innerHeight;
  }
  if (browser.isNS) {
    maxX = window.scrollX + window.innerWidth;
    maxY = window.scrollY + window.innerHeight;
  }
  maxX -= item.subMenu.offsetWidth;
  maxY -= item.subMenu.offsetHeight;

  if (x > maxX)
    x = Math.max(0, x - item.offsetWidth - item.subMenu.offsetWidth
      + (menu.offsetWidth - item.offsetWidth));
  y = Math.max(0, Math.min(y, maxY));

  // Position and show it.

  item.subMenu.style.left = x + "px";
  item.subMenu.style.top  = y + "px";
  item.subMenu.style.visibility = "visible";
  
   // For IE; size, position and show the menu's IFRAME as well.

  if (item.subMenu.iframeEl != null)
  {
    item.subMenu.iframeEl.style.left    = item.subMenu.style.left;
    item.subMenu.iframeEl.style.top     = item.subMenu.style.top;
    item.subMenu.iframeEl.style.width   = item.subMenu.offsetWidth + "px";
    item.subMenu.iframeEl.style.height  = item.subMenu.offsetHeight + "px";
    item.subMenu.iframeEl.style.display = "";
  }

  // Stop the event from bubbling.

  if (browser.isIE)
    window.event.cancelBubble = true;
  else
    event.stopPropagation();
}
};

function closeSubMenu(menu) {

  if (menu == null || menu.activeItem == null)
    return;

  // Recursively close any sub menus.

  if (menu.activeItem.subMenu != null) {
    closeSubMenu(menu.activeItem.subMenu);
	
    menu.activeItem.subMenu.style.visibility = "hidden";
	
    // For IE, hide the sub menu's IFRAME as well.

    if (menu.activeItem.subMenu.iframeEl != null)
      menu.activeItem.subMenu.iframeEl.style.display = "none";
	
	
    menu.activeItem.subMenu = null;
  }
  removeClassName(menu.activeItem, "menuItemHighlight");
  
  // Deactivate the active menu item.  

  menu.activeItem = null;
};

// [MODIFIED] Added for activate/deactivate on mouseover. Handler for mouseout
// event on buttons and menus.

function buttonOrMenuMouseout(event) {

  var el;

  // If there is no active button, exit.

  if (activeButton == null)
    return;

  // Find the element 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 the element is not part of a menu, reset the active button.

  if (getContainerWith(el, "DIV", "menu") == null) {
    // Added timer code to set a delay before the menu disappears due to a mouseout
	mb_startTimeout();
    //resetButton(activeButton);
    //activeButton = null;
  }
};

// [END MODIFIED]

// Clears the timeout, should be called on a new menu mouseover
function mb_clearTimeout() {
	if (mbHideMenuTimer) clearTimeout(mbHideMenuTimer);
	mbHideMenuTimer = null;
	mbDHFlag = false;
};

// Starts the timer counting towards hiding the menu "mbDoHide()"
function mb_startTimeout() {
	//if( window.ActiveMenu ) {
		mbStart = new Date();
		mbDHFlag = true;
		mbHideMenuTimer = setTimeout("mbDoHide()", timeoutdelay);
	//}
};

// Hides the menu, sets the "do hide" flag to false
function mbDoHide() {
	if (!mbDHFlag) return;
	resetButton(activeButton);
    activeButton = null;
	mbDHFlag = false;
};

//----------------------------------------------------------------------------
// Code to initialize menus.
//----------------------------------------------------------------------------

function menuInit(menu) {

  var itemList, spanList;
  var textEl, arrowEl;
  var itemWidth;
  var w, dw;
  var i, j;

  // For IE, replace arrow characters.

  if (browser.isIE) {
    menu.style.lineHeight = "2.5ex";
    spanList = menu.getElementsByTagName("SPAN");
    for (i = 0; i < spanList.length; i++)
      if (hasClassName(spanList[i], "menuItemArrow")) {
        spanList[i].style.fontFamily = "Webdings";
        spanList[i].firstChild.nodeValue = "4";
      }
  }

  // Find the width of a menu item.

  itemList = menu.getElementsByTagName("A");
  if (itemList.length > 0)
    itemWidth = itemList[0].offsetWidth;
  else
    return;

  // For items with arrows, add padding to item text to make the
  // arrows flush right.

  for (i = 0; i < itemList.length; i++) {
    spanList = itemList[i].getElementsByTagName("SPAN");
    textEl  = null;
    arrowEl = null;
    for (j = 0; j < spanList.length; j++) {
      if (hasClassName(spanList[j], "menuItemText"))
        textEl = spanList[j];
      if (hasClassName(spanList[j], "menuItemArrow")) {
        arrowEl = spanList[j];
      }
    }
    if (textEl != null && arrowEl != null) {
      textEl.style.paddingRight = (itemWidth 
        - (textEl.offsetWidth + arrowEl.offsetWidth)) + "px";
      // For Opera, remove the negative right margin to fix a display bug.
      if (browser.isOP)
        arrowEl.style.marginRight = "0px";
    }
  }

  // Fix IE hover problem by setting an explicit width on first item of
  // the menu.
  //AG adds: actually need to do this for every element in the list for compatibility with IE 5

  if (browser.isIE) {
  	for (i=0; i < itemList.length; i++)
	{
    w = itemList[i].offsetWidth;
    itemList[i].style.width = w + "px";
    dw = itemList[i].offsetWidth - w;
    w -= dw;
    itemList[i].style.width = w + "px";
	}
  }
  
   // Fix the IE display problem (SELECT elements and other windowed controls
  // overlaying the menu) by adding an IFRAME under the menu.

  if (browser.isIE) {
    menu.iframeEl = menu.parentNode.insertBefore(document.createElement("IFRAME"), menu);
    menu.iframeEl.style.display = "none";
    menu.iframeEl.style.position = "absolute";
  }
  
  // Mark menu as initialized.

  menu.isInitialized = true;
};

//----------------------------------------------------------------------------
// General utility functions.
//----------------------------------------------------------------------------

function getContainerWith(node, tagName, className) {

  // Starting with the given node, find the nearest containing element
  // with the specified tag name and style class.

  while (node != null) {
    if (node.tagName != null && node.tagName == tagName &&
        hasClassName(node, className))
      return node;
    node = node.parentNode;
  }

  return node;
};

function hasClassName(el, name) {

  var i, list;

  // Return true if the given element currently has the given class
  // name.

  list = el.className.split(" ");
  for (i = 0; i < list.length; i++)
    if (list[i] == name)
      return true;

  return false;
};

function removeClassName(el, name) {

  var i, count, curList, newList;

  if (el.className == null)
    return;

  // Remove the given class name from the element's className property.

  newList = new Array();
  curList = el.className.split(" ");
  count = 0;
  for (i = 0; i < curList.length; i++)
    if (curList[i] != name)
	{
      newList[count]=curList[i]; count++;
	  }
  el.className = newList.join(" ");
};

function getPageOffsetLeft(el) {

  var x;

  // Return the x coordinate of an element relative to the page.

  x = el.offsetLeft;
  if (el.offsetParent != null)
    x += getPageOffsetLeft(el.offsetParent);

  return x;
};

function getPageOffsetTop(el) {

  var y;

  // Return the x coordinate of an element relative to the page.

  y = el.offsetTop;
  if (el.offsetParent != null)
    y += getPageOffsetTop(el.offsetParent);

  return y;
};

function removeFocus(oLink) {
	// Removes focus from clicked link, makes the menu look nicer.
	oLink.blur();
	return true;
}