/*
mark@shift.co.nz | 03

Max Gilbert 20 June 06
Added iframe menu 'shim' for IE to work around issue with Select lists always showing over menu
See http://dotnetjunkies.com/WebLog/jking/archive/2003/07/21/488.aspx
 
*/


if (!document.getElementById)
	document.getElementById = function() { return null; }

var hideDelay = 300;
var showDelay = 10;

var currentMenu = null;
var currentSubMenu = null;
var hideOut = null;
var hideSubOut = null;
var showTime = null;

function isIE(){
	var browser=navigator.appName;
	//var b_version=navigator.appVersion;
	if(browser=="Microsoft Internet Explorer"){
		return true;
	}else{
		return false;
	}
}



function setMenu(menuId, buttonId, menuAlign) {
    var menu 			= document.getElementById(menuId);
    var button 			= document.getElementById(buttonId);
	var menushim 		= document.getElementById('menushim');
	var menushim_sub 	= document.getElementById('menushim_sub');

	if (menuAlign == "subright" || menuAlign == "subleft") {
		button.className = "menuOff";
	}

	if (menu == null || button == null) return;
	
	menu.onmouseover = function() {
		if (menuAlign == "subright" || menuAlign == "subleft") {
			button.className = "menuHilite";
			clearTimeout(hideSubOut);
		} else {
			clearTimeout(hideOut);
		}
	}
	menu.onmouseout = function() {
		if (menuAlign == "subright" || menuAlign == "subleft") {
			button.className = "";
			hideSubOut = setTimeout("hideSubMenu();", hideDelay);
		} else {
			hideOut = setTimeout("hideMenu();", hideDelay);
		}
	}

	button.onmouseover = function() {
		if (hideOut) {
			clearTimeout(hideOut);
		} 
		
		if (currentMenu != menu && currentMenu != null){
			hideMenu();
		}
		
		if (menuAlign=="right") {
			this.showMenuRight();
			showMenu();
		} else if (menuAlign=="left") {
			this.showMenuLeft();
			showMenu();
		} else if (menuAlign=="center") {
			this.showMenuCenter();
			showTime = setTimeout("showMenu();", showDelay);
		} else if (menuAlign=="center-right") {
			this.showMenuCenterRight();
			showTime = setTimeout("showMenu();", showDelay);
		} else if (menuAlign=="subright") {
			this.showSubMenuRight();
			showTime = setTimeout("showMenu();", showDelay);
		} else if (menuAlign=="subleft") {
			this.showSubMenuLeft();
			showTime = setTimeout("showMenu();", showDelay);
		}
		
 	}
	button.onmouseout = function() {
		if (menuAlign == "subright" || menuAlign == "subleft") {
			hideSubOut = setTimeout("hideSubMenu();", hideDelay);
		} else {
			hideOut = setTimeout("hideMenu();", hideDelay);
		}
		if (showTime) {
			clearTimeout(showTime);
		}
		if (hideOut) {
			clearTimeout(hideOut);
		}
		if (currentMenu){
			hideOut = setTimeout("hideMenu();", hideDelay);
		}
	}
	showMenu = function() {
		if (currentMenu) {
			currentMenu.style.display = "block";
			//Check for height once the item is visible
			menushim.style.height = currentMenu.offsetHeight -6 + "px";
			menushim.style.width = currentMenu.offsetWidth -2 + "px";
			menushim.style.display = "block";
		}
		if (currentSubMenu) {
			currentSubMenu.style.display = "block";
			//Check for height once the item is visible
			menushim_sub.style.height = currentSubMenu.offsetHeight + "px";
			menushim_sub.style.width = currentSubMenu.offsetWidth + "px";
			menushim_sub.style.display = "block";
		}
	}
	hideMenu = function() {
		menushim.style.display = "none";
		currentMenu.style.display = "none";
		currentMenu = null;
	}
	hideSubMenu = function() {
		menushim_sub.style.display = "none";
		currentSubMenu.style.display = "none";
		currentSubMenu = null;
	}
	
	// menu display methods	
	button.showMenuLeft = function() {
        menu.style.left =  getOffsetLeft(this) + "px";
        menu.style.top = getOffsetTop(this) + this.offsetTop + "px";
		menushim.style.left =  menu.style.left;
		menushim.style.top =  menu.style.top;
		currentMenu = menu;
    }
	button.showMenuRight = function() {
        menu.style.left =  getOffsetLeft(this) + this.offsetWidth + "px";
        menu.style.top = getOffsetTop(this) + "px";
		menushim.style.left =  menu.style.left;
		menushim.style.top =  menu.style.top;
		currentMenu = menu;
    }
	button.showMenuCenter = function() {
        var topOffset = 0;
		
		if(isIE()){
			topOffset = getOffsetTop(this) + this.offsetHeight + 1;
		}else{
			topOffset = getOffsetTop(this) + this.offsetHeight
		}
        
        menu.style.left =  getOffsetLeft(this) + "px";
        menu.style.top = topOffset + "px";
		menushim.style.left = menu.style.left;
		menushim.style.top =  menu.style.top;
		currentMenu = menu;
		
	
    }
	button.showMenuCenterRight = function() {
		//Have to hardcode the width set by the style
		var topOffset = 0;
		
		if(isIE()){
			topOffset = getOffsetTop(this) + this.offsetHeight + 1;
		}else{
			topOffset = getOffsetTop(this) + this.offsetHeight;
		}

       	menu.style.left =  getOffsetLeft(this) - (180 - this.offsetWidth) + "px";
        menu.style.top = topOffset + "px";
		menushim.style.left = menu.style.left;
		menushim.style.top =  menu.style.top;
		currentMenu = menu;
		
		;
    }
	
	// subMenu display methods	
	button.showSubMenuRight = function() {
	    menu.style.left = this.offsetLeft + (this.offsetParent.offsetWidth - 16) + "px";
		menu.style.top =  this.offsetTop + "px";
		menushim_sub.style.left =  menu.style.left;
		menushim_sub.style.top =  menu.style.top;
		currentSubMenu = menu;
	}
	button.showSubMenuLeft = function() {
	    menu.style.left =  this.offsetLeft - (this.offsetParent.offsetWidth + 1) + "px";
		menu.style.top =  this.offsetTop - 10 + "px";
		menushim_sub.style.left =  menu.style.left;
		menushim_sub.style.top =  menu.style.top;
		currentSubMenu = menu;
	}
	
	// offset calculation functions	
	function getOffsetLeft(el) {
		var x = el.offsetLeft;
		if (el.offsetParent != null) {
			x += getOffsetLeft(el.offsetParent);
		}
		return x;
	}
	function getOffsetTop(el) {
		var y = el.offsetTop;
		if (el.offsetParent != null) {
			y += getOffsetTop(el.offsetParent);
		}
		
		return y;
	}
}