﻿///////////////////////////////////////////
// recupero idNews se presente
///////////////////////////////////////////
jQuery(document).ready(function() {
    var idNews = 0;
    var url = window.location.href;

    // controllo se sono in pagina delle news
    if (url.indexOf("/news") > -1) {
        // recupero eventuale idNews
        idNews = url.substring(url.indexOf("/news") + 6, url.length);
        try {
            idNews = parseInt(idNews);
            // mi ricavo quindi tutti di div all'inerno del repater delle news
            var divs = document.getElementsByTagName("div");

            // ciclo tutti gli elementi dell'array 
            for (i = 0; i < divs.length; i++) {
                // controllo se l'elemento in questione contiene la parola dettagli
                if (divs[i].id.toString().indexOf("dettNews_" + idNews) >= 0) {
                    jQuery("#" + divs[i].id.toString()).toggle(1000); // e lo rendo visibile 
                }
            }
        }
        catch (e) { }
    }
});

jQuery(function() {
    jQuery("#slides").slides({
        pagination: true
    });
});

///////////////////////////////////////////////////////
// funzione chiamata al body onload della master page
///////////////////////////////////////////////////////
function checkToggle() {
    // può tornare utile per recuperare la url... intanto commentata
    //var toggle = getUrlVars()["menu"];

    var toggle = getUrl();
    toggleMenu(toggle, 0);
}

///////////////////////////////////////////////////////
// funzione per recuperare parametri dalla url... 
// non usata ma può tornare utile
///////////////////////////////////////////////////////
function getUrlVars() {
    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;
}

///////////////////////////////////////////////////////
// funzione per gestire l'apertura del menu 
// laterale anche sulle altre pagine
///////////////////////////////////////////////////////
function getUrl() {
    var hashes = window.location.href;
    if (hashes.indexOf("_t") > -1) return 1;
    if (hashes.indexOf("_c") > -1) return 2;
    if (hashes.indexOf("_r") > -1) return 3;
    if (hashes.indexOf("_v") > -1) return 4;
    if (hashes.indexOf("_g") > -1) return 5;
    return 4;
}

///////////////////////////////////////////////////////
// funzione per gestione animazione del menu
// 1: TipoVino
// 2: Cantina
// 3: Regione
// 4: Vino 
// 5: Gastronomia
///////////////////////////////////////////////////////
function toggleMenu(qualeMenu, secondi) {
    switch (qualeMenu) {
        case 1:
            jQuery("#ulTipoVino").toggle(secondi);
            if (document.getElementById("imgArrowTV").src.indexOf("up_arrow.png") > -1) {
                jQuery("#imgArrowTV").attr("src", "/public/images/generic/down_arrow.png");
                jQuery("#lblTipoVino").css("color", "#f4f3ef");
            }
            else {
                jQuery("#imgArrowTV").attr("src", "/public/images/generic/up_arrow.png");
                jQuery("#lblTipoVino").css("color", "#ffcc00");
            }
            jQuery("#ulVino").hide(secondi);
            jQuery("#imgArrowV").attr("src", "/public/images/generic/down_arrow.png");
            jQuery("#lblVino").css("color", "#f4f3ef");
            jQuery("#ulCantina").hide(secondi);
            jQuery("#imgArrowC").attr("src", "/public/images/generic/down_arrow.png");
            jQuery("#lblCantinaVino").css("color", "#f4f3ef");
            jQuery("#ulRegione").hide(secondi);
            jQuery("#imgArrowR").attr("src", "/public/images/generic/down_arrow.png");
            jQuery("#lblRegioneVino").css("color", "#f4f3ef");
            jQuery("#ulGastronomia").hide(secondi);
            jQuery("#imgArrowG").attr("src", "/public/images/generic/down_arrow.png");
            jQuery("#lblGastronomia").css("color", "#f4f3ef");
            break;
        case 2:
            jQuery("#ulCantina").toggle(secondi);
            if (document.getElementById("imgArrowC").src.indexOf("up_arrow.png") > -1) {
                jQuery("#imgArrowC").attr("src", "/public/images/generic/down_arrow.png");
                jQuery("#lblCantinaVino").css("color", "#f4f3ef");
            }
            else {
                jQuery("#imgArrowC").attr("src", "/public/images/generic/up_arrow.png");
                jQuery("#lblCantinaVino").css("color", "#ffcc00");
            }
            jQuery("#ulVino").hide(secondi);
            jQuery("#imgArrowV").attr("src", "/public/images/generic/down_arrow.png");
            jQuery("#lblVini").css("color", "#f4f3ef");
            jQuery("#ulTipoVino").hide(secondi);
            jQuery("#imgArrowTV").attr("src", "/public/images/generic/down_arrow.png");
            jQuery("#lblTipoVino").css("color", "#f4f3ef");
            jQuery("#ulRegione").hide(secondi);
            jQuery("#imgArrowR").attr("src", "/public/images/generic/down_arrow.png");
            jQuery("#lblRegioneVino").css("color", "#f4f3ef");
            jQuery("#ulGastronomia").hide(secondi);
            jQuery("#imgArrowG").attr("src", "/public/images/generic/down_arrow.png");
            jQuery("#lblGastronomia").css("color", "#f4f3ef");
            break;
        case 3:
            jQuery("#ulRegione").toggle(secondi);
            if (document.getElementById("imgArrowR").src.indexOf("up_arrow.png") > -1) {
                jQuery("#imgArrowR").attr("src", "/public/images/generic/down_arrow.png");
                jQuery("#lblRegioneVino").css("color", "#f4f3ef");
            }
            else {
                jQuery("#imgArrowR").attr("src", "/public/images/generic/up_arrow.png");
                jQuery("#lblRegioneVino").css("color", "#ffcc00");
            }
            jQuery("#ulVino").hide(secondi);
            jQuery("#imgArrowV").attr("src", "/public/images/generic/down_arrow.png");
            jQuery("#lblVini").css("color", "#f4f3ef");
            jQuery("#ulCantina").hide(secondi);
            jQuery("#imgArrowC").attr("src", "/public/images/generic/down_arrow.png");
            jQuery("#lblCantinaVino").css("color", "#f4f3ef");
            jQuery("#ulTipoVino").hide(secondi);
            jQuery("#imgArrowTV").attr("src", "/public/images/generic/down_arrow.png");
            jQuery("#lblTipoVino").css("color", "#f4f3ef");
            jQuery("#ulGastronomia").hide(secondi);
            jQuery("#imgArrowG").attr("src", "/public/images/generic/down_arrow.png");
            jQuery("#lblGastronomia").css("color", "#f4f3ef");
            break;
        case 4:
            jQuery("#ulVino").toggle(secondi);
            if (document.getElementById("imgArrowV").src.indexOf("up_arrow.png") > -1) {
                jQuery("#imgArrowV").attr("src", "/public/images/generic/down_arrow.png");
                jQuery("#lblVini").css("color", "#f4f3ef");
            }
            else {
                jQuery("#imgArrowV").attr("src", "/public/images/generic/up_arrow.png");
                jQuery("#lblVini").css("color", "#ffcc00");
            }
            jQuery("#ulTipoVino").hide(secondi);
            jQuery("#imgArrowTV").attr("src", "/public/images/generic/down_arrow.png");
            jQuery("#lblTipoVino").css("color", "#f4f3ef");
            jQuery("#ulCantina").hide(secondi);
            jQuery("#imgArrowC").attr("src", "/public/images/generic/down_arrow.png");
            jQuery("#lblCantinaVino").css("color", "#f4f3ef");
            jQuery("#ulRegione").hide(secondi);
            jQuery("#imgArrowR").attr("src", "/public/images/generic/down_arrow.png");
            jQuery("#lblRegioneVino").css("color", "#f4f3ef");
            jQuery("#ulGastronomia").hide(secondi);
            jQuery("#imgArrowG").attr("src", "/public/images/generic/down_arrow.png");
            jQuery("#lblGastronomia").css("color", "#f4f3ef");
            break;
        case 5:
            jQuery("#ulGastronomia").toggle(secondi);
            if (document.getElementById("imgArrowG").src.indexOf("up_arrow.png") > -1) {
                jQuery("#imgArrowG").attr("src", "/public/images/generic/down_arrow.png");
                jQuery("#lblGastronomia").css("color", "#f4f3ef");
            }
            else {
                jQuery("#imgArrowG").attr("src", "/public/images/generic/up_arrow.png");
                jQuery("#lblGastronomia").css("color", "#ffcc00");
            }
            jQuery("#ulTipoVino").hide(secondi);
            jQuery("#imgArrowTV").attr("src", "/public/images/generic/down_arrow.png");
            jQuery("#lblTipoVino").css("color", "#f4f3ef");
            jQuery("#ulCantina").hide(secondi);
            jQuery("#imgArrowC").attr("src", "/public/images/generic/down_arrow.png");
            jQuery("#lblCantinaVino").css("color", "#f4f3ef");
            jQuery("#ulRegione").hide(secondi);
            jQuery("#imgArrowR").attr("src", "/public/images/generic/down_arrow.png");
            jQuery("#lblRegioneVino").css("color", "#f4f3ef");
            jQuery("#ulVino").hide(secondi);
            jQuery("#imgArrowV").attr("src", "/public/images/generic/down_arrow.png");
            jQuery("#lblVini").css("color", "#f4f3ef");
            break;
        default:
            break;
    }
}

function getEventTarget(e) {
    var target = (e.target) ? e.target : e.srcElement;
    if (target.nodeType == 3) target = target.parentNode;   //bug di Safari
    return target;
}

///////////////////////////////////////////////////////
// funzione per la creazione di un input hidden
///////////////////////////////////////////////////////
function createHidden(namesValues) {
    if (document.createElement) {
        var arr = namesValues.split(",");
        for (i = 0; i < arr.length; i++) {
            var tokens = arr[i].split("=");
            var oldObj = document.getElementById(tokens[0]);
            if (oldObj) {
                oldObj.value = "";
                oldObj.value = tokens[1];
            } else {
                var newField = document.createElement("input");
                newField.type = "hidden";
                newField.name = tokens[0];
                newField.id = tokens[0];
                newField.value = tokens[1];
                document.forms[0].appendChild(newField);
            }
        }
    } else {
        alert("Il browser in uso non supporta operazioni fondamentali per l\'uso di questo sito.")
    }
}

///////////////////////////////////////////////////////
// funzione per settare il valore dei campi nascosti
///////////////////////////////////////////////////////
function setHiddenField(campo, valoreCampo) {
    document.getElementById(campo).value = valoreCampo;
}


///////////////////////////////////////////////////////
// funzione per la gestione dell'apertura delle news
// e delle relative immagini associate alla news
///////////////////////////////////////////////////////
function apriNews(newsDaAprire, imgDaAprire) {
    jQuery("#" + newsDaAprire).show(1000);

    // prendo tutti gli elementi con tag div e li salvo in un array
    var divs = document.getElementsByTagName("div");

    // ciclo tutti gli elementi dell'array 
    for (i = 0; i < divs.length; i++) {
        // controllo se l'elemento in questione contiene la parola dettagli
        if (divs[i].id.toString().indexOf("dettNews") >= 0) {
            // controllo se l'elemento in questione coincide con il nome passato come parametro
            if (divs[i].id.toString() != newsDaAprire.toString()) {
                jQuery("#" + divs[i].id.toString()).hide(1000); // e lo rendo visibile 
            }
        }
    }
    jQuery("#" + imgDaAprire).css("display", "block");
}

///////////////////////////////////////////////////////
// funzione per recupero scheda tecnica 
// vino mediante chiamata ajax
///////////////////////////////////////////////////////
function getSchedaVino(idVino, lingua) {
    // ajax call per recupero scheda vino
    jQuery.ajax({
        type: "POST",
        url: "/AjaxSchedaVino.aspx",
        data: "idVino=" + idVino + "&lingua=" + lingua,
        success: function(data) {
            visualizzaScheda(data);
            //return false;
        },
        error: function(data) {
            noVisualizzaScheda(data);
        }
    });
}

///////////////////////////////////////////////////////
// funzione per visualizzazione scheda tecnica 
///////////////////////////////////////////////////////
function visualizzaScheda(esito) {
    jQuery("#lb_sfondo_st").show(500);
    jQuery("#lb_scheda_tecnica").show(500);
    jQuery("#lb_scheda_tecnica").html(esito);
}

///////////////////////////////////////////////////////
// funzione per chiusura scheda tecnica 
///////////////////////////////////////////////////////
function chiudiSchedaTecnica() {
    jQuery("#lb_sfondo_st").hide(500);
    jQuery("#lb_scheda_tecnica").hide(500);
    jQuery("#lb_scheda_tecnica").html("");
}

///////////////////////////////////////////////////////
// funzione per visualizzazione errore scheda tecnica 
///////////////////////////////////////////////////////
function noVisualizzaScheda(esito) {
    jQuery("#lb_sfondo_st").show(500);
    jQuery("#lb_scheda_tecnica").show(500);
    jQuery("#lb_scheda_tecnica").html(esito);
}

function chiudiLightboxSt() {
    jQuery("#lb_sfondo_st").hide(500);
    jQuery("#lb_scheda_tecnica").hide(500);
}
