
/*	Expanding Menus for Indexhibit Framework.
 	Using jQuery 1.1.2
 	Autor Julien Pecorino / Date: 2 June 2010.
*/

$(document).ready(function(){

	var target 			= $("#menu ul");
	var speed 			= 300;
	
	/*--- Intenal var don't touch------- */
	var maxitems 		= target.length;
	var firstClick 		= false;
	var clickeditem		= Array();
	
	function init ()
	{
		for (var i=0; i< maxitems ; i++)
		{
			expandingMenu(i);
		}
	}
	
	function expandingMenu(num) 
	{
		/* init our items*/
		var item_title = target.eq(num).children(":first");
		var items = target.eq(num).children().filter(function (index) { return index > 0; });
			
		/* when reload page keep the htlm tag active visible */
		if (items.is(".active") == false)
		items.hide();
		else
		firstClick = true;
		
		/* add click functions + pointer*/
		item_title.css({cursor:"pointer"}).click
		(
			function () 
			{
				/* check if the same item is clicked*/
				if (clickeditem == item_title)
				{
					items.hide(speed);
					clickeditem = null;
					firstClick = false;
					return;
				}
				
				/* hide all items displayed then show */
				checkActive();
				
				/* need to check firstClick to avoid the delay at the first time you click on an item */
				if (firstClick == false)
				{
					items.show(speed);
				}
				else
				{
					var delay = speed+50;
					setTimeout(function(){items.show(speed)}, delay);
				}
				firstClick = true;
				clickeditem = item_title;
			}
		)
	}
	/* hidde all items displayed the loop is runned on every element this bad but it's the only stuff working!!! (because the entire page is reloaded each time you click on link)*/
	function checkActive()
	{
		for (var i=0 ; i< maxitems; i++)
		{
			var it = target.eq(i).children().filter(function (index) { return index > 0; });
			it.hide(speed);	
		}
	}
	
	init ();
	
});


