﻿/***********************************************
 * Menu script by Robin. This does two things. *
 *                                             *
 * 1) contrainmenu() stops the menu from going *
 *    off the side of the screen. Called using *
 *    the addEvent() function below.           *
 * 2) makes the menu work with IE5&6. This is  *
 *    done using the cryptic code at the start *
 *    which makes the code IE only by using MS *
 *    'conditional compilation' (see MSDN). To *
 *    get this working properly we need the    *
 *    defer="defer" attribute on the calling   *
 *    script element. This means that the code *
 *    only runs once the DOM is loaded.        *
 ***********************************************/

// Add menu support for IE using conditional compliation.
// also add in hiding for select elements.
/*@cc_on @*/
/*@if (@_jscript_version < 5.7) // IE6 and below

marginLeftShift = -4; // this value is the margin-left on the dropdowns
marginTopShift = 23; // extra margin needed because? Oh well...

iframe = document.createElement("IFRAME");
iframe.style.position = "absolute";
iframe.style.top = "0";
iframe.style.left = "0";
iframe.style.display = "none";
iframe.style.zIndex = "1";
iframe.style.filter = "alpha(opacity=0)";
iframe.frameBorder = "0";

document.body.attachEvent("load", function(){document.body.appendChild(iframe)}); // call the onload handler

var sfEls = document.getElementById("menu").childNodes;

for (var i=0; i<sfEls.length; i++) {
  if (sfEls[i].nodeName == 'LI') {
    sfEls[i].onmouseover=function() {
      this.className+=" over";
      iframe.style.display = "block";
      dropdown = this;
      dropdown_left = 0;
      dropdown_top = 0;
      if (dropdown.offsetParent) {
        var dropdown_parents = dropdown;
        while (dropdown_parents.offsetParent) {
          dropdown_left += dropdown_parents.offsetLeft;
          dropdown_top += dropdown_parents.offsetTop;
          dropdown_parents = dropdown_parents.offsetParent;
        }
      }
      if (this.getElementsByTagName("div")[0]) {
        iframe.style.left = (dropdown_left + marginLeftShift) + "px";
        iframe.style.top = (dropdown_top + marginTopShift) +"px";
        iframe.style.width = this.getElementsByTagName("div")[0].offsetWidth+"px";
        iframe.style.height = this.getElementsByTagName("div")[0].offsetHeight+"px";
      }
    }
    sfEls[i].onmouseout=function() {
      this.className=this.className.replace(new RegExp(" over\\b"), "");
      iframe.style.display = "none";
      iframe.style.width = "0";
      iframe.style.height = "0";
    }
  }
}
/*@end @*/

constrainMenu = function(e) {
  e = e || window.event;
  target = e.target || e.srcElement;

  // don't bother unless we're hovering over a main item
  var topLevel = true;
  var currentNode = target.parentNode;
  while (currentNode.parentNode) {
    currentNode = currentNode.parentNode;
    if (currentNode.nodeName == 'LI') {
      topLevel = false;
      break;
    }
  }

  if(topLevel) {
		var dropdown = this.getElementsByTagName('div')[0];
		if ( dropdown ) {
			/* find the dropdown list's position from left */
			var dropdown_left = 0;
			if (dropdown.offsetParent) {
				var dropdown_parents = dropdown.parentNode;
				while (dropdown_parents.offsetParent) {
					dropdown_left += dropdown_parents.offsetLeft;
					dropdown_parents = dropdown_parents.offsetParent;
				}
			}
			/* find the dropdowns width */
			var dropdown_width = dropdown.offsetWidth;

			/* find the width of the viewport */
			var x;
			if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				x = document.documentElement.clientWidth;
			}
			else if (document.body) { // other Explorers
				x = document.body.clientWidth;
			}
			/* now, do the adjustment if needed */
			if ( (dropdown_left + dropdown_width) > x ) {
				dropdown.style.marginLeft = -((dropdown_left + dropdown_width) - x) + "px";
			}
		}
  }
}

addOver = function() {
  this.className += " open";
}

removeOver = function() {
  this.className=this.className.replace(new RegExp("\\bopen\\b"), "");
}

showMenu = function() {
  this.className += " focussed";
  if (this.parentNode.parentNode.parentNode.parentNode.className.indexOf("over") == -1) { this.parentNode.parentNode.parentNode.parentNode.className += " over"; }
  if (this.parentNode.parentNode.parentNode.parentNode.className.indexOf("open") == -1) { this.parentNode.parentNode.parentNode.parentNode.className += " open"; }
}

hideMenus = function() {
  this.className = this.className.replace(new RegExp("\\bfocussed\\b"), "");
  main_li = document.getElementById('menu').childNodes;
  for (i=0; i<main_li.length; i++) {
    if (main_li[i].nodeName == 'LI') {
      main_li[i].className = main_li[i].className.replace(new RegExp("\\bover\\b"), "");
      main_li[i].className = main_li[i].className.replace(new RegExp("\\bopen\\b"), "");
    }
  }
}

function addEvent( obj, type, fn ) {
  if ( obj.attachEvent ) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
    obj.attachEvent( 'on'+type, obj[type+fn] );
  } else
    obj.addEventListener( type, fn, false );
}

addEvent(window, 'load', setupMenu);

function setupMenu() {
  main_li = document.getElementById('menu').childNodes;
  for (i=0; i<main_li.length; i++) {
    if (main_li[i].nodeName == 'LI') {
      addEvent(main_li[i], 'mouseover', constrainMenu);
      addEvent(main_li[i], 'mouseover', addOver);
      addEvent(main_li[i], 'mouseout', removeOver);
      sub_li = main_li[i].getElementsByTagName("li");
      for (j=0; j<sub_li.length; j++) {
        addEvent(sub_li[j].getElementsByTagName("a")[0], 'focus', showMenu);
        addEvent(sub_li[j].getElementsByTagName("a")[0], 'blur', hideMenus);
      }
      addEvent(main_li[i].getElementsByTagName("a")[0], 'focus', hideMenus);
    }
  }
}