var MENUS = new Array();
var SUBS = new Array();

var BUSY = false;

var mainId = "menu";
var menu = null;
var lock = null;

var ratio = 10;
var time = 5;

window.onload = function()
{
	menu = document.getElementById(mainId);
	lock = createElement('div');

	lock.setAttribute("id","blocker");
	lock.style.width = menu.offsetWidth;

	menu.parentNode.insertBefore(lock,menu);

	if (menu==null) return;

	var items = menu.childNodes;

	for(var i=0; i<items.length; i++)
	{
		var sub = items[i].lastChild;
		if(sub && (sub.tagName=="UL" || sub.tagName=="ul"))
		{
			// Get sub-level menu dimensions
			sub.height = sub.offsetHeight;
			sub.style.position = "relative";
			sub.style.visibility = "visible";
			sub.style.height = "0px";
			sub.style.display = "none";
			SUBS.push(sub);
			sub.thisMenu = SUBS.length - 1;

			// Assign onclick function
			MENUS.push(items[i]);
			items[i].isOpen = false;
			items[i].onclick = function()
			{
				toggleMenu(this);
			}
		}
	}

	// Shadow links
	items = menu.getElementsByTagName('a');

	var tmp = null;
	for(var i=0; i<items.length; i++)
	{
		tmp = createElement('div');
		tmp.innerHTML = items[i].innerHTML;

		if(items[i].parentNode.parentNode.getAttribute("id")==mainId)
		{
			tmp.className = "menuShadow topMenuShadowColor";
		}
		else
		{
			tmp.className = "menuShadow subMenuShadowColor";
			tmp.onmouseover = function () { this.className = "menuShadow subMenuHoverShadowColor"; };
			tmp.onmouseout = function () { this.className = "menuShadow subMenuShadowColor"; };
		}
		//items[i].parentNode.insertBefore(tmp,items[i]);

		// Disable linking for top level <a>'s whose parents have children
		var hasChild = items[i].parentNode.lastChild;
		if(hasChild && (hasChild.tagName=="UL" || hasChild.tagName=="ul"))
		{
			items[i].onclick = function () { return false; };
		}
	}
}

function toggleMenu(obj)
{
	if(BUSY) return;

	if(obj.isOpen)
	{
		closeMenu(obj.lastChild.thisMenu);
	}
	else
	{
		closeAllMenus();
		obj.className = "menuOpen";
		obj.lastChild.style.height = "0px";
		obj.lastChild.style.display = "block";
		openMenu(obj.lastChild.thisMenu);
	}
}

function closeMenu(i, increment, progress)
{
	var obj = SUBS[i];

	if(!obj.parentNode.isOpen)
	{
		return;
	}

	var h = obj.height;
	var pro = progress || 0;
	var inc = increment || (h/ratio) + 1;

	pro += inc;
	inc = ((h-pro)/ratio) + 1;

	var diff = h - pro;

	//if(pro>=h)
	if(diff<1)
	//if(pro>=147)
	{
		obj.style.height = "0px";
		obj.style.display = "none";
		obj.parentNode.className = "menuClosed";
		obj.parentNode.isOpen = false;
		return;
	}
	else
	{
		obj.style.height = diff + "px";
	}

	setTimeout('closeMenu('+i+','+inc+','+pro+')',time);
}

function openMenu(i, increment, progress)
{
	lockMenu();

	var obj = SUBS[i];
	var h = obj.height;

	var pro = progress || 0;
	var inc = increment || (h/ratio) + 1;

	pro += inc;
	inc = ((h-pro)/ratio) + 1;

	if(pro>=h)
	{
		obj.style.height = h + "px";
		obj.parentNode.isOpen = true;
		unlockMenu();
		return;
	}

	obj.style.height = pro + "px";

	setTimeout('openMenu('+i+','+inc+','+pro+')',time);
}

function closeAllMenus()
{
	for(var i=0; i<MENUS.length; i++)
	{
		MENUS[i].className = "menuClosed";
		closeMenu(MENUS[i].lastChild.thisMenu);
	}
}

function lockMenu()
{
	BUSY = true;
	lock.style.height = menu.offsetHeight + "px";
	lock.style.display = "block";

}

function unlockMenu()
{
	BUSY = false;
	lock.style.display = "none";
}

function createElement(element)
{
	if(typeof document.createElementNS!='undefined')
	{
		return document.createElementNS('http://www.w3.org/1999/xhtml', element);
	}

	if(typeof document.createElement!='undefined')
	{
		return document.createElement(element);
	}

	return false;
}