function $(n){return document.getElementById(n)}
function RegisterNamespaces(){for(var i=0;i<arguments.length;i++){var p=arguments[i].split("."),w=window;for(var j=0;j<p.length;j++){if(!w[p[j]])w[p[j]]=new Object();w=w[p[j]];}}}
RegisterNamespaces("API.Menu");

API.Menu=new function()
{	
	// --- Config .:. Start ---
	var stepShow = 35; // co ile zmieniac wartosc przy pokazywaniu menu
	var stepHide = 25; // co ile zmieniac wartosc przy ukrywaniu menu
	var timeoutShow = 35; // co ile milisekund animacja pokazania menu
	var timeoutHide = 25; // co ile milisekund animacja ukrycia menu
	var timeoutDelay = 150; // po jakim czasie ukryc podmenu
	var finalOpacity = 1;
	// --- Config .:. End ---

	var objMenu = this;
	var contener;
	var timer = [];
	var timerHide = [];
	var progress = [];
	var max = [];

	// --------------- Czesc konfigurowalna .:. Start ----------------------

	// --- SlideFade .:. Start ---

	// SlideFade in
	this.showSubMenuSlideHide = function( i )
	{
		try 
		{
			var menu = getMenu(i);
			if(!progress[i])
			{
				progress[i] = 0;
				menu.style.visibility = "hidden";
				menu.style.display = "block";
				if(!max[i])
				{
				 if( menu.offsetHeight < 10 )
				 {
				  max[i] = 0;
				  for(k=0;k<menu.childNodes.length;k++)
				  {
				   if(menu.childNodes[k].offsetHeight)
				    max[i] += menu.childNodes[k].offsetHeight;
				  }
				 }
				 else max[i] = menu.offsetHeight;
				}
				menu.style.height = "0px";
				if( !window.opera )
				{
					if(document.all)
					{
						menu.filters.alpha.opacity = 0;
					}
					else
					{
						menu.style.MozOpacity = 0;
					}
				}
				menu.style.visibility = "visible";
			}
			progress[i] += stepShow;
			menu.style.height = (progress[i]<max[i]?progress[i]:max[i]) + "px";
			
			if( !window.opera )
			{
				opacity = (progress[i]/max[i]) * finalOpacity;
				if(document.all)
				{
					menu.filters.alpha.opacity = (opacity*100);
				}
				else
				{
					menu.style.MozOpacity = opacity;
				}
			}
			if(progress[i] >= max[i])
			{
				progress[i] = max[i];
				clearTimer(i);
			}
		}catch(e){}
	}

	// SlideFade out
	this.hideSubMenuSlideHide = function( i )
	{
		try 
		{
			var menu = getMenu(i);
			progress[i] -= stepHide;
			menu.style.height = (progress[i]>0?progress[i]:0) + "px";
			if( !window.opera )
			{
				opacity = progress[i]/max[i];
				if(document.all)
				{
					menu.filters.alpha.opacity = (opacity*100);
				}
				else
				{
					menu.style.MozOpacity = opacity;
				}
			}
			if(progress[i] <= 0)
			{
				progress[i] = 0;
				menu.style.display = "none";
				clearTimer(i);
			}
		}catch(e){}
	}
	
	// --- SlideFade .:. Stop ---

	// --- Funkcje dodatkowe .:. Start ---

	// pozwala sprawdzic czy dany UL jest podmenu
	function isSubMenu(oUl)
	{
		return ( "UL" == oUl.parentNode.parentNode.tagName ? true : false );
	}

	// wyciaga i-te podmenu
	function getMenu( i )
	{
		return  $(contener).getElementsByTagName("UL")[i];
	}

	// --- Funkcje dodatkowe .:. Stop ---

	// --------------- Czesc konfigurowalna .:. Stop ----------------------

	function clearTimer(i)
	{
		if(timer[i])
		{
			window.clearInterval(timer[i]);
			timer[i] = null;
		}
	}

	this.init = function( cont, animation )
	{
		try 
		{
			contener = cont;
			animation = animation || "SlideHide";
			var oUls = $(contener).getElementsByTagName("UL"),k,i;
			for( i=0; i<oUls.length; i++ )
			{
				if( isSubMenu(oUls[i]) )
				{
					oUls[i].parentNode.onmouseover = new Function('API.Menu.showSubMenu('+i+', "'+animation+'")');
					oUls[i].parentNode.onmouseout = new Function('API.Menu.hideSubMenu('+i+', "'+animation+'")');
					for(k=0; k<oUls[i].childNodes.length; k++)
					{
						if("LI" == oUls[i].childNodes[k].tagName)
						{
							oUls[i].childNodes[k].onmouseover = new Function('API.Menu.clearHideSubMenu('+i+')');
						}
					}
				}
			}
		}catch(e){}
	}

	this.showSubMenu = function( i, animation )
	{
		try 
		{
			clearTimer(i);
			objMenu.clearHideSubMenu(i);
			getMenu(i).style.zIndex = 100;
			timer[i] = window.setInterval('API.Menu.showSubMenu'+animation+'('+i+')', timeoutShow);
		}catch(e){}
	}

	this.hideSubMenu = function( i, animation )
	{
		try 
		{
			if(!timerHide[i])
			{
				clearTimer(i);
				timerHide[i] = window.setTimeout('API.Menu.hideSubMenuStart('+i+',"'+animation+'")', timeoutDelay);
			}
		}catch(e){}
	}

	this.hideSubMenuStart = function( i, animation )
	{
		objMenu.clearHideSubMenu(i);
		clearTimer(i);
		getMenu(i).style.zIndex = 50;
		timer[i] = window.setInterval("API.Menu.hideSubMenu"+animation+"("+i+")", timeoutHide);
	}

	this.clearHideSubMenu = function( i )
	{
		if(timerHide[i])
		{
			window.clearTimeout(timerHide[i]);
			timerHide[i] = null;
		}
	}
}	
