﻿//Parametri impostabili
var PercorsoFileXml = "/contenuti/xml/contenuti.asp";
//var ParametriFileXml = "lingua=IT&Codice=17"; //questa variabile la setto sulla pagina principale
var SecondInterval = 5000; //millisecondi



var docXML;
var MatriceHTMLBox;
var MatriceHTMLTasti;
var Indice = 1;
var IndiceMax = 0;
var bloccaclick = false;
var IDSetInterval = 0;




//==============================================================================
//Lettura file xml
//==============================================================================
$(document).ready(function () {
    //Leggo il contenuto del div matrice
    //MatriceHTMLBox = $(document.getElementById('DIVBox')).html();
    MatriceHTMLTasti = $(document.getElementById('DIVPulsanti')).html();
    //Azzero il contenuto del div matrice
    //$(document.getElementById('DIVBox')).html('');

    var xml = $.ajax({
        type: "POST",
        url: PercorsoFileXml,
        data: ParametriFileXml,
        success: AjaxSucceeded,
        error: AjaxFailed
    });

});
//Esito positivo
function AjaxSucceeded(result) {
    ConvertiInXml(result);
}
//Esito negativo
function AjaxFailed(result) {
    alert(result.status + ' ' + result.statusText);
}
//==============================================================================




//==============================================================================
// ConvertiInXml
//==============================================================================
function ConvertiInXml(Stringa) {
    if (window.ActiveXObject) {
        //Internet explorer
        docXML = new ActiveXObject("Microsoft.XMLDOM");
        docXML.async = "false";
        docXML.loadXML(Stringa);
    }
    else {
        //Mozilla, Firefox, Opera
        var parser = new DOMParser();
        docXML = parser.parseFromString(Stringa, "text/xml");
    }
    CreaItems();

}
//==============================================================================



//==============================================================================
// PRELOAD
//==============================================================================
(function ($) {
    var cache = [];
    // Arguments are image paths relative to the current page.   
    $.preLoadImages = function () {
        var args_len = arguments.length;
        for (var i = args_len; i--; ) {
            var cacheImage = document.createElement('img');
            cacheImage.src = arguments[i];
            cache.push(cacheImage);
        }
    }
})(jQuery)
//Esempio di chiamata per fare il preload di una o più immagini
//jQuery.preLoadImages("immagine1.gif", "immagine2.jpg");
//==============================================================================



//==============================================================================
// CreaItems()
//==============================================================================
function CreaItems() {
    //Primo nodo del file xml
    var FileXml = docXML.getElementsByTagName("item");
    IndiceMax = FileXml.length;
    //Ciclo il file xml
    var MatriceTasti = "";
    if (IndiceMax > 0) {
        //Creo i tastini
        //alert(FileXml.length);
        for (var i = 0; i < FileXml.length; i++) {
            MatriceTasti += MatriceHTMLTasti;
            MatriceTasti = MatriceTasti.replace(/##INDICE##/g, i);
        }
        //Riempio il div coi tastini generati
        MatriceHTMLTasti = $(document.getElementById('DIVPulsanti')).html(MatriceTasti);
        $(document.getElementById('DIVPulsanti')).show();


        //Ciclo i nodi dell'xml e genero gli onclick dei tastini
        for (var i = 0; i < FileXml.length; i++) {
            $(document.getElementById("img" + i)).show();
            $(document.getElementById("img" + i)).attr('idd', i);

            var Immagine = "";
            var IDD = i;
            if (docXML.getElementsByTagName("item")[IDD].getElementsByTagName("immagine")[0].childNodes[0] != null) Immagine = docXML.getElementsByTagName("item")[IDD].getElementsByTagName("immagine")[0].childNodes[0].nodeValue


            //Faccio il preload dell'immagine corrente
            if (Immagine != "") jQuery.preLoadImages(docXML.getElementsByTagName("item")[i].getElementsByTagName("immagine")[0].childNodes[0].nodeValue);

            
            //...........................................................................
            //Al primo avvio carico la prima immagine con il testo della prima immagine
            if (i == 0) {
                //Cambio l'immagine di background
                if (Immagine != "") $(document.getElementById('FileIMG')).attr('style', 'background-image:url(' + docXML.getElementsByTagName("item")[IDD].getElementsByTagName("immagine")[0].childNodes[0].nodeValue + ')');

                for (var ind = 0; ind < IndiceMax; ind++) {
                    $(document.getElementById("img" + ind)).show();
                    $(document.getElementById("DIVPulsSel" + ind)).hide();
                }

                $(document.getElementById("DIVPulsSel" + i)).show();
                $(document.getElementById("img" + i)).hide();

                //Cambio il testo dentro al Box
                var html = MatriceHTMLBox;

                $(document.getElementById("FileIMG")).fadeOut(0, function () {
                    // Animation complete
                });

                $(document.getElementById("FileIMG")).fadeIn('slow', function () {
                    // Animation complete
                });

                setTimeout(TogliSfondoSlideIMG, 3000);

            }
            //...........................................................................



            //...........................................................................
            //Genero l'evento onclick
            $(document.getElementById("img" + i)).click(function () {
                if (!bloccaclick) {
                    bloccaclick = true;
                    clearInterval(IDSetInterval);
                    var IDD = $(this).attr("idd");
                    Indice = parseInt(IDD) + 1;

                    for (var ind = 0; ind < IndiceMax; ind++) {
                        $(document.getElementById("img" + ind)).show();
                        $(document.getElementById("DIVPulsSel" + ind)).hide();
                    }

                    $(document.getElementById("DIVPulsSel" + IDD)).show();
                    $(document.getElementById("img" + IDD)).hide();

                    $(document.getElementById("FileIMG")).attr('idd', IDD);
                    $(document.getElementById("FileIMG")).fadeOut('slow', function () {
                        // Animation complete

                        var IDD = $(this).attr("idd");
                        //Cambio l'immagine di background
                        var Immagine = "";
                        if (docXML.getElementsByTagName("item")[IDD].getElementsByTagName("immagine")[0].childNodes[0] != null) Immagine = docXML.getElementsByTagName("item")[IDD].getElementsByTagName("immagine")[0].childNodes[0].nodeValue

                        //Cambio l'immagine di background
                        if (Immagine != "") $(document.getElementById('FileIMG')).attr('style', 'background-image:url(' + docXML.getElementsByTagName("item")[IDD].getElementsByTagName("immagine")[0].childNodes[0].nodeValue + ')');

                    });

                    $(document.getElementById("FileIMG")).fadeIn('slow', function () {
                        // Animation complete
                        //alert("fine fadeIn");
                        IDSetInterval = imageInterval = window.setInterval("changeImage();", SecondInterval);
                        bloccaclick = false;
                    });

                }
            });
            //...........................................................................
        }
        if (IndiceMax > 1) {
            IDSetInterval = imageInterval = window.setInterval("changeImage();", SecondInterval);
        }
        else {
            $(document.getElementById("DIVPulsSel0")).hide();
            $(document.getElementById("img0")).hide();
        }
    }
}

//==============================================================================


function TogliSfondoSlideIMG() {
    NomeDiv = "SlideIMG"
    $(document.getElementById(NomeDiv)).attr("style", "background-image:url('/images/neutro.gif')");
    
}

//==============================================================================
// changeImage()
//==============================================================================
function changeImage() {
    if (Indice >= IndiceMax) Indice = 0;
    var IDD = Indice
    
    

    for (var ind = 0; ind < IndiceMax; ind++) {
        $(document.getElementById("img" + ind)).show();
        $(document.getElementById("DIVPulsSel" + ind)).hide();
    }

    $(document.getElementById("DIVPulsSel" + IDD)).show();
    $(document.getElementById("img" + IDD)).hide();
    
    
    $(document.getElementById("FileIMG")).attr('idd', IDD);
    $(document.getElementById("FileIMG")).fadeOut('slow', function () {
        // Animation complete

        var IDD = $(this).attr("idd");

        var Immagine = "";
        if (docXML.getElementsByTagName("item")[IDD].getElementsByTagName("immagine")[0].childNodes[0] != null) Immagine = docXML.getElementsByTagName("item")[IDD].getElementsByTagName("immagine")[0].childNodes[0].nodeValue

        //Cambio l'immagine di background
        if (Immagine != "") $(document.getElementById('FileIMG')).attr('style', 'background-image:url(' + docXML.getElementsByTagName("item")[IDD].getElementsByTagName("immagine")[0].childNodes[0].nodeValue + ')');

    });

    $(document.getElementById("FileIMG")).fadeIn('slow', function () {
        // Animation complete
        //alert("fine fadeIn");
    });

    Indice++;
    if (Indice == IndiceMax) Indice = 0;

}
//==============================================================================


