/* menu */
//'/Menu/' + $("li#li_stillnovo").attr('file'),//      "../menu_stillnovo.htm",
			//url: "../menu_stillnovo.htm",
			
/*
error: function (XMLHttpRequest, textStatus) {
				menuStart();
}
*/
function menu() {
	if ($("li#li_stillnovo").size()) {
		$.ajax({
			url: '/stillnovomenu.asp', //../still_novo_new.htm 
			cache: true,
			success: function(html){
				$("li#li_stillnovo").after(html);
				$("li#li_stillnovo").remove();
				menuStart();
			},
			error: function(html){
				$("li#li_stillnovo").after(html);
				$("li#li_stillnovo").remove();
				menuStart();
			}
		});
	} else {
		menuStart();
	}
}

function menuStart() {
	
	/* pannelli sezione right */
	$("#right").attr("class",$("#menu").attr("class"));
		
	
	$("#menu ul.lev_1 li").addClass("li_1");
	$("#menu ul.lev_2 li").removeClass("li_1").addClass("li_2");
	$("#menu ul.lev_3 li").removeClass("li_2").addClass("li_3");
	$("#menu ul.lev_4 li").removeClass("li_3").addClass("li_4");
	$(".see_all").each(function(){
		$(this).remove();
	});
	$("#menu ul.lev_4 li img").attr("alt","");
	$("#menu ul.lev_3:not(:has(li))").parent().remove();
	
	/* modifiche immagini homefitness apertura secondo livello per Messico, Canada, USA */
	var paese = $("body").attr("id");
	var target = $("#menu").attr("class");
	if (target == "hf") {
		if (paese == "us" || paese == "ca" || paese == "mx") {
			$("li.li_2:eq(0) div.img_prev img").attr("src","http://www.technogym.com/media/immagini/481_t_wave0_a.jpg");
			$("li.li_2:eq(1) div.img_prev img").attr("src","http://www.technogym.com/media/files/010_36_abductor_diago_picc.jpg");
		}
	}
	
	/* prodotti nuovi per FIBO 21 aprile 2009 */
	//nuoviProdotti = ["2737","2742","2752","2763","2769","2779","3052","3430","3315","3829","3860","3856","4049","4061","4063"];
	//for (i = 0; i < nuoviProdotti.length; i++) {
	//	$("#menu li.li_4 a[href*='viewdoc.asp?co_id=" + nuoviProdotti[i] + "'] span").after("<em class='new_prod'>&nbsp; NEW!</em>");
	//}

	
	/* apertura 2° livello */
	var openMenu = false;
		
	$("#menu ul.lev_1 li a").not($("#menu ul.lev_2 li a")).not($("#menu li.close a")).click(function(){
		// show stillnovo homepage button
		if ($(this).next().find("li.home_stillnovo")) {
			$("li.home_stillnovo").show();
		}
																																																	 
		$("ul.lev_2").css({"height":"285px"});
		$("#menu ul.lev_1 li.close").css({"top":"288px"});
		openMenu = true;
		$("#menu ul.lev_2").hide();
		$("#menu ul.lev_3").hide();
		$("#menu li.close a").hide();
		$("#menu ul.lev_4").hide().find("li a").hide();
		$("#menu ul.lev_1 li a.on").removeClass("on");
		$(".cat_1,.cat_2,.cat_3,.cat_4,.cat_5,.cat_6,.cat_7,#ricerca_tipo").hide();			
		$(this).addClass("on").next().slideDown("normal", function(){
			$("#menu li.close a").show("normal");
		});
				
		/* se c'è solo 1 solo ul.lev_3 lo faccio vedere */
		if ($(this).next().find("ul.lev_3").size() == 1) {
			$(this).parent().find("ul.lev_2 li a:first").addClass("on").next().show();
			
			/* se non c'è il nome del livello ul.lev_3, alzo il livello ul.lev_4 */
			if ($(this).next().find("ul.lev_4").size() == 1 && $(this).next().find("li.li_3 a:not(:has(span))").text() == "") {
					for (var i=1; i<=$(this).next().find("ul.lev_3 li ul.lev_4").length; i++){
						$(this).next().find("ul.lev_3 li ul.lev_4:eq("+(i-1)+")").addClass("ul_"+i).css({marginTop:"-20px"}).show().find("li a").show();
					}
			} else {
				for (var i=1; i<=$(this).next().find("ul.lev_3 li ul.lev_4").length; i++){
					$(this).next().find("ul.lev_3 li ul.lev_4:eq("+(i-1)+")").addClass("ul_"+i).show().find("li a").show();
				}
			}
		}
		
		$(this).next().find("div.img_prev").show();
		
		
		/* conto quanti ul di 2° livello ci sono */
		var levsTwo = $(this).parent().find("li.li_3").size();
		 //self.status = levsTwo;
		
		switch (levsTwo) {
			case 1:
				$("#menu ul.lev_2").css({width:"656px"});
				$("#menu ul.lev_3").css({width:"656px"});
				$("#menu li.close").css({left:"645px"});
			break;
			case 2:
				$("#menu ul.lev_2").css({width:"656px"});
				$("#menu ul.lev_3").css({width:"656px"});
				$("#menu li.close").css({left:"645px"});
			break;
			case 3:
				$("#menu ul.lev_2").css({width:"656px"});
				$("#menu ul.lev_3").css({width:"656px"});
				$("#menu li.close").css({left:"645px"});
			break;
			case 4:
				$("#menu ul.lev_2").css({width:"656px"});
				$("#menu ul.lev_3").css({width:"656px"});
				$("#menu li.close").css({left:"645px"});
			break;
			case 5:
				$("#menu ul.lev_2").css({width:"656px"});
				$("#menu ul.lev_3").css({width:"656px"});
				$("#menu li.close").css({left:"645px"});
			break;
			default:
				$("#menu ul.lev_2").css({width:"995px"});
				$("#menu ul.lev_3").css({width:"995px"});
				$("#menu li.close").css({left:"984px"});
		}
		
		
	});
	
	
	/* CLOSE */
	$("#menu li.close a").hide();
	function closeMenu() {
		$("#menu ul.lev_2").slideUp("normal");
		$("#menu li.close a").hide();
		$("#menu div ul.lev_1 li a.on").removeClass("on");
		openMenu = false;
	}
	$("#menu li.close a").click(closeMenu);
	$("#body").click(function(){
		if (openMenu) {
			closeMenu();
		}
	});
	$("#footer").click(function(){
		if (openMenu) {
			closeMenu();
		}
	});
	$("#header").click(function(){
		if (openMenu) {
			closeMenu();
		}
	});
	
	/* controllo coordinate mouse per chiudere menu */
	var IE = document.all?true:false;
	if (!IE) document.captureEvents(Event.MOUSEMOVE)
	document.onmousemove = getMouseXY;
	var tempX = 0;
	var tempY = 0;
	var leftCol = 0;
	var rightCol = 0;
	function getMouseXY(e) {
		if (IE) { // grab the x-y pos.s if browser is IE
			tempX = event.clientX + document.body.scrollLeft;
			tempY = event.clientY + document.body.scrollTop;
		}	else {  // grab the x-y pos.s if browser is NS
			tempX = e.pageX;
			tempY = e.pageY;
		}  
		if (tempX < 0){tempX = 0;}
		if (tempY < 0){tempY = 0;}
		if ($("#container").size()){
			leftCol = document.getElementById("container").offsetLeft;
		}
		rightCol = leftCol + 996;
		//self.status = tempX + " / " + tempY + " /// " + leftCol + " - " + rightCol;
		return true;
	}
	$("body").click(function(){
		if (tempX < leftCol || tempX > rightCol) {
			if (openMenu) {
				closeMenu();
			}
		}
	});
	
	// Chiusura alla pressione del tasto ESC
	$("body").keypress(function (e) {
		if (openMenu) {
			if (e.which == 27) {
				closeMenu();
			}
		}
	});


	/* apertura 3° livello */
	$("#menu ul.lev_2 li a").not($("#menu ul.lev_3 li a")).click(function clickIt(){
		// nascondo stillnovo
		$("li.home_stillnovo").hide();


		// rollover link nel caso in cui il link sia diverso da #
		if ($(this).attr("href").indexOf("#") == -1) {
			$(this).css("text-decoration","underline");
			location.href = $(this).attr("href");
			return false;
		} else {
			$(this).css("cursor","default");
		}
		
		var levsTwo = $(this).parent().siblings().size()+1;
		var levsThree = $(this).parent().find("li.li_3").size();
		
		/*gestione ricerca x tipologie  in fondo menu prodotti*/
		$(".cat_1,.cat_2,.cat_3,.cat_4,.cat_5,.cat_6,.cat_7,#ricerca_tipo").hide();
		var nsel = $(this).parent().prevAll().size() + 1;
		$("#ricerca_tipo,.cat_" + nsel).show();
		var moreHeight = $("#ricerca_tipo").height(); // alteza #ricerca_tipo
		
		
		//self.status = levsThree;
		if (levsTwo < 5) {

			if (levsThree > 5) {
					$("#menu ul.lev_2").css({width:"984px"});
					$("#menu ul.lev_3").css({width:"984px"});
					$("#menu li.close").css({left:"973px"});
			} else if (levsThree = 5) {
					$("#menu ul.lev_2").css({width:"984px"});
					$("#menu ul.lev_3").css({width:"984px"});
					$("#menu li.close").css({left:"973px"});
			} else if (levsThree == 5) {
					$("#menu ul.lev_2").css({width:"820px"});
					$("#menu ul.lev_3").css({width:"820px"});
					$("#menu li.close").css({left:"809px"});
			} else if (levsTwo == 1) {
					$("#menu ul.lev_2").css({width:"656px"});
					$("#menu ul.lev_3").css({width:"656px"});
					$("#menu li.close").css({left:"645px"});
			} else {
					$("#menu ul.lev_2").css({width:"656px"});
					$("#menu ul.lev_3").css({width:"656px"});
					$("#menu li.close").css({left:"645px"});
			}
		
		}
				
		// recupero l'immagine di categoria per la preview
		catImg = $(this).next().find("img").attr("src");
		$("li.see_all img").attr("src",catImg);
		if ($("li.see_all a").attr("href")=="#") {
			$("li.see_all a").hide();
		} else {
			$("li.see_all a").show();
		}
		
		
		$("#menu ul.lev_2 li div.img_prev").find("img").css("border","2px solid #c5c5c5").parent().hide();
		$("#menu ul.lev_3").hide();
		$("#menu ul.lev_2 li a.on").removeClass("on");
		$(this).addClass("on").parent().find("ul.lev_3").show().find("ul.lev_4").show().find("li a").show();
		
		for (j=0; j<$(this).parent().find("ul.lev_3 li").not($("ul.lev_3 li ul li")).length; j++) {
			$(this).parent().find("ul.lev_3 li").not($("ul.lev_3 li ul li")).eq(j).addClass("li_"+(j+1));
		}
				
		for (i=0; i<$(this).parent().find("ul.lev_4").length; i++) {
			$(this).parent().find("ul.lev_4").eq(i).addClass("ul_"+(i+1));
		}
		
		// funzione nuovo menu
		var li3Height = 0;
		var numLi3 = $(this).parent().find("li.li_3").size();
		$(this).parent().find("li.li_3:lt(5)").each(function(){
			if ($(this).find("ul").height() > li3Height) {
				li3Height = $(this).find("ul").height();
			}
		});
		if (numLi3 > 6) {
			var li32Height = 0;
			$(this).parent().find("li.li_3:gt(5)").css({"margin-top":li3Height + 20 + "px", "border-top":"1px solid #e6e6e6"}).find("ul").css({"margin-top":li3Height + 41 + "px"});
			$(this).parent().find("li.li_3:gt(5)").each(function(){
				if ($(this).find("ul").height() > li32Height) {
					li32Height = $(this).find("ul").height();
				}
			});
			$("ul.lev_2").css({"height": (li32Height + parseInt($(this).parent().find("li.li_3:eq(6)").find("ul").css("margin-top")) + 90) + moreHeight + "px"});
			$("#menu ul.lev_1 li.close").css({"top":(li32Height + parseInt($(this).parent().find("li.li_3:eq(6)").find("ul").css("margin-top")) + 93 + moreHeight) + "px"});
		} else {
			if (li3Height < 285) {
				li3Height = 285;
			} else {
				li3Height += 70;
			}
			$("ul.lev_2").css({"height": li3Height + moreHeight + "px"});
			$("#menu ul.lev_1 li.close").css({"top":li3Height + 3 + moreHeight + "px"});
		}

	});
	
	
	
	// funzioni del link
	$("#menu ul.lev_2 li").not($("#menu ul.lev_3 li")).bind("mouseover",function(){
		if($(this).find(".img_prev:visible").size()){
			$(this).find("a:eq(0)").addClass("on");
			$(this).find(".img_prev").css({cursor:"pointer", textDecoration:"underline"}).find("img").css("border","2px solid #878787");
		}
	}).bind("mouseout",function(){
		if($(this).find(".img_prev:visible").size()){
			$(this).find("a:eq(0)").removeClass("on");
			$(this).find(".img_prev").css({cursor:"pointer", textDecoration:"none"}).find("img").css("border","2px solid #c5c5c5");
		}
	});


	// funzioni sull'immagine di preview del tab prodotti
	$("div.img_prev").css("cursor","pointer").click(function(){
//		$("#menu ul.lev_2 li div.img_prev").hide();
//		$(this).hide();
//		$("#menu ul.lev_3").hide();
//		$("#menu ul.lev_2 li a.on").removeClass("on");
//		$(this).parent().find("a:eq(0)").addClass("on").parent().find("ul.lev_3").show().find("ul.lev_4").show().find("li a").show();
		
		$(this).parent().find("a:eq(0)").click();
	});

	/* apertura 4° livello */
	$("#menu ul.lev_3 li a").not($("#menu ul.lev_4 li a")).each(function(){
		if ($(this).attr("href")=="#") {
			$(this).css({cursor:"default", textDecoration:"none"});
		}
	});
	
	
	
	/* swap image 4° livello */
	$("ul.lev_4 li a span").mouseover(function(){
		if($(this).parent().find("img").length != 0){
			//var srcImg = $(this).find("img").attr("src");
			//$("li.see_all img").attr("src",srcImg).show();
			var srcImg = $(this).parent().find("img");
			var realSrcImg;
			
			if (srcImg.attr("shadowsrc")) {
				realSrcImg = srcImg.attr("shadowsrc");
			} else {
				realSrcImg = srcImg.attr("src");
			}
			
			srcImg.attr("src",srcImg.attr("shadowsrc")).fadeIn("fast");
		}
	}).mouseout(function(){
			$(this).parent().find("img").hide();
	});
	
	$("ul.lev_4 li a img").mouseover(function(){
		$(this).hide();
	});
}

$(document).ready(function(){
	menu();
});

// funzione di caricamente delle immagini del menu all'onload della pagina
//window.onload = function () {
//	var res = "<img src='../img/menu/drop_menu.gif' /><img src='../img/menu/bg_menu_li.gif' /><img src='../img/menu/bg_prev_img.gif' />";
//	$("ul.lev_4 img").each(function(){
//		res = res + "<img src='" + $(this).attr("shadowsrc") + "' />";
//	});
//	$("body").append("<div id='postload'>" + res + "</div>");
//}
