/**
 * Simple nav display code
 * @copyright 2006 Toolbox Studios, Inc.
**/
var menuFlyout = {
	menutime:200,
	lastn:0,
	fadeInStep:10,
	fadeOutStep:10,
	fadeTime:50,
	subnavs:new Array(),

	showSubnav:function(menu) {
		for (var m = 0; m < this.subnavs.length; m++) {
			if (menu != m)
				this.fadeOut(this.subnavs[m]);
			else
				this.fadeIn(this.subnavs[m]);
		}
		this.lastn++;
	},

	hideSubnav:function() {
		this.lastn++;
		var currentTick = this.lastn;
		(function(){
			var tick = currentTick;
			setTimeout(function(){ menuFlyout.closeSubnav(tick); }, menuFlyout.menutime);
		}());
	},

	closeSubnav:function(tick) {
		 if (tick == this.lastn)
			 this.showSubnav(999);
	},

	fadeIn:function(obj) {
		if (obj) {
			if (typeof obj.fade == 'undefined')
				obj.fade = 0;
			obj.step = this.fadeInStep;
			if (obj.fade == 0)
				this.doFade(obj);
		}
	},

	fadeOut:function(obj) {
		if (obj) {
			if (typeof obj.fade == 'undefined')
				obj.fade = 0;
			obj.step = -this.fadeOutStep;
			if (obj.fade == 100)
				this.doFade(obj);
		}
	},

	doFade:function(obj) {
		if (obj) {
			// add/subtract current step amount
			obj.fade = Math.min(100,Math.max(0,obj.fade + obj.step));
			// set opacity
			this.setFade(obj,obj.fade);
			// Still more to go - timeout for a bit and keep going
			if (obj.fade<100 && obj.fade>0)
				setTimeout(function(){ menuFlyout.doFade(obj); },menuFlyout.fadeTime);
		}
	},

	setFade:function(obj,value) {
		// if completely faded, don't display
		obj.style.display = (value == 0 ? 'none' : 'block');
		// Set fade amount
		value = Math.min(90,value);
		// CSS3
		obj.style.opacity = Math.min(99.999,value/100) + '';
		// IE5.5+
		obj.style.filter = 'alpha(opacity=' + value + ')';
		// Gecko before CSS3 support
		obj.style.MozOpacity = Math.min(99.999,value/100) + '';
		// Konquerer and Safari
		obj.style.KHTMLOpacity = Math.min(99.999,value/100) + '';
	},
	
	hideitems:function(nodelist) {
		for (var n = 0; n < nodelist.length; n++)
			nodelist[n].style.display = 'none';
	},
	
	bind:function(ulId,subnavClassName) {
		if (typeof subnavClassName == 'undefined')
			subnavClassName = 'flyoutSubnav';
		var skipClassname = 'curMenu';
		var ul = (typeof ulId == 'string'
				? document.getElementById(ulId)
				: ulId
			);
		if (ul && ul.nodeName != 'UL') {
			var uls = ul.getElementsByTagName('ul');
			if (uls.length)
				ul = uls[0];
		}
		if (ul && ul.nodeName == 'UL') {
			var li = ul.firstChild;
			var index;
			var i;
			var submenu;
			var skipClass = new RegExp("(^|\\s)" + skipClassname + "(\\s|$)");
			while (li) {
				// Each menu item
				if (li.nodeName == 'LI') {
					if (li.className && skipClass.test(li.className)) {
						this.bind(li);
					}
					else {
						i = li.firstChild;
						index = this.subnavs.length;
						this.subnavs[index] = false;
						submenu = false;
						while (i) {
							// Have a submenu?
							if (i.nodeName == 'UL') {
								this.subnavs[index] = i;
								i.className = (i.className == '' ? '' : i.className + ' ') + subnavClassName;
								i.style.display = 'none';
								submenu = i;
								// If the submenu has submenus, prevent them from displaying (show only one leve)
								this.hideitems(i.getElementsByTagName('ul'));
								break;
							}
							i = i.nextSibling;
						}
						(function(){
							var submenuIndex = index;
							var ul = submenu;
							li.onmouseover = function(e) {
								if (ul) {
									// Find my current position
									var X = menuFlyout.pageX(this);
									var Y = menuFlyout.pageY(this);
									// Change to absolute and reposition
									ul.style.position = 'absolute';
									ul.style.left = X + 'px';
									ul.style.top = (Y + this.offsetHeight) + 'px';
									if (ul.parentNode != document.body) {
										menuFlyout.moveToEndOfBody(ul);
										(function(){
											var index = submenuIndex;
											ul.onmouseover = function(e) {
												menuFlyout.showSubnav(index);
											};
											ul.onmouseout = function(e) {
												menuFlyout.hideSubnav();
											}
										}());
									}
								}
								menuFlyout.showSubnav(submenuIndex);
							};
							li.onmouseout = function(e) {
								menuFlyout.hideSubnav();
							};
						}());
					}
				}
				li = li.nextSibling;
			}
		}
	},
	
	pageX:function(elem) {
//		alert(elem.nodeName + ' [id: ' + elem.id + '] = ' + elem.offsetLeft);
		return elem.offsetLeft + (elem.offsetParent ? this.pageX(elem.offsetParent) : 0);
	},
	pageY:function(elem) {
		return elem.offsetTop + (elem.offsetParent ? this.pageY(elem.offsetParent) : 0);
	},
	
	moveToEndOfBody:function(elem) {
		if (document && document.getElementsByTagName && document.getElementById && document.body)
			document.body.appendChild(elem);
		else
			setTimeout(function(){ this.moveToEndOfBody(elem); }, 50);
	}
}