/*
mark@shift.co.nz | 03
*/

if (!document.getElementById) document.getElementById = function() { return null; }
var agt = navigator.userAgent.toLowerCase();
var isMac = ((agt.indexOf("mac") != -1) && (agt.indexOf("msie") != -1));

var hideDelay = 450;
var currentMenu = null;
var currentButton = null;
var hideOut = null;
var showTime = null;

function MenuList() {};

MenuList.prototype.set = function(buttonId, menuId, 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;
		}
	}
	
	menu.onmouseover = function() {
		clearTimeout(hideOut);
	}
	menu.onmouseout = function() {
		hideOut = setTimeout('hideMenu()', hideDelay);
	}
	
	button.onmouseover = function() {
		if (hideOut) clearTimeout(hideOut);
		if (currentMenu) hideMenu();
		switch(menuAlign) {
			case 'right' : this.showMenuRight();  break;
			case 'center': this.showMenuCenter(); break;
			default: this.showMenuLeft(); break;
		}
		this.showMenu();
		currentButton = this;
 	}
	
	button.onmouseout = function() {
		hideOut = setTimeout('hideMenu()', hideDelay);
	}
	
	button.showMenu = function() {
		hideSelect();
		this.childNodes[0].className += ' current';
		if (currentMenu) currentMenu.style.display = "block";
		clearTimeout(showTime);
	}
	
	hideMenu = function() {
		showSelect();
		if (currentButton) {
			currentButton.childNodes[0].className = currentButton.childNodes[0].className.replace(' current', '');
		}
		currentButton = null;
		if (currentMenu) {
			currentMenu.style.display = "none";
			currentMenu = null;
		}
	}
	
	button.showMenuLeft = function() {
        menu.style.left =  getOffsetLeft(this) + "px";
        menu.style.top = getOffsetTop(this) + this.offsetHeight -4 + "px";
		currentMenu = menu;
    }
	
	button.showMenuRight = function() {
        menu.style.left =  (getOffsetLeft(this) + this.offsetWidth)-157 + "px";
        menu.style.top = getOffsetTop(this) + this.offsetHeight -4 + "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 -4 + "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);
		}
		if (isMac) y += 8;
		return y;
	}
}

// see http://youngpup.net/2005/0221010713
if (window.attachEvent) {
    var clearElementProps = ['onmouseover','onmouseout' ];
    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;
            }
        }
    });
}