var globEvents, globEventIdx, globOth, globOthIdx, globTmrId;

function initIndex () {
    globEventIdx = -1;
    globOthIdx = -1;
    $("#container_homenews").newsbar({
        ajaxUrl: "scripts/news/getnews.php?shidx=1"
    });
    execTimer();
}

function execTimer() {
    $("#articolo_an").fadeOut("slow", function () {
        updateOtherNews();
        $("#articolo_an").fadeIn("slow");
    });
    $("#div_appuntamenti").fadeOut("slow", function () {
        updateEvents();
        $("#div_appuntamenti").fadeIn("slow");
    });
    globTmrId = setTimeout(function () {
        execTimer();
    }, 8000);
}

function updateEvents () {
    var it;

    if (globEvents && globEvents.length > 0) {
        if (globEventIdx === (globEvents.length - 1)) {
            globEventIdx = 0;
        } else {
            globEventIdx = globEventIdx + 1;
        }
        it = globEvents[globEventIdx];
    } else {
        it = {
            id: 0,
            dt_ref: "",
            title: "",
            subtitle: "",
            descr: ""
        }
    }
    //Link al calendario
    $("#div_appuntamenti").bind("click", {evid: it.id}, function (event) {
        location.href = "calendario.php?id=" + event.data.evid;
    });
    //Data, titolo, descrizione
    $("#div_event_dt").html(it.dt_ref);
    $("#sp_event_title").html(it.title);
    $("#sp_event_subtitle").html(it.subtitle);

}

function updateOtherNews () {
    var it, tag;

    if (globOth && globOth.length > 0) {
        if (globOthIdx === (globOth.length - 1)) {
            globOthIdx = 0;
        } else {
            globOthIdx = globOthIdx + 1;
        }
        it = globOth[globOthIdx];
    } else {
        it = {
            id: 0,
            dt_ref: "",
            title: "",
            descr: "",
            oth_bg_path: ""
        }
    }
    //Link alle news
    $("#articolo_an").bind("click", {nid: it.id}, function (event) {
        location.href = "news.php?id=" + event.data.nid;
    });
    //Immagine
    tag = $("#img_oth");
    if ($.trim(it.oth_bg_path).length > 0) {
        tag.attr("src", it.oth_bg_path);
        tag.show();
    } else {
        tag.attr("src", "");
        tag.hide();
    }
    //Data, titolo, descrizione
    $("#h_oth_dt").html(it.dt_ref);
    $("#h_oth_title").html(it.title);
    $("#p_oth_descr").html(it.descr);
}
