/*
mark@shift.co.nz | 03
 */

if (!document.getElementById)
	document.getElementById = function() { return null; }

var hideDelay = 450;
var showDelay = 200;
var currentMenu = null;
var currentSubMenu = null;
var hideOut = null;
var hideSubOut = null;
var showTime = null;

function MenuList() {};

MenuList.prototype.set = function(menuId, buttonId, menuAlign) {
    var menu = document.getElementById(menuId);
    var button = document.getElementById(buttonId);
	var hselect = false;
	if (menu == null || button == null) return;
	if (document.all) {
		hselect = document.getElementsByTagName('select');
	}
	function hideSelect() {
		if (hselect) fixSelect('hidden');
	}
	function showSelect() {
		if (hselect) fixSelect('visible');
	}
	function fixSelect(val) {
		for(var s=1;s<hselect.length;s++) {
			hselect[s].style.visibility = val;
		}
	}
	
	if (menuAlign == "subright" || menuAlign == "subleft") {
		button.className = "menuOff";
	}
	
	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) hideMenu();
		if (menuAlign=="right") {
			(mode == 'narrow') ? this.showMenuRight() :	this.showMenuLeft();			
		} else if (menuAlign=="center") {
			(mode == 'narrow') ? this.showMenuCenter() : this.showMenuLeft();
		} else {
			this.showMenuLeft();
		}
		showMenu();
 	}
	
	button.onmouseout = function() {
		if (menuAlign == "subright" || menuAlign == "subleft") {
			hideSubOut = setTimeout("hideSubMenu();", hideDelay);
		} else {
			hideOut = setTimeout("hideMenu();", hideDelay);
		}
	}
	
	showMenu = function() {
		hideSelect();
		if (currentMenu) currentMenu.style.display = "block";
		if (currentSubMenu) {
			currentSubMenu.style.display = "block";
		}
		clearTimeout(showTime);
	}
	hideMenu = function() {
		showSelect();
		// fix to handle mouseout triggered before events load
		if (currentMenu) {
			currentMenu.style.display = "none";
			currentMenu = null;
		}
	}
	hideSubMenu = function() {
		currentSubMenu.style.display = "none";
		currentSubMenu = null;
	}
	
	button.showMenuLeft = function() {
        menu.style.left =  getOffsetLeft(this) + "px";
        menu.style.top = getOffsetTop(this) + this.offsetHeight + 1 + "px";
		currentMenu = menu;
    }
	button.showMenuRight = function() {
        menu.style.left =  (getOffsetLeft(this) + this.offsetWidth)-160 + "px";
        menu.style.top = getOffsetTop(this) + this.offsetHeight + 1 + "px";
		currentMenu = menu;
    }
	button.showMenuCenter = function() {
        menu.style.left =  getOffsetLeft(this) - Math.round((160-this.offsetWidth)/2) + "px";
        menu.style.top = getOffsetTop(this) + this.offsetHeight + 1 + "px";
		currentMenu = menu;
    }
	
	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;
	}
}

if (window.attachEvent) {
    var clearElementProps = ['onmouseover','onmouseout' ];
	// see http://youngpup.net/2005/0221010713
    window.attachEvent("onunload", function() {
        var el;
        for(var d = document.all.length;d--;){
            el = document.all[d];
            for(var c = clearElementProps.length;c--;){
                el[clearElementProps[c]] = null;
            }
        }
    });
}