/*
mark@shift.co.nz | 03
 */

if (!document.getElementById)
	document.getElementById = function() { return null; }

var hideDelay = 1000;
var showDelay = 500;
var currentMenu = null;
var currentSubMenu = null;
var hideOut = null;
var hideSubOut = null;
var showTime = null;

function setMenu(menuId, buttonId, menuAlign) {
    var menu = document.getElementById(menuId);
    var button = document.getElementById(buttonId);

	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 (hideSubOut) {
			clearTimeout(hideSubOut);
		}
		if (currentSubMenu) {
            hideSubMenu();
        }
		if (currentMenu && menuAlign != "subright") {
			if (currentMenu && menuAlign != "subleft") {
			hideMenu(); 
            }
		}
		if (menuAlign=="right") {
			this.showMenuRight();
			showMenu();
		} else if (menuAlign=="left") {
			this.showMenuLeft();
			showMenu();
		} else if (menuAlign=="center") {
			this.showMenuCenter();
			showMenu();
		} 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);
		}
	}
	showMenu = function() {
		if (currentMenu) {
			currentMenu.style.display = "block";
		}
		if (currentSubMenu) {
			currentSubMenu.style.display = "block";
		}
		clearTimeout(showTime);
	}
	hideMenu = function() {
		currentMenu.style.display = "none";
		currentMenu = null;
	}
	hideSubMenu = function() {
		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";
		currentMenu = menu;
    }
	button.showMenuRight = function() {
        menu.style.left =  getOffsetLeft(this) + this.offsetWidth + "px";
        menu.style.top = getOffsetTop(this) + "px";
		currentMenu = menu;
    }
	button.showMenuCenter = function() {
        menu.style.left =  getOffsetLeft(this) + 1 + "px";
        menu.style.top = getOffsetTop(this) + this.offsetHeight + "px";
		currentMenu = menu;
    }
	
	// subMenu display methods	
	button.showSubMenuRight = function() {
	    menu.style.left = this.offsetLeft + (this.offsetParent.offsetWidth - 16) + "px";
		menu.style.top =  this.offsetTop -1 + "px";
		currentSubMenu = menu;
	}
	button.showSubMenuLeft = function() {
	    menu.style.left =  this.offsetLeft - (this.offsetParent.offsetWidth + 1) + "px";
		menu.style.top =  this.offsetTop - 10 + "px";
		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;
	}
}