/**
 * Class used to run the navigation of the site
 * @param UnorderedList list
 * @return void
 */
var Navigation = function(list)
{
	/**
	 * Unordered list, holds the root of the nav
	 */
	this.list = list;
	this.timeout = 500;
	this.closetimer	= 0;
	/**
	 * Holds a reference to the lastly opened sub nav
	 */
	this.ddmenuitem	= 0;
	
	var currentAnchor, tmpLi, subNav;
	var i;
	for(i = 0; i < this.list.childNodes.length; i++)
	{
		// Run all the li, and their first child (a)
		tmpLi = this.list.childNodes[i];
		if(tmpLi.tagName == "LI")
		{
			currentAnchor = tmpLi.getElementsByTagName("a")[0];
			addEvent(
					currentAnchor, 
					"mouseover", 
					createObjectCallback(this, this.OpenSubNav, [tmpLi]));
			addEvent(
					currentAnchor, 
					"mouseout", 
					createObjectCallback(this, this.StartClosingTimer));
			
			subNav = tmpLi.getElementsByTagName("ul")[0];
			if(subNav)
			{
				addEvent(subNav, "mouseover", createObjectCallback(this, this.CancelClosingTimer));
				addEvent(subNav, "mouseout", createObjectCallback(this, this.StartClosingTimer));
			}
		}
	}
};

//open hidden layer
Navigation.prototype.OpenSubNav = function(menuItem)
{
	// close old layer
	this.CancelClosingTimer();
	
	var newItem = menuItem.getElementsByTagName("ul")[0];
	
	if(newItem != this.ddmenuitem)
	{
		this.CloseSubMenu();
		
		// get new layer and show it
		this.ddmenuitem = newItem;
		if(this.ddmenuitem)
		{
			jQuery(this.ddmenuitem).slideDown(500);
		}
	}
};

// close showed layer
Navigation.prototype.CloseSubMenu = function()
{
	if(this.ddmenuitem) 
	{
		// cancel close timer
		jQuery(this.ddmenuitem).slideUp(500);
		this.ddmenuitem = null;
	}
};

// go close timer
Navigation.prototype.StartClosingTimer = function()
{
	this.closetimer = window.setTimeout(createObjectCallback(this, this.CloseSubMenu), this.timeout);
};

// cancel close timer
Navigation.prototype.CancelClosingTimer = function ()
{
	if(this.closetimer)
	{
		window.clearTimeout(this.closetimer);
		this.closetimer = null;
	}
};

