/// <reference path="/JS/debug/jquery-1.6.1.min-vsdoc.js" />

/*
INIT MENU
--------------*/
jQuery.fn.initMenu = function ()
{
    return this.each(function ()
    {
        var theMenu = $(this).get(0);
        $('.acitem', this).hide();
        $('li.expand > .acitem', this).show();
        $('li.expand > .acitem', this).prev().addClass('active');
        $('li a', this).click(
            function (e)
            {
                e.stopImmediatePropagation();
                var theElement = $(this).next();
                var parent = this.parentNode.parentNode;
                if ($(parent).hasClass('noaccordion'))
                {
                    if (theElement[0] === undefined)
                    {
                        window.location.href = this.href;
                    }
                    $(theElement).slideToggle('normal', function ()
                    {
                        if ($(this).is(':visible'))
                        {
                            $(this).prev().addClass('active');
                        }
                        else
                        {
                            $(this).prev().removeClass('active');
                        }
                    });
                    return false;
                }
                else
                {
                    if (theElement.hasClass('acitem') && theElement.is(':visible'))
                    {
                        if ($(parent).hasClass('collapsible'))
                        {
                            $('.acitem:visible', parent).first().slideUp('normal',
                            function ()
                            {
                                $(this).prev().removeClass('active');
                            }
                        );
                            return false;
                        }
                        return false;
                    }
                    if (theElement.hasClass('acitem') && !theElement.is(':visible'))
                    {
                        $('.acitem:visible', parent).first().slideUp('normal', function ()
                        {
                            $(this).prev().removeClass('active');
                        });
                        theElement.slideDown('normal', function ()
                        {
                            $(this).prev().addClass('active');
                        });
                        return false;
                    }
                }
            }
    );
    });
};


/*  SLIDER - Pagina Interna
-------------------------------*/
function InitSlider()
{
    $("#slider").slider({
        value: 0,
        min: 0,
        max: $('#slider_cont .items').children('div').size() - 1,
        step: 1,
        animate: true,
        slide: function (event, ui)
        {
            $("#slider").data('stop', true);
            //setTimeout(function(){$( "#slider" ).data('stop',false);},7000);
        },
        change: function (event, ui)
        {
            $("#amount").val("$" + ui.value);
            var scrollable_div = $(".scrollable").scrollable();
            scrollable_div.seekTo(ui.value);
        }
    });
    $("#amount").val("$" + $("#slider").slider("value"));
}

/*  equalHeight
-------------------------------*/
function equalHeight(group) {
   tallest = 0;
   group.each(function() {
      thisHeight = $(this).height();
      if(thisHeight > tallest) {
         tallest = thisHeight;
      }
   });
   group.height(tallest);
}

/*  SLIDERDIVISION
-------------------------------*/
//function sliderDivision()
//{
//    var i = $("#label_barra ul li").size();
//    var c = Math.round($("#label_barra").width() / i);
//    var d = $("#label_barra ul li").size() - 1;
//    var e = Math.round(c / d);
//    var g = (e + c);
//	var h = Math.round(g * d);
//	var w = $("#label_barra").width();
//	if(h >= w) {
//		var h = g - 1;
//		$("#label_barra ul li").css("width", h, "px");
//		$("#label_barra ul li:last").css("width", "1px").addClass("last");
//	}
//	else {
//		$("#label_barra ul li").css("width", g, "px");
//		$("#label_barra ul li:last").css("width", "1px").addClass("last");
//	}
//}

function sliderDivision()
{
	
    if ($("#slider_cont")) {
		var i = $("#label_barra ul li").size();
		var c = parseInt ( ($("#label_barra").width() / i).toString().split ( ".")[0] );
		var d = $("#label_barra ul li").size() - 1;
		var e = parseInt ( ( c / d ).toString().split ( ".")[0] );
		var g = parseInt ( ( e + c ).toString().split ( ".")[0] );
		var h = parseInt ( ( g * d ).toString().split ( ".")[0] );
		var w = $("#label_barra").width();
		if ( (h >= w) ) {
			var h = g - 1;
			$("#label_barra ul li").css("width", h, "px");
			$("#label_barra ul li:last").css("width", "1px").addClass("last");
		} else {
			$("#label_barra ul li").css("width", g, "px");
			$("#label_barra ul li:last").css({'width':'1px'}).addClass("last");
		}
		
		if ( (i == 2) ) {
			$("#label_barra ul li:last").css({"width": "1px" , "margin-left":"0px"}).addClass("last");
		}									
	}
}

/* PRE-REG Community
---------------------*/
function PreRegCommunity(sender)
{
    $("#imgPreRegLoading").remove();
    $("#txtHpEmail").removeClass("red");

    var txt = $.trim($("#txtHpEmail").val());
    var rel = $("#txtHpEmail").attr("rel");
    var redirectOk = $("#hfCommunityURL").val();

    if (txt != rel & txt.length > 0)
    {
        if (!ValidateEmail(txt))
            $("#txtHpEmail").addClass("red");
        else
        {
            $(sender).after("<img id=\"imgPreRegLoading\" src=\"/img/loading.gif\" alt=\"loading\" />");
            $(sender).fadeOut(800);
            $.ajax({
                type: "POST",
                url: "/ws/wsCommunity.asmx/CheckEmail",
                cache: false,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: "{ email: '" + txt + "'}",
                success: function (data)
                {
                    if (data.d.Status)
                        location.href = (redirectOk + "?email=" + txt);
                    else
                    {
                        $(sender).after("<span>" + data.d.HTML + "</span>");
                        $("#imgPreRegLoading").remove();
                        $(sender).fadeIn(500);
                    }
                },
                error: function (data)
                {
                    $("#imgPreRegLoading").remove();
                    $(sender).fadeIn(500);
                    alert("Web-Service Alert!");
                }
            });
        }
    }
    else
    {
        location.href = redirectOk;
    }
    return false;
}

/* Validate Email
---------------------*/
function ValidateEmail(email)
{
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    return reg.test(email);
}


/*  READY
----------------*/
$(function ()
{
    if ($("#setBackground").size())
    {
        var img = $("#setBackground").val();
        if (img != null && img != "")
        {
            $("body").css("background-repeat", "no-repeat");
            $("body").css("background-position", "top");
            $("body").css("background-image", "url(" + img + ")");
        }
    }

    // Classe EXPAND sull'Elemento Corrente nel Menù della Scheda Prodotto
    var mProdCa = $("#MenuProdottoCA").val();
    $("#MenuP" + mProdCa).addClass("expand");
    var href = $("#MenuP" + mProdCa).children()[0]
    $(href).addClass("active");

    // Classe ON sul Contenuto Corrente nel Menù della Scheda Prodotto
    var mProdCo = $("#MenuProdottoCO").val();
    $("#SubMenu" + mProdCo).addClass("on");

    // Init Menu
    $('.menu').initMenu();

    //footer
    $("#footer ul li:last-child").css("background", "none");
    $("#footer ul li:first-child").css("padding-left", "0px");

    // NAV
    $("li#tendina").hover(function () { $(this).find('ul').show(); }, function () { $(this).find('ul').hide(); });
    $("li#tendina ul").hover(function () { $(this).show(); $(this).prev().addClass('attivo'); });

    // Classe ON sull'Elemento Corrente del Menù
    var currCa = $("#CurrCA").val();
    $("#MenuCa" + currCa).addClass("on");

    // Nella Lista Prodotti, il primo <div> deve avere classe 'border-left'
    if ($(".prodotto").size())
    {
        var tot = $(".prodotto").size();
        for (var i = 0; i < tot; i = i + 2)
        {
            $(".prodotto:eq(" + i.toString() + ")").addClass("border-left");
        }
    }

    // Init Slider - Pagina Interna
    InitSlider();
    sliderDivision();
	equalHeight($(".art"));

    // classe 'last' nell'ultimo Box
    $(".art").last().addClass("last");

    // class 'art_evident' all Box corrente
    var idArt = "art" + $("#currArt").val();
    $("#" + idArt).addClass("art_evident");

    // Zoom Bottiglia in Scheda Prodotto
    $(".fancyZoomBottle").fancybox({ 'titleShow': false });
    // Zoom immagine in News
    $(".newsFancyZoom").fancybox({ 'titleShow': false });
    // Fancy Corpo News
    $(".newsFancyBody").fancybox({ 'titleShow': false, 'hideOnContentClick': false });
    // Fancy Video News
    $(".newsFancyVideo").fancybox({ 'type': 'iframe', 'width': 510, 'height': 410, 'padding': 0, 'titleShow': false, 'hideOnContentClick': false });

    // Community
    $("#btnHpRegCommunity").click(function () { return PreRegCommunity(this); });
    $("#txtHpEmail").focus(function () { $(this).val(""); });
    $("#txtHpEmail").blur(function () { var txt = $.trim($(this).val()); if (txt.length <= 0) { $(this).val($(this).attr("rel")); } });
    $("#spPrivacy").fancybox({ 'autoDimensions': false, 'width': 980 });

    if ($("#frmRegistrazione").size())
    {
        var email = $.query.get("email");
        if (email != null && email.length > 0)
        {
            $("#txtEmail").val(email);
            $("#txtEmail").attr("disabled", "disabled");
        }
    }

    if ($("#selectNazione").size())
    {
        idNazione = $("#selectNazione").val();
        $("#ddlNazione").val(idNazione);
    }

    $("#submenu div:eq(4)").addClass("last");

    $("#btnCerca").click(function ()
    {
        var txt = $("#txtCerca").val();
        if (txt != null && txt.length > 0)
        {
            baseUrl = $("#hfRedirectUrl").val();
            location.href = baseUrl + "?search=" + txt;
        }
        return false;
    });
	displayNon();
	equalHeight($(".art .as_p.text_reg"));
	
	
});
	
function equalHeight(group) {
   tallest = 0;
   group.each(function() {
      thisHeight = $(this).height();
      if(thisHeight > tallest) {
         tallest = thisHeight;
      }
   });
   group.height(tallest);
}
	

function displayNon() {
	$("#footer ul li:eq(5)").css("display","none");
	$("#footer ul li:eq(6)").css("display","none");	
	$("#spPrivacy").css("display","none");
}
