var active_menu = null;
var menu_intv = null;
var over_menu = null;
var current_menu = null;
var fade = [];

function init_menus()
{
	var menu = document.getElementById("menu");
	var i, len = menu.childNodes.length;
	var node, child, x, num_children;
	
	for (i = 0; i < len; i++)
	{
		node = menu.childNodes[i];
		
		if ( menu.childNodes[i].nodeType == 1 && menu.childNodes[i].tagName == "LI" )
		{
			menu.childNodes[i].onmouseover = menu_mouseover;
			menu.childNodes[i].onmouseout = menu_mouseout;
			menu.childNodes[i].onclick = menu_click;
			
			if ( menu.childNodes[i].className.indexOf("current") > -1)
				current_menu = menu.childNodes[i];
			
			child = menu.childNodes[i].getElementsByTagName("UL")
			
			if ( child.length > 0 )
			{
				menu.childNodes[i].submenu = child[0];
				child = child[0].getElementsByTagName("LI");
				
				num_children = child.length;
				for (x = 0; x < num_children; x++)
				{
					child[x].style.width = (menu.childNodes[i].offsetWidth - 10) +"px";  // -10px for left / right paddding and border (see default.css)
					child[x].onmouseover = child_mouseover;
					child[x].onmouseout = child_mouseout;
					//child[x].onclick = child_click;
				}
			}
		}
	}
}

function menu_mouseover(e)
{
	if ( typeof this.submenu != "undefined" )
	{
		over_menu = this;
		show_menu( this );
	} else {
		if ( active_menu ) hide_menu();
		
		this.className = "active";
	}
}

function menu_mouseout(e)
{
	if ( typeof this.submenu == "undefined" )
		this.className = null;
	
	over_menu = null;
}

function menu_click(e)
{
	e = getCommonEvent(e || event);
	
	if ( !e.target.getAttribute("href") )
	{
		var n;
		
		if ( (n = e.target.getElementsByTagName("A")[0]) )
		{
			if ( typeof n.onclick == "function" )
				n.onclick();
			else
				location.href = n.getAttribute("href");
		}
	} else {
		if ( typeof e.target.onclick != "function" )
			location.href = e.target.getAttribute("href");
	}
	
	return( false );
}

function child_mouseover(e)
{
	if ( this.className.indexOf("last") > -1 )
		this.className = "last over";
	else
		this.className = "over";
}

function child_mouseout(e)
{
	if ( this.className.indexOf("last") > -1 )
		this.className = "last";
	else
		this.className = null;
}

function show_menu(n)
{
	if ( active_menu == n )
		return;
	
	if ( active_menu ) hide_menu();
	
	n.className = "active";
	
	n.submenu.style.left = n.offsetLeft +"px";
	n.submenu.style.width = n.offsetWidth +"px";
	
	active_menu = n;
	
	menu_intv = setInterval(check_menu, 1500);
}

function hide_menu()
{
	clearInterval(menu_intv);
	
	fade_out(active_menu);
	
	//active_menu.className = null;
	active_menu = null;
}

function check_menu()
{
	if ( !active_menu && menu_intv )
		clearInterval(menu_intv);
	else if ( !over_menu || over_menu != active_menu )
		hide_menu();
}

function fade_out(n)
{
	if ( !n ) return;
	
	if ( typeof n.fade_intv != "undefined" )
		clearInterval( n.fade_intv );
	
	fade.push( n );
	n.fade_intv = setInterval("do_fadeout("+ (fade.length - 1) +")", 25);
}

function do_fadeout(index)
{
	var node = fade[index];
	var alpha, hop = 20;
	
	if ( isDOM )
	{
		if ( node.submenu.style.opacity.trim() == "" )
			alpha = 100;
		else
			alpha = parseFloat( node.submenu.style.opacity ) * 100 - hop;
		
		if ( alpha <= .01 )
		{
			if ( node == current_menu )
				node.className = "current";
			else
				node.className = null;
			
			node.submenu.style.opacity = 1;
			
			clearInterval( node.fade_intv );
			node.fade_intv = null;
		} else
			node.submenu.style.opacity = parseFloat(alpha / 100);
	} else {
		if ( node.submenu.style.filter.trim() == "" )
			alpha = 100;
		else
			alpha = parseInt( node.submenu.style.filter.replace(/[^\d]+/g, "") ) - hop;
		
		if ( alpha <= 0 )
		{
			if ( node == current_menu )
				node.className = "current";
			else
				node.className = null;
			
			node.submenu.style.filter = "alpha(opacity=100)";
			
			clearInterval( node.fade_intv );
			node.fade_intv = null;
		} else
			node.submenu.style.filter = "alpha(opacity="+ alpha +")";
	}
}

if ( isDOM )
{
	window.addEventListener("load", init_menus, false);
	document.addEventListener("mousedown", check_menu, false);
} else {
	window.attachEvent("onload", init_menus);
	document.attachEvent("onmousedown", check_menu, false);
}