

var Navigation = 
{
	NAVIGATION_ID : 'navigation',
	SUB_MENU_CLASSNAME : 'menu',
	SUB_MENU_TITLE_CLASSNAME : 'menu_title',
	SUB_MENU_LINKS_CLASSNAME : 'menu_links',
	SUB_NAVIGATION_ID : 'subNavigation',

	/*****************************************************************
	  Initializes the navigation and creates the 
	  navigation tree structure.	 
	 *****************************************************************/
	init : function()
	{
		this.setEventHandlers('click');
		var params = document.location.href.toQueryParams();
		if(params.menu != undefined)
		{
			this.openMenu($(params.menu));
		}
		
		
		

	},

	/*****************************************************************
	  Adds specified event handler to all submenues.	 
	 *****************************************************************/
	setEventHandlers : function(eventType)
	{
		var subMenus = document.getElementsByClassName('menu',$(this.NAVIGATION_ID));
		for(var i = 0; i < subMenus.size(); i++)
		{
			Event.observe(subMenus[i],eventType,this.handleMenuEvent.bindAsEventListener(this));
		}
	},

	/*****************************************************************
	  Handles sub menu events, class openMenu() 
	 *****************************************************************/
	handleMenuEvent : function(event)
	{
		this.openMenu(Event.findElement(event, 'span'));
	},

	/*****************************************************************
	  Opens the menu for the specified menu element. 
	 *****************************************************************/
	openMenu : function(menuElement)
	{
		this.clear();
		var div = document.createElement("div");
		var subNav = $($( $(menuElement).immediateDescendants() )[1]).cloneNode(true);
		
		subNav.style.display = 'block';
		div.id = this.SUB_NAVIGATION_ID;
		div.appendChild(subNav);
		
		$(this.NAVIGATION_ID).appendChild(div);

		this.highlightMenu(menuElement);
	},
	
	/*****************************************************************
	  Clears out all displayed sub menus.
	 *****************************************************************/
	clear : function()
	{
		if($(this.SUB_NAVIGATION_ID) != undefined)
		{
			$(this.SUB_NAVIGATION_ID).remove();
		}
	},

	
	/*****************************************************************
	  Highlits the currently selected sub menu.
	 *****************************************************************/
	highlightMenu : function(menuElement)
	{
		var elements = $(this.NAVIGATION_ID).getElementsByTagName('a');
		$A(elements).each(function(item)
		{
			item.removeClassName('current');	
		})
		var child = menuElement.immediateDescendants()[0];
	
		$(child).addClassName('current');
	},
	
	/*****************************************************************
	   Finds the anchor which has the current url 
	   in its href property 
	 *****************************************************************/
	findSubMenuItemByURL : function()
	{
		var elements = $(this.NAVIGATION_ID).getElementsByTagName('a');
		var selectedItem = null;
		$A(elements).each(function(item)
		{
			var currentURL = document.location.href;
			if(currentURL == item.href && item.parentNode.hasClassName('menu_links'))
			{
				selectedItem = item;
			}
		});
		return selectedItem;
	},
	/*****************************************************************
	    Finds the menu element relative to the result of
		findItemByURL()
	 *****************************************************************/
	findMenu : function()
	{
		var currentAnchor = this.findSubMenuItemByURL();
		if(currentAnchor != null)
		{
			if(currentAnchor.parentNode.hasClassName(this.SUB_MENU_LINKS_CLASSNAME))
			{
				return currentAnchor.parentNode.parentNode;
			}
		}
		return null;
	}
	


	
}