﻿// Gestione vetrine a comparsa del sito.
// Data ultima modifica: 03/11/2009
// Autore: Vincenzo Pizio

var list1; 
var list2;
var divNews1; 
var divNews2;

var hex1 = 255;
var hex2 = 255;
var op1 = 0;
var alpha1 = 0;
var op2 = 0;
var alpha2 = 0;

//funzione per la variazione del colore delle news.
function fadeTesto(num) {
    var go = false;
    if (num == 1) {
        if (op1 < 1) {
            op1 += 0.05;
            alpha1 += 5;
            go = true;
        }
        else {
            op1 = 0;
            alpha1 = 0;
        }
        if (go == true) {
            divNews1.style.opacity = op1;
            divNews1.style.filter = "filter:alpha(opacity=" + alpha1 + ")";
            setTimeout("fadeTesto(" + num + ")", fadeSpeed);
        }
        else {
            op1 = 0;
            alpha1 = 0;
        }
    }
    else if (num == 2) {
        //a = 1;
        //window.setTimeout("a==1", 2000); 
        if (op2 < 1) {
            op2 += 0.05;
            alpha2 += 5;
            go = true;
        }
        else {
            op2 = 0;
            alpha2 = 0;
        }
        if (go == true) {
            divNews2.style.opacity = op2;
            divNews2.style.filter = "filter:alpha(opacity=" + alpha2 + ")";
            setTimeout("fadeTesto(" + num + ")", fadeSpeed);
        }
        else {
            op2 = 0;
            alpha2 = 0;
        }
    }

    
}
//funzione di inizializzazione.
function inizializzaNews(id1,id2) 
{
  divNews1 = document.getElementById(id1);
  divNews2 = document.getElementById(id2);

  list1 = divNews1.getElementsByTagName("div");
  list2 = divNews2.getElementsByTagName("div");

  run(id1,id2, 0);

}
//funzione esecutiva.
function run(id1,id2,count) 
{
    var ncont1 = 0;
    var ncont2 = 0;
    
    for (var i=0; i < list1.length; i++) {
        if (list1.item(i).id == "notizia") {
            ncont1 = ncont1 + 1; 
        }
    }
    for (var i = 0; i < list2.length; i++) {
        if (list2.item(i).id == "notizia") {
            ncont2 = ncont2 + 1;
        }
    }
    if (ncont1 > 0) {
        var ar1 = new Array(ncont1 - 1);
    }
    if (ncont2 > 0) {
        var ar2 = new Array(ncont2 - 1);
    }

    var cont = 0;
    var cont2 = 0;

    if (ar1 != null) {
        for (var i = 0; i < list1.length; i++) {
            if (list1.item(i).id == "notizia") {
                ar1[cont] = list1.item(i);
                cont = cont + 1;
            }
        }
    }
    cont = 0;
    if (ar2 != null) {
        for (var i = 0; i < list2.length; i++) {
            if (list2.item(i).id == "notizia") {
                ar2[cont2] = list2.item(i);
                cont2 = cont2 + 1;
            }
        }
    }
    cont2 = 0;
    if (ar1 != null) {
        if ((ar1.length > 1)) {
            fadeTesto(1);
        }
    }
    if (ar2 != null) {
        if ((ar2.length > 1)) {
            fadeTesto(2);
        }
    }

    //1° blocco
    if (ar1 != null) {
        if (ar1.length > 1) {
            var nNews = (ar1.length);

            ar1[count1].style.display = "block";
            if (count1 > 0) //se non sono al primo elemento nascondo il precedente.
            {
                ar1[count1 - 1].style.display = "none";
            }
            else //se sono al primo elemento nascondo l'ultimo della lista.
            {
                ar1[nNews - 1].style.display = "none";
            }
            count1++; //incremento il contatore.

            if (count1 == nNews) //se sono all'ultimo elemento riparto dal primo.
            {
                count1 = 0;
            }
        }
        else {
            ar1[0].style.display = "block";
        }
    }
    //2° blocco
    if (ar2 != null) {
        if (ar2.length > 1) {
            var nNews2 = (ar2.length);

            ar2[count2].style.display = "block";
            if (count2 > 0) //se non sono al primo elemento nascondo il precedente.
            {
                ar2[count2 - 1].style.display = "none";
            }
            else //se sono al primo elemento nascondo l'ultimo della lista.
            {
                ar2[nNews2 - 1].style.display = "none";
            }
            count2++; //incremento il contatore.

            if (count2 == nNews2) //se sono all'ultimo elemento riparto dal primo.
            {
                count2 = 0;
            }
        }
        else {
            ar2[0].style.display = "block";
        }
    }

    window.setTimeout("run('" + id1 + "','" + id2 + "' ," + count + ")", interval * 1000);
}

var interval = 4; // intervallo in secondi.
var fadeSpeed = 100;
var count1 = 0;
var count2 = 0;


