var element;
var height;
var counterDown;
var htmlLink;
var counterUp;

var submenuElements;
var heightMenuItem
var	heightSubMenuItem=0;
var nSubMenuItem=0;
var activeMenu;


//initialize menu and var sizes
var speedUp=50;
var speedDown=30;
var navElement=document.getElementById("nav");
var menuElements=navElement.getElementsByTagName("li");

for (var i =0; i < menuElements.length; i++)
{
	submenuElements=menuElements[i].getElementsByTagName("li");
	if (submenuElements.length > 0)
	{
		activeMenu=menuElements[i];
		nSubMenuItem=submenuElements.length;
		heightSubMenuItem=submenuElements[0].offsetHeight;
		break;
	}
}
if (activeMenu)
{
	heightMenuItem=activeMenu.offsetHeight;
}
//if animate is true
if (typeof animate == "undefined" || animate=="true"){
	if (activeMenu)
	{
		showSubmenu(activeMenu,(nSubMenuItem+1)*heightSubMenuItem);
		rebuildLinks();
	}
}
else{
	rebuildLinks();
	if (activeMenu){
		openActiveMenu();
	}
}

//functions:
function rebuildLinks()
{
	var nav=document.getElementById("nav");
	var li=nav.getElementsByTagName("li");
	for (var i=0; i < li.length; i++)
	{
		if (li[i].parentNode==nav)
		{
			var a=li[i].getElementsByTagName("a");
			var linkString='<a href=\"#\" onclick=\'javascript: hideSubmenu(\"'+a[0].href+'\");\'>'+a[0].innerHTML+'</a>';
			li[i].removeChild(a[0]);
			li[i].innerHTML=linkString+li[i].innerHTML;
		}
	}
}
function showSubmenu(elem,h)
{
	if (h>0)
	{
		height=h-1;
		counterDown=activeMenu.offsetHeight;
		animateDown();
	}
}
function hideSubmenu(link)
{
	if(activeMenu){
		htmlLink=link;
		counterUp=parseInt(activeMenu.style.height);
		animateUp();
	}
	else{
		window.location=link;
	}
}
function animateDown()
{
	if (counterDown <= height){
		activeMenu.style.height=counterDown+"px";
		window.setTimeout("animateDown();",10);
		counterDown+=speedDown;
	}
	else{
		activeMenu.style.height=height+"px";
	}

}
function animateUp()
{
	if (counterUp >= heightMenuItem){
		activeMenu.style.height=counterUp+"px";
		window.setTimeout("animateUp();",10);
		counterUp-=speedUp;
	}
	else{
		activeMenu.style.height=heightMenuItem+"px";
		window.location=htmlLink;
		
	}
}
function openActiveMenu()
{
	activeMenu.style.height=(nSubMenuItem+1)*heightSubMenuItem+"px";
}


