var _arrCurrMenu = null;
var _submenuEnterCount = 0;
var _subsubmenuEnterCount = 0;

jQuery(document).ready(function()
{
	//aggiusto il letter-spacing del menù "social ice-cream"
	jQuery(".menu ul li a[hashname=social-ice-cream]").css("letter-spacing", "-1px");
//	jQuery("#box_sitemap ul li a[hashname=info-societarie]").addClass("disable");
//	jQuery("#box_sitemap ul li a[hashname=privacy-policy]").addClass("disable");
//	jQuery("#box_sitemap ul li a[hashname=contatti]").addClass("disable");
	

	//sistemo i click per le aperture dei menù/submenù
	jQuery(".menu").children("ul").children("li").children("a").each(function()
	{
		jQuery(this).click(function(event)
		{	
			if(isLoggato)
			{
			   $(".content_box_login").hide();
			   $(".content_box_logout").show();
			   
			   //metto visibile il menu dell'area riservata
			   $(".menu_area_riservata").show();
			   $(".menu_area_riservata .submenu").animate({
			   height: "29px"
			   },500);
			}
			
			//GESTISCO ECCEZIONE AREA RISERVATA se faccio il click sulla voce di menu area riservata ritorno 
			if(jQuery(this).parent().hasClass("menu_area_riservata"))return;
			
			if (jQuery(this).css("cursor") != "pointer")
				return;

			jQuery.historyLoad(jQuery.trim(jQuery(this).attr("hashName")));
		});
	});
	
	//aggancio i click per i submenù
	jQuery(".submenu ul li a").click(function(event)
	{
		event.stopImmediatePropagation();
		if (jQuery(this).css("cursor") != "pointer")
			return;

		if (jQuery(this).parent("li").parent("ul").parent("div").hasClass("submenu"))
			jQuery.historyLoad(jQuery.trim(jQuery(this).parents(".submenu").parent().children("a").attr("hashName")) + "/" + jQuery.trim(jQuery(this).attr("hashName")));
		else
		{
			jQuery.historyLoad(jQuery.trim(jQuery(this).parents(".submenu").parent().children("a").attr("hashName")) + "/" + jQuery.trim(jQuery(this).parents(".subsubmenu").parent().children("a").attr("hashName")) + "/" + jQuery.trim(jQuery(this).attr("hashName")));
		}
	});
	
	//aggancio i click sui menù presenti nel sitemap del footer
	jQuery("#box_sitemap ul li a").click(function()
	{
		if (jQuery(this).hasClass("disable"))
			return;


		if (jQuery(this).parent("li").parent("ul").parent("#box_sitemap").length > 0)
		{
			//menù di primo livello
			jQuery.historyLoad(jQuery.trim(jQuery(this).attr("hashName")));
		}
		else
		{
			//menù di secondo livello
			jQuery.historyLoad(jQuery.trim(jQuery(this).parents("li").parents("li").children("a").attr("hashName")) + "/" + jQuery.trim(jQuery(this).attr("hashName")));
		}
		jQuery(document).scrollTop(0);
	});
});

function backToHome()
{
	_currMenu = null;
	_currSubMenu = null;	
	jQuery.historyLoad("");
	jQuery(document).scrollTop(0);
}

function resetMenu()
{
	_currMenu = null;
	_currSubMenu = null;	
	jQuery(document).scrollTop(0);
}

function tagSearch(idTag)
{
	resetMenu();
	jQuery.historyLoad('tag/id=' + idTag);
}

function syncMenuWithUrl(arrHash)
{
	//ricerco il menù di primo livello associato
	var ret = new Object();
	ret.idMenuToLoad = 1;
	ret.idDetailToLoad = 0;
	ret.idProductToLoad = "";

	//controllo se ho un menù aperto...
	if (_arrCurrMenu != null && _arrCurrMenu.length > 0)
	{
		//devo chiudere il menù attivo
		//adesso provo a capire quale livello chiudere
		//ricerco il primo menù diverso da quello corrente
		var found = false;
		for (var i = 0; i < arrHash.length; i++)
		{
			if (i >= _arrCurrMenu.length || jQuery.trim(arrHash[i]) != jQuery.trim(_arrCurrMenu[i]))
			{
				//trovato!!!
				//chiudo da qui
				var precArrMenu = _arrCurrMenu;

				_arrCurrMenu = arrHash;
				closeMenu(precArrMenu, i);
				found = true;
				break; 
			}
		}
		
		if (!found)
		{
			//percorso completamente differente: chiudo dalla root
			var precArrMenu = _arrCurrMenu;
			_arrCurrMenu = arrHash;
			closeMenu(precArrMenu, 0);
		}
	}
	else if (_isPrimaVolta && jQuery.trim(arrHash[0]) != "")
	{
		//non sono in home quindi apro il menù corrente
		_arrCurrMenu = arrHash;
		openMenu(_arrCurrMenu, false);		
	}
	else if (jQuery.trim(arrHash[0]) != "")
	{
		//non sono in home quindi apro il menù corrente
		_arrCurrMenu = arrHash;
		openMenu(_arrCurrMenu, true);		
	}

	//ricerco l'id menù di 1° o 2° o 3° livello
	if (_arrCurrMenu != null && _arrCurrMenu.length > 0 && jQuery.trim(_arrCurrMenu[0]) != "")
	{
		//controllo se ho un Id per accedere ad un dettaglio	
		if (_arrCurrMenu[_arrCurrMenu.length - 1].indexOf("id=") != -1)
		{
			//ricavo id dettaglio
			ret.idDetailToLoad = parseInt(_arrCurrMenu[_arrCurrMenu.length - 1].substring("id=".length), 10);
			_arrCurrMenu.splice(_arrCurrMenu.length - 1, 1);
		}			
		//controllo se ho un IdProdotto per accedere ad un prodotto	
		else if (_arrCurrMenu[_arrCurrMenu.length - 1].indexOf("idp=") != -1)
		{
			//ricavo id prodotto
			ret.idProductToLoad = _arrCurrMenu[_arrCurrMenu.length - 1].substring("idp=".length);
			_arrCurrMenu.splice(_arrCurrMenu.length - 1, 1);
		}

		//ricerco l'id menu da caricare
		var idMnu = 0;
		if (_arrCurrMenu.length == 1)
			idMnu = jQuery(".menu ul li a[hashName=" + _arrCurrMenu[0] + "]").attr("idMenu");
		else if (_arrCurrMenu.length == 2)
			idMnu = jQuery(".menu ul li a[hashName=" + _arrCurrMenu[0] + "]").parent().find(".submenu ul li a[hashName=" + _arrCurrMenu[1] + "]").attr("idMenu");
		else if (_arrCurrMenu.length == 3)
			idMnu = jQuery(".menu ul li a[hashName=" + _arrCurrMenu[0] + "]").parent().find(".submenu ul li a[hashName=" + _arrCurrMenu[1] + "]").parent().find(".subsubmenu ul li a[hashName=" + _arrCurrMenu[2] + "]").attr("idMenu");;

		ret.idMenuToLoad = idMnu;
	}
	
	return ret;
}

function openMenu(pArrMenu, pAnimate)
{
	if (pArrMenu == null)
		return;


	/* @@@@ FINIRE
	if (jQuery(".menu").find("a[idMenu=" + _idMenuToLoad + "]").length > 0)
	{
		jQuery("#freccia_menu_attivo").css("top", jQuery(".menu").find("a[idMenu=" + _idMenuToLoad + "]").offset().top + "px");
		jQuery("#freccia_menu_attivo").show();
	}
	*/
				
				
	var firstLevelMenu = jQuery(".menu ul li a[hashName=" + pArrMenu[0] + "]");
	var firstLevelMenuAlreadyOpen = jQuery(firstLevelMenu).hasClass("menu_open");
	//if (!jQuery(firstLevelMenu).hasClass("menu_open"))
	{
		//gestisco eccesione sul menu dell'area riservata
		if(jQuery(firstLevelMenu).parent().attr("class") != "menu_area_riservata")
			jQuery(firstLevelMenu).addClass("menu_open");	
				
		//apro il sottomenù	(se esiste)
		
		var secondLevelMenu = null;
		if (pArrMenu.length > 1)
		{
			secondLevelMenu = jQuery(firstLevelMenu).parent().find(".submenu ul li a[hashName=" + pArrMenu[1] + "]");
		}

		if (!firstLevelMenuAlreadyOpen)
		{
			//metto fuori (a dx) tutti i sottomenù
			jQuery(firstLevelMenu).parent().children(".submenu").children("ul").children("li").children("a").each(function()
			{
				if (jQuery(this).attr("hashName") == jQuery(secondLevelMenu).attr("hashName"))
				{	
					//lo evidenzio
					if(jQuery(this).parent().parent().parent().parent().attr("class") != "menu_area_riservata")
						jQuery(this).addClass("submenu_open");
				}			
				jQuery(this).css("margin-right", -(jQuery(this).width() + parseInt(jQuery(this).css("padding-left"), 10) * 2) + "px");
			});
		
			//sposto i menù di primo livello per far posto ai submenù
			if (pAnimate)
				jQuery(firstLevelMenu).parent().find(".submenu").animate({height: jQuery(firstLevelMenu).parent().find(".submenu").children("ul").height() + "px"}, (jQuery(firstLevelMenu).parent().find(".submenu ul li a").length - 1) * 300, "linear");
			else
				jQuery(firstLevelMenu).parent().find(".submenu").css("height", jQuery(firstLevelMenu).parent().find(".submenu").children("ul").height() + "px");
	
			//faccio entrare ogni singolo submenù
			_submenuEnterCount = 0;
			jQuery(firstLevelMenu).parent().children(".submenu").children("ul").children("li").children("a").each(function()
			{
				_submenuEnterCount++;
				jQuery(this).attr("isEntered", "0");
			
				if (pAnimate)
				{
					jQuery(this).delay(_submenuEnterCount * DELAY_OPEN_MENU).animate({marginRight: "0px"}, DELAY_OPEN_MENU, "linear", function()
					{
						jQuery(this).attr("isEntered", "1");
						
						//controllo se ho fatto entrare tutti i sottomenù
						if (jQuery(firstLevelMenu).parent().children(".submenu").children("ul").children("li").children("a").length == jQuery(firstLevelMenu).parent().children(".submenu").children("ul").children("li").children("a[isEntered=1]").length)
						{
							openThirdMenu(jQuery(secondLevelMenu), pArrMenu, pAnimate);
						}
					});
				}
				else
				{
					jQuery(this).css("margin-right", "0px");
				}
			});
		}
		else
		{
			//setto il submenù
			jQuery(firstLevelMenu).parent().children(".submenu").children("ul").children("li").children("a[hashName=" + jQuery(secondLevelMenu).attr("hashName") + "]").addClass("submenu_open");
			
			if (pAnimate)
			{
				//controllo se devo aprire il menù di terzo livello...
				openThirdMenu(jQuery(secondLevelMenu), pArrMenu, pAnimate);
			}
		}
		
		if (!pAnimate)
			openThirdMenu(jQuery(secondLevelMenu), pArrMenu, pAnimate);
	}
}

function openThirdMenu(pParentMenu, pArrMenu, pAnimate)
{
	if (jQuery(pParentMenu).parent().find(".subsubmenu").length == 0 || jQuery(pParentMenu).parent().find(".subsubmenu ul li a[ghost=1]").length > 0)
		return;
		
	//apro il menù di terzo livello (se esiste)
	var thirdLevelMenu = null;
	if (pArrMenu.length > 2)
	{
		thirdLevelMenu = jQuery(pParentMenu).parent().find(".subsubmenu ul li a[hashName=" + pArrMenu[2] + "]");
	}
	
	var alreadyOpen = jQuery(pParentMenu).parent().find(".subsubmenu").height() > 0;
	
	if (!alreadyOpen)
	{
		//metto fuori (a dx) tutti i sottomenù di terzo livello
		jQuery(pParentMenu).parent().find(".subsubmenu ul li a").each(function()
		{
			if (jQuery(this).attr("hashName") == jQuery(thirdLevelMenu).attr("hashName"))
			{	
				//lo evidenzio
				jQuery(this).addClass("subsubmenu_open");
			}
			
			jQuery(this).css("margin-right", -(jQuery(this).width() + parseInt(jQuery(this).css("padding-left"), 10) * 2) + "px");
		});
	
		
		//sposto i menù di secondo livello per far posto ai submenù
		if (pAnimate)
		{
			//espando anche il menù superiore
			jQuery(pParentMenu).parents(".submenu").animate({height: (jQuery(pParentMenu).parents(".submenu").children("ul").height() + jQuery(pParentMenu).parent().find(".subsubmenu").children("ul").height()) + "px"}, (jQuery(pParentMenu).parent().find(".subsubmenu ul li a").length - 1) * 300, "linear");
			
			jQuery(pParentMenu).parent().find(".subsubmenu").animate({height: jQuery(pParentMenu).parent().find(".subsubmenu").children("ul").height() + "px"}, (jQuery(pParentMenu).parent().find(".subsubmenu ul li a").length - 1) * 300, "linear");
		}
		else
		{
			//apro anche il menù superiore
			jQuery(pParentMenu).parents(".submenu").css("height", (jQuery(pParentMenu).parents(".submenu").children("ul").height() + jQuery(pParentMenu).parent().find(".subsubmenu").children("ul").height()) + "px");	
	
			jQuery(pParentMenu).parent().find(".subsubmenu").css("height", jQuery(pParentMenu).parent().find(".subsubmenu").children("ul").height() + "px");
		}
		
		//faccio entrare ogni singolo submenù di terzo livello
		_subsubmenuEnterCount = 0;
		jQuery(pParentMenu).parent().find(".subsubmenu ul li a").each(function()
		{
			_subsubmenuEnterCount++;
		
			if (pAnimate)
				jQuery(this).delay(_subsubmenuEnterCount * DELAY_OPEN_MENU).animate({marginRight: "0px"}, DELAY_OPEN_MENU, "linear");
			else
			{
				jQuery(this).css("margin-right", "0px");
			}
		});
	}
	else
	{
		//seleziono il menù
		jQuery(thirdLevelMenu).addClass("subsubmenu_open");
	}
}

function closeMenu(pArrMenu, pCloseFromIndex)
{
	//comincio dal 3° livello
	if (pCloseFromIndex <= 2 && pArrMenu.length >= 3 && pArrMenu[2].indexOf("=") == -1 && pArrMenu[2] != _arrCurrMenu[2])
	{
		if (jQuery(".submenu_open").parent().find(".subsubmenu ul li a[ghost=1]").length == 0)
		{
			//chiudo il 3° livello
			_subsubmenuEnterCount = 0;
			jQuery(".subsubmenu_open").parent().find(".subsubmenu ul li a").each(function()
			{
				jQuery(this).delay((jQuery(".subsubmenu_open").parent().find(".subsubmenu ul li a").length - _subsubmenuEnterCount - 1) * DELAY_CLOSE_MENU).animate({marginRight: -(jQuery(this).width() + parseInt(jQuery(this).css("padding-left"), 10) * 2) + "px"}, DELAY_CLOSE_MENU, "linear");
				_subsubmenuEnterCount++;
			});
							
			jQuery(".subsubmenu_open").parent().find(".subsubmenu").delay(DELAY_CLOSE_MENU).animate({height: "0px"}, (jQuery(".subsubmenu_open").parent().find(".subsubmenu ul li a").length - 1) * DELAY_CLOSE_MENU, "linear", function(){
				//chiudo il 2° livello
				closeSecondLevelMenu(pArrMenu, pCloseFromIndex);
			});
							
			//rimuovo eventuali selezioni al menù di 3° livello
			jQuery(".subsubmenu ul li a").removeClass("subsubmenu_open");
			}
	}

	//chiudo il 2° livello
	closeSecondLevelMenu(pArrMenu, pCloseFromIndex);
}

function closeSecondLevelMenu(pArrMenu, pCloseFromIndex)
{
	//comincio dal 2° livello
	if (pCloseFromIndex <= 1 && pArrMenu.length >= 2 && pArrMenu[1].indexOf("=") == -1 && pArrMenu[1] != _arrCurrMenu[1])
	{
		if (jQuery(".submenu_open").parent().find(".subsubmenu ul li a[ghost=1]").length == 0)
		{
			//chiudo il 2° livello
			_submenuEnterCount = 0;
			jQuery(".submenu_open").parent().children(".subsubmenu").children("ul").children("li").children("a").each(function()
			{
				jQuery(this).delay((jQuery(".submenu_open").parent().children(".subsubmenu").children("ul").children("li").children("a").length - _submenuEnterCount - 1) * DELAY_CLOSE_MENU).animate({marginRight: -(jQuery(this).width() + parseInt(jQuery(this).css("padding-left"), 10) * 2) + "px"}, DELAY_CLOSE_MENU, "linear");
				_submenuEnterCount++;
			});
			
						
			jQuery(".submenu_open").parents(".submenu").delay(DELAY_CLOSE_MENU).animate({height: (jQuery(".submenu_open").parents(".submenu").height() - jQuery(".submenu_open").parent().children(".subsubmenu").children("ul").height()) + "px"}, (jQuery(".submenu_open").parent().children(".subsubmenu").children("ul").children("li").children("a").length - 1) * DELAY_CLOSE_MENU, "linear");
			jQuery(".submenu_open").parent().children(".subsubmenu").delay(DELAY_CLOSE_MENU).animate({height: "0px"}, (jQuery(".submenu_open").parent().children(".subsubmenu").children("ul").children("li").children("a").length - 1) * DELAY_CLOSE_MENU, "linear", function(){
				//chiudo il 1° livello
				closeFirstLevelMenu(pArrMenu, pCloseFromIndex);
			});
		}
						
		//rimuovo eventuali selezioni al menù di 2° livello
		jQuery(".submenu ul li a").removeClass("submenu_open");

		if ((jQuery(".submenu_open").parent().children(".subsubmenu").length == 0 || (jQuery(".submenu_open").parent().children(".subsubmenu").length > 0 && jQuery(".submenu_open").parent().find(".subsubmenu ul li a[ghost=1]").length > 0)) && pArrMenu[0] == _arrCurrMenu[0])
		{
			//continuo con l'apertura del nuovo menù
			openMenu(_arrCurrMenu, true);
		}
	}

	//chiudo il 1° livello
	closeFirstLevelMenu(pArrMenu, pCloseFromIndex);
}

function closeFirstLevelMenu(pArrMenu, pCloseFromIndex)
{
	//comincio dal 1° livello
	if (pCloseFromIndex == 0 && pArrMenu.length >= 1 && pArrMenu[0].indexOf("=") == -1 && pArrMenu[0] != _arrCurrMenu[0])
	{
		//chiudo il 1° livello
		jQuery(".menu_open").parent().find(".submenu").delay(DELAY_CLOSE_MENU).animate({height: "0px"}, (jQuery(".menu_open").parent().find(".submenu ul li a").length - 1) * DELAY_CLOSE_MENU, "linear");
	
		jQuery(".menu ul li a").removeClass("menu_open");
	}

	//continuo con l'apertura del nuovo menù
	openMenu(_arrCurrMenu, true);
}
