// global variables for timeout and for current menu
var t=false,current;
var menutimers = new Array();
var menutimern = 0;
var openmenuelement, opentime;
var hidemenuelement, hidetime;
//alert("betölt");
function SetupMenu() {
	//alert("setupmenu");
   if (!document.getElementsByTagName) return;
   items=document.getElementsByTagName("li");
   for (i=0; i<items.length; i++) {
      if (items[i].className != "menu") continue;
      //set up event handlers
	  //alert("almeülinkekbeállítása");
      thelink=findChild(items[i],"A");
      thelink.onmouseover=ShowMenu;
      thelink.onmouseout=StartTimer;
			//alert("eseménykezelők kész");
      //is there a submenu?
      if (ul=findChild(items[i],"UL")) {
         ul.style.display="none";
            ul.onmouseout=StartTimer2;
            ul.onmouseover=ResetTimer2;
			
		items2 = 0;
		
         for (j=0; j<ul.childNodes.length; j++) {
            ul.childNodes[j].onmouseover=ResetTimer;
			if(ul.childNodes[j].tagName == "LI") items2++;
			for (k=0; k<ul.childNodes[j].childNodes.length; k++) 
			{
				ul.childNodes[j].childNodes[k].onmouseover=ResetTimer3;
			}
         }
		 
		menutimers[menutimern] = {
			target: ul,
			status: "idle",
			link: thelink,
			timer: "",
			index: menutimern,
			height: 0,
			maxheight: (items2) * 25,
			openAnim : function()
			{
				if(this.status != "open") return;
				
				try
				{
					if(this.maxheight > 26)
					{
						time2 = parseFloat(this.target.style.opacity) + 0.08;
						if(time2 <= 0.9) this.target.style.opacity = time2;
					}
					else
					{
						this.target.style.opacity = 0.9;
					}
				}
				catch(e){}
				
				this.height += 11;
				this.target.style.height = this.height + "px";
				
				if(this.height < this.maxheight)
				{
					this.timer = window.setTimeout("openAnim(menutimers[" + this.index + "])", 15);
				}
				else 
				{
					try
					{
						this.target.style.opacity = 0.9;					
					}
					catch(e){}
					
					this.target.style.height = this.maxheight + "px";
					this.height = this.maxheight;
					this.status = "opened";
				}
			},
			hideAnim : function()
			{
				if(this.status != "closing") return;
				
				try
				{
					time2 = parseFloat(this.target.style.opacity) - 0.04;
					if(time2 >= 0)this.target.style.opacity = time2;				
				}
				catch(e){}
				
				
				this.height -= 8;
				if(this.height < 0) this.height = 0;
				this.target.style.height = this.height + "px";
				
				if(this.height > 0)
				{
					this.timer = window.setTimeout("hideAnim(menutimers[" + this.index + "])", 25);
				}
				else 
				{
					try
					{
						this.target.style.opacity = 0;
					}
					catch(e){}
					this.target.style.display="none";
					this.height = 0;
					this.status = "idle";
				}
			}
		};
		menutimern++;
      }
   } 
   //alert("setupmenu vége");
}

// find the first child object of a particular type
function findChild(obj,tag) {
   cn = obj.childNodes;
   for (k=0; k<cn.length; k++) {
     if (cn[k].nodeName==tag) return cn[k];
   }
   return false;
}

function ShowMenu(e) {
	//alert("showmenu");
   if (!e) var e = window.event;
   // which link was the mouse over?
   thislink = (e.target) ? e.target: e.srcElement;
   //ResetTimer();
   // hide the previous menu, if any
   if (current) HideMenu(current);
   // we want the LI, not the link
   thislink = thislink.parentNode;
   // find the submenu, if any
   var ul = findChild(thislink,"UL");
   if (!ul) return;
   ul.style.opacity = 0;
   ul.style.display="block";
   for(i = 0;i<menutimern;i++)
   {
	if(menutimers[i].target == ul)
	{
		if(menutimers[i].status == "idle" || menutimers[i].status == "closing")
		{
			current=menutimers[i];
			menutimers[i].status = "open";
			menutimers[i].target.style.height = 0
			menutimers[i].openAnim();
			//openAnim(menutimers[i], 0);
		}
	}
   }
}

function openAnim(element)
{
	element.openAnim();
}

function hideAnim(element, time)
{
	element.hideAnim();
}

function HideMenu(element) {
	//alert("hidemenu")
   // find the submenu, if any
   //ul = findChild(thelink,"UL");
   var ul = element.target;
   if (!ul) return;
   element.hideAnim();
   //hideAnim(element, element.target.style.opacity);
}

function ResetTimer2(e) {
   if (!e) var e = window.event;
   // which link was the mouse over?
   var ul = (e.target) ? e.target: e.srcElement;
   
	for(i = 0;i<menutimern;i++)
	{
		if(menutimers[i].target == ul)
		{
			//window.clearTimeout(t);
			try
			{
				window.clearTimeout(menutimers[i].timer);
			}
			catch(e2){}
		}
	}
}

function ResetTimer(e) {
   if (!e) var e = window.event;
   // which link was the mouse over?
   var li = (e.target) ? e.target: e.srcElement;
   
	for(i = 0;i<menutimern;i++)
	{
		if(menutimers[i].target == li.parentNode)
		{
			//window.clearTimeout(t);
			try
			{
				window.clearTimeout(menutimers[i].timer);
			}
			catch(e2){}
		}
	}
}

function ResetTimer3(e) {
   if (!e) var e = window.event;
   // which link was the mouse over?
   var li = (e.target) ? e.target: e.srcElement;
   
	for(i = 0;i<menutimern;i++)
	{
		if(menutimers[i].target == li.parentNode.parentNode)
		{
			//window.clearTimeout(t);
			try
			{
				window.clearTimeout(menutimers[i].timer);
			}
			catch(e2){}
		}
	}
}

function StartTimer() {
	//current.status = "closing";
	for(i = 0;i<menutimern;i++)
	{
		if(menutimers[i] == current)
		{
			menutimers[i].timer = window.setTimeout("firstHideMenu(menutimers[" + i + "])",200);
		}
	}
}

function StartTimer2() {
	//alert("ok");
	//current.status = "closing";
	for(i = 0;i<menutimern;i++)
	{
		if(menutimers[i] == current)
		{
			menutimers[i].timer = window.setTimeout("firstHideMenu(menutimers[" + i + "])",200);
		}
	}
}

function firstHideMenu(element) {
	//alert("hidemenu")
   // find the submenu, if any
   //ul = findChild(thelink,"UL");
   var ul = element.target;
   if (!ul) return;
	element.status = "closing";
   element.hideAnim();
   //hideAnim(element, element.target.style.opacity);
}
