 

// restituisce oggetto con variabili da url passato
function getUrlVaria(urlo){
	urlo = urlo +"";
	
    var obj = {};
	var varia;
	
    var variaAll = urlo.slice(urlo.indexOf('?') + 1).split('&');
    for(var i = 0; i < variaAll.length; i++){
        varia = variaAll[i].split('=');
		//alert(varia[0]+" "+varia[1]);
        obj[varia[0]] = varia[1];
    }
    return obj;
}


function ckMenuA(){
	var botto = $(this);
	var zonaSotto = $("#menu ul.menuTop");
	var allSottoMenu = $("#menu .menuTop li ul");
	var mioMenu =  botto.parent().find("ul").first();
	
	if(mioMenu.length >0){
		// tutti i sottomenu invisibili
		allSottoMenu.css("display","none");
		//il suo sottomenu visibile;
		mioMenu.css("display","block");
		//zonaSotto.height(150);
		var H = botto.height()+mioMenu.height();

		zonaSotto.clearQueue();
		zonaSotto.animate({height: H}, {duration:500,easing:"easeOutQuad"});
	}else{
		var padre = botto.parent().parent();
		var linko = botto.attr('href');
		var gid = linko.substr(1);
		
		var variaGet = getUrlVaria(window.location.href);
		var oldGid = "#" + variaGet["gid"];
		
		// se il padre del nuovo bottone contiene anche il vecchio eseguo centramento pag, altrimento salto alla nuova sezione
		var trovato = "NO";
		$.each(padre.children("li"), function(){
			if($(this).children("a").attr("href")==oldGid){
				trovato = "SI";
			}
		});
		
		
		if(trovato == "SI"){
			pag01_centroId(gid);
		}else{
			top.location.href = "index.php?gid="+gid+"&";
		}
		
		//top.location.href = "index.php?gid="+gid+"&";
	}

	

	//alert("A");
	// blocco esecuzione href
	return false;
 }
 
 
 
 
 function ckMenuB(){
	var botto = $(this);
	//var padre = botto.parent().parent().parent();
	var padre = botto.parent().parent();
	
	var linko = botto.attr('href');
	var gid = linko.substr(1);
	
	var variaGet = getUrlVaria(window.location.href);
	var oldGid = "#" + variaGet["gid"];
	
	// se il padre del nuovo bottone contiene anche il vecchio eseguo centramento pag, altrimento salto alla nuova sezione
	var trovato = "NO";
	$.each(padre.children("li"), function(){
		if($(this).children("a").attr("href")==oldGid){
			trovato = "SI";
		}
	});
	
	if(trovato == "SI"){
		pag01_centroId(gid);
	}else{
		top.location.href = "index.php?gid="+gid+"&";
	}
	
	
	
	/*
	var oldBotto = padre.find(("[href=#" +oldGid+ "]"));
	if(oldBotto.html()){
		pag01_centroId(gid);
	}else{
		top.location.href = "index.php?gid="+gid+"&";
	}
	*/
	// blocco esecuzione href
	return false;
 }
 
 
 
 
  function ckMenuC(){
	var botto = $(this);
	var padre = botto.parent();
	
	var linko = botto.find("a").attr('href');
	linko = linko.split("#");
	var gid = linko[1]+"";
	//var gid = linko.substr(1);
	
	var variaGet = getUrlVaria(window.location.href);
	var oldGid = "#" + variaGet["gid"];
	

	// se il padre del nuovo bottone contiene anche il vecchio eseguo centramento pag, altrimento salto alla nuova sezione
	var trovato = "NO";
	$.each(padre.children("li"), function(){
		var myId = $(this).children("a").attr("href");
		myId = myId.split("#");
		if("#"+myId[1]==oldGid){
			trovato = "SI";
		}
	});
	
	if(trovato == "SI"){
		pag01_centroId(gid);
	}else{
		top.location.href = "index.php?gid="+gid+"&";
	}

	// blocco esecuzione href
	return false;
 }
 
 
 
 /*
  function ckMenuC(){
	var botto = $(this);
	var padre = botto.parent();
	
	var linko = botto.find("a").attr('href');
	var gid = linko.substr(1);
	
	var variaGet = getUrlVaria(window.location.href);
	var oldGid = "#" + variaGet["gid"];
	

	// se il padre del nuovo bottone contiene anche il vecchio eseguo centramento pag, altrimento salto alla nuova sezione
	var trovato = "NO";
	$.each(padre.children("li"), function(){
		if($(this).children("a").attr("href")==oldGid){
			trovato = "SI";
		}
	});
	
	if(trovato == "SI"){
		pag01_centroId(gid);
	}else{
		top.location.href = "index.php?gid="+gid+"&";
	}

	// blocco esecuzione href
	return false;
 }
 */
 
 

 
 
 function rolloMenuOut(){
	var zonaSotto = $("#menu ul.menuTop");
	var primoBot = zonaSotto.find("li a").first();

	var H = primoBot.height();

	zonaSotto.clearQueue();
	zonaSotto.animate({height: H}, {duration:500,easing:"easeOutQuad"});
 }


 
// modifica la dimensione dei tasti del primo liv per fare entrare tutti
function sizeMenuLiv1(){
	//var box = $("#menu ul.menuTop");
	//var w = Math.floor(box.width()/box.children().length);
	//box.children().width(w);
	
	var menuPrimo = $("#menu ul.menuTop");
	var tastiLivA = menuPrimo.children();

	var totMenuW = 0;
	for(var i=0;i< tastiLivA.length;i++){
		totMenuW += $(tastiLivA[i]).width();
	}
	//var areaRestante = 950- (totMenuW+200);
	var areaRestante = 950- (totMenuW+50);
	
	var largoMargine = areaRestante/(tastiLivA.length-1);
	tastiLivA.css("margin-right", largoMargine+"px");
	tastiLivA.last().css("margin-right", "0px");
	
	// chiamo il tween che mi rende visibili i tasti primo livello
	$("#menu ul.menuTop").children().fadeIn(1000);
}






// crea il menu di terzo livello in ogni box
function creoMenuLiv3(){
	var barra = $("#barra_allpag");
	var allPag = barra.find(".box_onepag");
	
	$.each(allPag, function(){
		var idBox = $(this).attr("id");
		
		var botto = $("#menu .menuTop").find(('a[href=\"#' +idBox+ '\"]'));
		var livello = doContoLivelli(botto, 0);
		//alert(idBox+ " "+ botto.html()+" "+livello);
		// le voci di secondo livello -> 5, quelle di terzo livello -> 7
		switch ((livello+"")){
			case "5":
				var myHtm = botto.parent().children("ul").html();
				var foto = botto.parent().attr("ff");
				break;
			case "7":
				var myHtm = botto.parent().parent().html();
				var foto = botto.parent().parent().parent().attr("ff");
				break;
		}
		
		
		
		if(foto){
			var contenitore = $(this).find(".pag_menu3").children("div");
			var img = $(new Image());
			contenitore.append(img);
			img.attr("src",foto);
		}
		
		if(myHtm){
			$(this).find(".pag_menu3 ul").html(myHtm);
			$(this).find(".pag_menu3 ul li").unbind("click").click(ckMenuC);
			pag01_ridimensiono();
		}
		
	});
}




function doContoLivelli(botto, livello){
	if(botto.html()){
		livello++;
		
		var padre = botto.parent();
		if(padre.attr("id")== "menu"){
			return livello;
		}else{
			livello = doContoLivelli(botto.parent(), livello);
		}
		
	}
	return livello;
}



 
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
 
 
$(document).ready(function(){
	// rendo i tasti di primo livello invisibili
	$("#menu ul.menuTop").children().css("display","none");
	
	// faccio un tween fake perchè altrimenti sbaglia misure tasti primo livello
	$("#menu .menuTop").css("display","none").delay(100).fadeIn(1000,function(){
		$("#menu").unbind("mouseleave").mouseleave(rolloMenuOut);
		$("#menu .menuTop li a").unbind("click").click(ckMenuA);
		$("#menu .menuTop li ul li a").unbind("click").click(ckMenuB);
		creoMenuLiv3();
		sizeMenuLiv1();
		
	})
	
});
 
 
