﻿
/*****************************************/
/***** Script Pagina Scheda Prodotto *****
/*****************************************/

/* Variabili globali */

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
var expanding = false;
var expandingWhishlist = false;
var attivo = false;
var attivoWhishlist = false;


/* Funzioni */


function checkCorrelazioniCategoria(ordine, categorie) {
    for (var i = 0; i < ordine; i++) {
        if ( $("#ctl00_ContentPlaceHolderCenter_MCVariante" + (i + 1)).val() != categorie[i].ID) return false;
    }
    return true;
}

function RemoveDuplicateSelect(idcombo)
{
    var usedNames = {};
    $(idcombo + " > option").each(function () {
        if (this.text != '') {
            if (usedNames[this.text]) {
                $(this).remove();
            } else {
                usedNames[this.text] = this.value;
            }
        }
    }); 
}

function CurrencyFormatted(amount) {
    var i = parseFloat(amount);
    if (isNaN(i)) { i = 0.00; }
    var minus = '';
    if (i < 0) { minus = '-'; }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if (s.indexOf('.') < 0) { s += '.00'; }
    if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
    s = minus + s;
    s = s.replace('.', ',');
    return s;
}

function monkeyPatchAutocomplete() {

    // Don't really need to save the old fn, 
    // but I could chain if I wanted to
    var oldFn = $.ui.autocomplete.prototype._renderItem;

    $.ui.autocomplete.prototype._renderItem = function(ul, item) {
        var re = new RegExp(this.term, "i");
        var t = item.label.replace(re, "<span style='font-weight:bold'><span style='text-decoration:underline'>" + this.term + "</span></span>");
        return $("<li></li>")
                  .data("item.autocomplete", item)
                  .append("<a>" + t + "</a>")
                  .appendTo(ul);
    };
}

function InizializzaEmailTextBox(id, txtNoEmail) {
    var ctl = document.getElementById(id);
    if (ctl.value == txtNoEmail) ctl.value = "";
}

function CheckEmailTextBox(id, text) {
    var ctl = document.getElementById(id);
    if (ctl.value == "") {
        ctl.value = text;
    }
}

function Ricerca(alertMsg) {
    var txtQuery = $('#ctl00_txtRicerca').val();
    if (txtQuery == '') {
        alert(alertMsg);
        return false;
    }
    else {
        return true;
    }
}

/* Inizializzazione widget jquery */

$(function() {

    $('#hlImgCarrello').click(function() {
        EspandiCarrello();
    });

    $('#hlImgWishList').click(function() {
        EspandiWhishList();
    });
});

$(document).ready(function() {
    $("#popupLogin").dialog({ autoOpen: false,
        width: 590,
        title: 'Login',
        open: function() { $(this).parent().appendTo("form"); },
        modal: true,
        resizable: false
    });
});

$(document).ready(function() {
    $("#ctl00_MenuMplaza").supersubs({
        minWidth: 3,
        maxWidth: 20,
        extraWidth: 1
    }).superfish({
        animation: { opacity: 'show' },   // slide-down effect without fade-in */
        delay: 0,              // 1.2 second delay on mouseout 
        speed: 0.5,
        autoArrows: false,
        dropShadows: false
    });
});

$(document).ready(function() {
    $("#listaPulsanti").superfish({
        animation: { opacity: 'show' },   // slide-down effect without fade-in */
        delay: 0,
        speed: 200,
        autoArrows: false,
        dropShadows: false

    });
});

/* Widgets */

/*********************/

$.extend({
    getUrlVars: function() {
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for (var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    },
    getUrlVar: function(name) {
        return $.getUrlVars()[name];
    }
});

function MM_openBrWindow(theURL, winName, features) { /* 2.0 */
    window.open(theURL, winName, features);
}

function MM_openBrWindowAutoSize(theURL, winName, features) {

    var img = new Image();
    var heightPopup;
    var widthPopup;

    img.src = theURL;
    img.onload = function () {
        heightPopup = img.height + 40;
        widthPopup = img.width + 40;
        window.open(theURL, winName, features + ',height=' + heightPopup + ',width=' + widthPopup)    
    };    
}

$(function() {
    $("#tabs").tabs();
});


$(function() {
    $("#tabs").css('visibility', 'visible');
    $("#tabs").css('position', 'relative');
});


function inizializzaScrollable()
{
    $("#RaccomandazioniScr").scrollable({
        disabledClass: "FrecciaDisabilitata",
        circular: true,
        onBeforeSeek: function () {
            $("#Raccomandazioni .paginaScrl")[0].innerHTML = this.getIndex() + 1;
            $("#Raccomandazioni .numPagineScrl")[0].innerHTML = this.getSize();
        },
        onSeek: function () {

            $("#Raccomandazioni .paginaScrl")[0].innerHTML = this.getIndex() + 1;
            $("#Raccomandazioni .numPagineScrl")[0].innerHTML = this.getSize();
        }
    });
};


/********************************************/
/**    Foto Gallery Prodotto e Varianti    **/
/********************************************/

$(function () {
    $(".thumbFotoProdotti img.thumbnail").click(function () {

        // see if same thumb is being clicked
        if ($(this).hasClass("active")) { return; }

        // calclulate large image's URL based on the thumbnail URL (flickr specific)
        var url = $(this).attr("src");

        // get handle to element that wraps the image and make it semi-transparent
        var wrap = $("#image_wrap");

        // the large image from www.flickr.com
        var img = new Image();

        // call this function after it's loaded
        img.onload = function () {

            // change the image
            wrap.find("img").attr("src", url);
            wrap.find("img").hide();
            wrap.find("img").fadeIn("fast");
        };

        // begin loading the image from www.flickr.com
        img.src = url;

        // activate item
        $(".thumbFotoProdotti ul img.thumbnail").removeClass("active");
        $(".thumbFotoProdotti ul img.thumbnail").addClass("deactivated");

        var idxFoto = 0;

        if ($("#Thumbnails").is(":visible")) {
            idxFoto = $("#Thumbnails  ul li").index(this.parentNode.parentNode) + 1;
        }
        else if ($("#ThumbnailsVarianti").is(":visible")) {
            idxFoto = $("#ThumbnailsVarianti  ul li").index(this.parentNode.parentNode) + 1;
        }

        $(".btnZoom").attr("href", url);
        $(".btnZoom").attr("title", "Foto " + idxFoto);

        $(this).removeClass("deactivated");
        $(this).addClass("active");

        // when page loads simulate a "click" on the first image
    });
});

//$(function() {
//    $(".thumbFotoProdotti img.thumbnail").mouseover(function () {

//        if ($(this).parents(':eq(3)').is(':visible'))
//            $("#image_wrap").find("img").attr("src", $(this).attr("src"));    
//        
//    });
//});

//$(function() {
//    $(".thumbFotoProdotti img.thumbnail").mouseout(function () {
//        if ($(this).parents(':eq(3)').is(':visible'))
//            $("#image_wrap").find("img").attr("src", $(this).parents(':eq(3)').find("img.active").attr("src"));
//    });
//});

/********************************************/
/**             Colori e Misure            **/
/********************************************/

$(function() {
    $("#taglie li.Udb ul.TagliaUdb li").click(function() {

        if ($(this).hasClass("active")) { return; }

        $("#tagliaslz").text(this.children[1].innerHTML + ' - ' + this.parentNode.parentNode.children[0].innerHTML);
        $("#ctl00_ContentPlaceHolderCenter_misuraSubmit").val(this.children[0].innerHTML);

        $("#taglie li.Udb ul.TagliaUdb li").removeClass("active");
        $(this).addClass("active");

        var idx = [$("#taglie li.Udb ul.TagliaUdb li").index(this)];

        $("#ctl00_ContentPlaceHolderCenter_colori li.GruppoColori").hide();
        $("#ctl00_ContentPlaceHolderCenter_colori li.GruppoColori:eq(" + idx + ")").show();
        $("#ctl00_ContentPlaceHolderCenter_colori li.GruppoColori:eq(" + idx + ") ul li").filter(":first").click();

    }).filter(":first").click();
});

$(function() {
    $("#ctl00_ContentPlaceHolderCenter_colori li.GruppoColori ul li").click(function() {
        if ($(this).hasClass("active")) { return; }
        $("#coloreSlz").text(this.children[0].alt);
        $("#ctl00_ContentPlaceHolderCenter_coloreSubmit").val(this.children[1].innerHTML);
        $("#ctl00_ContentPlaceHolderCenter_colori li.GruppoColori ul li").removeClass("active");
        $(this).addClass("active");
    }).filter(":first").click();
});

$(function() {
    $(".clsAnteprima").fancybox();
});

$(function () {
    $(".clsAnteprimaVarianti").fancybox();
});

function SelezionaDescrizione() {

    $("#tabs").tabs("select",0);
};

function SelezionaRecensioni() {

    $("#tabs").tabs("select", 1);

    if ($("#lnkCommentiUtenti a").children().length == 0) {
        if (!$("#btnAddReview a").hasClass("clicked")) {
            $("#ctl00_ContentPlaceHolderCenter_DivMsg").hide();
            $("#frmNewReview").slideToggle("slow");
            $("#btnAddReview a").addClass("clicked");
        }
    }
};

$(function() {
    $("#btnAddReview a").click(function() {
   
        if (!$(this).hasClass("clicked")) {
                $("#ctl00_ContentPlaceHolderCenter_DivMsg").hide();
                $("#frmNewReview").slideToggle("slow");
                $(this).addClass("clicked");
            }    

    });
});

$(function() {
    $(".frmline input").load(function() {

        if ($(this).hasClass("novalid")) $(this).removeClass("novalid");
    });
});

function chiudiGrafico() {
    if ($("#AreaGrafico").hasClass("chartVisible")) {
        $("#AreaGrafico").removeClass("chartVisible");
        $("#AreaGrafico").addClass("chartHidden");
    }
}

$(function() {
    $("#lnkRatingSummary").click(function() {
        $("#AreaGrafico").removeClass("chartHidden");
        $("#AreaGrafico").addClass("chartVisible");
    })
});

$(function() {
    $("#AreaGrafico").mouseleave(function() {
        chiudiGrafico();
    })
});

$(function() {
    $(".lnkChiudiGrafico").click(function() {
        chiudiGrafico();
    })
});

function AggiuntaProdottoInCarrello(){

    if ($("#ctl00_ContentPlaceHolderCenter_NonDisponibile").val() == null) {
        $("#carrello .lblAggiuntaInCorso").show();
        $("#carrello #ctl00_ContentPlaceHolderCenter_lnkToCarrello").hide();
        /*$('html').addClass("bodyopacity");*/
        $("#Loading").show();
    }
};

function FineAggiuntaProdottoInCarrello(){

    $("#Loading").hide();    
    $("#carrello #lblAggiuntaInCorso").hide();
    $("#ConfermaAggiuntaCarrello").fadeIn("fast");
    setTimeout(function() {
        $("#ConfermaAggiuntaCarrello").fadeOut("fast");
        $("#ctl00_btnCarrelloHome").addClass("sfHover");
        setTimeout(function() { $("#ctl00_btnCarrelloHome").removeClass("sfHover") }, 2000);
    }, 3000); 
}


function AggiuntaProdottoInWishlist() {

    if ($("#ctl00_ContentPlaceHolderCenter_NonDisponibile").val() == null) {

        $("#listadesideri .lblAggiuntaInCorso").show();
        $("#listadesideri #ctl00_ContentPlaceHolderCenter_lnkToWishlist").hide();
        /*$('html').addClass("bodyopacity");*/
        $("#Loading").show();
    }
};

function FineAggiuntaProdottoInWishlist() {

    /*$('html').removeClass("bodyopacity");*/
    $("#Loading").hide();
    $("#listadesideri .lblAggiuntaInCorso").hide();
    $("#ConfermaAggiuntaWishlist").fadeIn("fast");
    setTimeout(function() {
        $("#ConfermaAggiuntaWishlist").fadeOut("fast");
        $("#btnWishlistHome").addClass("sfHover");
        setTimeout(function() { $("#btnWishlistHome").removeClass("sfHover") }, 2000);
    }, 3000);
}

$(function() {
    var openReviewForm = $.getUrlVar("openformreview");
    if (openReviewForm == '1') {
        SelezionaRecensioni();
        window.location.href = window.location.href + '#tabs-2';
    }
    else
        SelezionaDescrizione();

});





