
	$(document).ready(function() {

		// Logout

		$(".logout-button").click( function () { $("#toploginform").submit();  })
		

		$('.acquisto-minimo').hide();
// simon, 16 feb 2011, da problemi di visualizzaizone
//
//		$('button').mouseenter( function () {
//			 $(this).parent().parent().children(".acquisto-minimo").show("fast");
//		});
//
//		$('button').mouseleave( function () {  $(this).parent().parent().children(".acquisto-minimo").hide("fast"); });

		// Mega banner
		// mette le immagini come sfondo per poter disegnare le curve

		$('.images-megabanner-home li a img').each (function () {

			 var url = $(this).attr("src");
			 $(this).parent().parent().css("background" , "transparent url(" + url + ") top left no-repeat");

		});

		// Carrello elettronico
		$('#look_cart_link').mouseenter(function () {
			$('#minicart-look').show();

				$.ajax({
					url: '/extera_mini_cart.php',
					success: function(data) {
						$('#minicart-look .content-cart').html(data);
						$('#minicart-look').css("background", "#fff");
					}
				});

		});

		$('#minicart-look').mouseleave(function () {

			$('#minicart-look .content-cart').empty();
			$('#minicart-look').hide();
			$('#minicart-look').css("background", "#fff");

		});



		// Menu principale
		// Sistema i menu a tendina nella homepage

		var spazio = 0;
		var somma_voci = 0;
		var spazioA = 0;
		$('.main_menu>ul>li:first-child').css({"border-left" : "none"});
		$('.main_menu>ul>li:last-child').css({"border-right" : "none"});

		var count_voci = $(".main_menu>ul>li").size();
		$('.main_menu>ul>li').each(function(i,e){

			var rientro_menu = 0;
			var is_double = $(this).children("div").children("div").children("div.col2").get();
			somma_voci += $(this).width();

			if (is_double != "") {
				$(this).children("div").children("div").addClass("double")
			}

			if (i > (count_voci-3)) {

				$(this).children("div").addClass("last");
				$(this).children("div").children("div").addClass("last");
			}

			if (i == (count_voci-1)) {
				$(this).addClass("last");
			}

		})

		if (category_id_ancestor != "") {
			somma_voci += 5
		}

		if (category_id_ancestor  != "" || category_id_parent != "" || category_id_current != "" ) {
			somma_voci += 14
		}

		spazio = (980 - somma_voci) / count_voci;
		spazio = Math.floor(spazio)/2;

		/* Calcolo il 4 che mi serve per fare il bordo */
		spazioA = spazio-4;

		$('.main_menu>ul>li>a, .main_menu>ul>li>ul>li').css({
			"padding-left" : (spazioA) +"px",
			"padding-right" : (spazioA) + "px"
		})

		// Selezioni menu
		$("span#category-" + category_id_ancestor).addClass("highlight");
		$("span#category-" + category_id_parent).parent().addClass("highlight_1");
		$("span#category-" + category_id_current).parent().addClass("highlight_1");
		$("span#category-" + category_id_current).parent().parent().addClass("noborder");

		// oggetti nei box

		// Aniamzione bottone aggiungi
		$("body").append("<div id='animazione'></div>");
		$('#animazione').hide();
		replace_bottoni();

		// Avvertimi quando un prodotto torna disponibile
		$(".alert_price").click(function () {
			$(".avvisami_form").fadeIn("fast");
			return false;
		})

		/* Layout navigation */
		/*('button.remove-filter').hide();*/

		$('a.altri-item').each( function () {

			$(this).click(function (event) {

				event.preventDefault();
				$(this).parent().css("display" , "none");

				layoutNavigationUpdate();

				$('#pannello-' + $(this).attr("col") + " ul.options-list li").each (function (index) {
					$(this).removeClass("nascosto");
				})

				layoutNavigationUpdate();


			});

		});



		$('.catalog .products-list .item:last').css("border-bottom" , "none");
		$('.onsale_display .products-list .item:last').css("border-bottom" , "none");
		$('#prodotti-relativi_d .upselling_product:last').css("border-bottom" , "none");

		// Controllo form avvisami
		$(".avvisami_form .button").bind ("click" , function(event) {

			event.preventDefault();

			var errore = false;
			$("#request_name").val() == "" ? errore = true : "";
			$("#request_email").val() == "" ? errore = true : "";

			if (!errore) {
				$(".avvisami_form #product_notify_price").submit();
			} else {
				$(".avvisami.alert").show();
			}


		});

		// Controllo form recensioni
		$("#recensioni_d .button").bind ("click" , function(event) {

			event.preventDefault();

			var errore = false;
			$("#review_author").val() == "" ? errore = true : "";
			$("#review_message").val() == "" ? errore = true : "";

			if (!errore) {
				$("#recensioni_d #form_reviews_form").submit();
			} else {
				$("#recensioni_d .alert").show();
			}


		});

		// Tabs prodotto

		$(".links-tab-product").bind("click" ,  function(event) {

			var this_id = $(this).attr("id");
			var salto = false;
			if (this_id == "recensioni_link")  {
				this_id = "recensioni";
				salto = true;
			}


			$(".dialog-tabs-product").removeClass("selected");
			$(".tab-button-product").removeClass("selected");

			$("#" +this_id +"_d.dialog-tabs-product").addClass("selected");
			$("#" +this_id +"_l.tab-button-product").addClass("selected");
			sistema_bottoni();

			if (!salto) {
				event.preventDefault();
			}

		})

		// More Products

		$(".more_products").click (function () {

			$(".upselling_more").hide();
			$(".upselling_product").addClass("up_products_visible")
			return false;

		});

		// Popup immagini
		$(".image-pop-up").mouseenter ( function () {
			$("#img-change").css("background" , "transparent url("+$(this).attr("href")+") top left no-repeat");
		});

		$(".image-pop-up").mouseleave ( function () {
			$("#img-change").css("background" , "transparent url("+$("#img-change").find("img").attr("src")+") top left no-repeat");
		});

		// disattivo il link alle immagini
		$(".image-pop-up").click
		(
			function (event)
			{
				event.preventDefault();
			}
		);



		// Ricerca
		// Sistema i risultati della ricerca che arrivano da ZOOM

		$("div.result_altblock, div.result_block").each(function (index) {

			$(this).children('.result_title').before('<div class="zoom_content"></div>');
			$(this).children('.result_title').appendTo( $(this).children(".zoom_content") );
			$(this).children('.context').appendTo( $(this).children(".zoom_content") );
			$(this).children('.infoline').appendTo( $(this).children(".zoom_content") );
			$(this).children('.zoom_content').after('<div class="clearing"></div>');

			if ($(this).children(".result_image").html() != null) {
				$(this).children(".zoom_content").addClass("small_item");
			}

		})

		$('img.result_image').each (function () {

			var links = $(this).attr("src");
			$(this).parent().parent().attr("style" , "background:transparent url("+ links + ") center center no-repeat;");
			$(this).parent().empty();

		});


		adjust_height_options();
		adjust_height_main();
		adjust_height_main_offerte();
		adjust_height_footer();
		layoutNavigationUpdate();
		adjust_height_convalida();

	});

	// Aggiusta la convalida

	function adjust_height_convalida () {
		var height_max = 0;
		height_max = $(".cart.convalida .cart-table-bonus").height()
		$(".cart.convalida .shipping-method").height() > height_max ? height_max = $(".cart.convalida .shipping-method").height() : "";
		$(".cart.convalida .cart-table-totals").height() >height_max ? height_max = $(".cart.convalida .cart-table-totals").height() : "";
		$(".cart.convalida .cart-table-bonus").height(height_max)
		$(".cart.convalida .shipping-method").height(height_max)
		$(".cart.convalida .cart-table-totals").height(height_max)
		$('.flc-container .cart-total-row').height(height_max);
	}

	// Aggiusta le opzioni menu

	function adjust_height_options () {

		var height_max = 0;
		$(".ridimensiona_1").each(function (i) {

			if (height_max<$(this).height() ) {
				height_max =$(this).height();

			}

		});

		adjust_height_option(height_max , "");

	}

	function adjust_height_option(h , txt) {
		$(".dim_op").each(function (i) {
			$(this).css("height" , h+"px");
		});
	}

	// Aggiusta la homepage

	function adjust_height_main_offerte() {

		var height_max = 0;
		$('.dialog.products-dialog.dialog-offer-list .item').each( function (i) {

			if (height_max<$(this).height() ) {
				height_max =$(this).height();
			}

		});

		$('.dialog.products-dialog.dialog-offer-list .item').height(height_max + "px");

		var height_max = 0;
		$('.dialog.products-dialog.dialog-featured-list .item').each( function (i) {

			if (height_max<$(this).height() ) {
				height_max =$(this).height();
			}

		});

		$('.dialog.products-dialog.dialog-featured-list .item').height(height_max + "px");

	}

	function adjust_height_main() {

		var height_max = 0;
		$(".count_height_1").each(function (i) {

			if (height_max<$(this).height() ) {
				height_max =$(this).height() + 15;
				adjust_height(height_max , "1");
			}

		});

		var height_max = 0;
		$(".count_height_2").each(function (i) {

			if (height_max<$(this).height() ) {
				height_max =$(this).height();
				adjust_height(height_max , "2");
			}

		});

	}

	function adjust_height(h , txt) {
		$(".dim_home_"+txt).each(function (i) {
			if (h > 60) {h=55}
			$(this).css("height" , h+"px");
		})
	}

	// Chiude il pannello del carrello elettronico

	function close_minicart() {
		$("#minicart-look").fadeOut('fast');
	}

	// Aggiunge l'interazione dei bottoni aggiungento un id a i bottoni delle pagina

	function replace_bottoni() {

		$('button').each(function (index) {
			$(this).attr("id" , "bottone" + index);
			$(this).css("display" ,"block");
		});

		adjust_height_options();
		adjust_height_main();
		sistema_bottoni();

	}

	// Aggiusta il footer
	/* Aggiustamento footer */

	function adjust_height_footer() {

		var heightLayOut = 0;

		$('#footer .footer-link div.ul').each ( function () {

			if ($(this).height() > heightLayOut) {
				heightLayOut = $(this).height();
			}

		});

		$('#footer .footer-link div.ul').css("height", heightLayOut  + "px");

	}

	// Aggiusta la lay - navigation

	function layoutNavigationUpdate() {

		var heightLayOut = 0;
		$('#navigazione-layout .option-layout').each ( function () { $(this).css("height", "auto"); });
		$('#navigazione-layout .option-layout').each ( function () {

			if ($(this).height() > heightLayOut) {
				heightLayOut = $(this).height();
			}

		});

		$('#navigazione-layout .option-layout').css("height", (heightLayOut+10)  + "px");
		$('#navigazione-layout').css("height", (heightLayOut+10)  + "px");

	}

	function sistema_bottoni() {
		$('button').each( function () {
				var me = $(this).children("span").children("span").width() + 40;
				$(this).width(me + "px");
			}
		);


	}

