$(window).load(function () {
    $('#loading').fadeOut('slow');
    //    $('#container').fadeIn(2000);
    //    $('#container').show();
});

$(document).ready(function() {

    if ($.cookie('selectElementId') == null) $.cookie('selectElementId', 'MENU-0');

    //over sulle voci di menu
    $("#links img")
            .mouseover(function() {
        if ($(this).parent().attr('id') != $.cookie('selectElementId')) {
            var src = $(this).attr("src").match(/[^\.]+/) + "_f2.png";
            $(this).attr("src", src);
        }
    })
            .mouseout(function() {
        if ($(this).parent().attr('id') != $.cookie('selectElementId')) {
            var src = $(this).attr("src").replace("_f2", "");
            $(this).attr("src", src);
        }
    });

    //aggiorna gli id del menu e dell'albero e seleziona la voce dal cookie
    $('.menuElement').each(function(index) {
        $(this).attr('id', 'MENU-' + index);

        if ($.cookie('selectElementId') != null && $(this).attr('id') == $.cookie('selectElementId')) {
            var img = $(this).children('img');
            img.attr('src', img.attr("src").match(/[^\.]+/) + "_f2.png");
        }
    });
    $('.treeElement').each(function(index) {
        $(this).attr('id', 'TREE-' + index);

        if ($.cookie('selectElementId') != null && $(this).attr('id') == $.cookie('selectElementId')) {
            $(this).css('color', 'white');
        }
    });

    //click su un elemento del menu
    $('.menuElement').click(function() {
        $.cookie('selectElementId', $(this).attr('id'), {path:'/'});
    });

    //click su un elmento dell'albero
    $('.treeElement').click(function() {
        $.cookie('selectElementId', $(this).attr('id'), {path:'/'});
    });

    //ripristina la posizione di scroll tree del menu
    var top_position = $.cookie('norma-scroll-tree');
    $('#tree').scrollTop(top_position);
    $.cookie('norma-scroll-tree', null, {path:'/'});

    //salva la posizione di scroll del menu
    $('#tree').scroll(function() {
        var top_position = $('#tree').scrollTop();
        $.cookie('norma-scroll-tree', top_position, {path:'/'});
    });
});

function slideSwitch() {
    var attiva = $('#slideshow a.attiva');

    //if ( attiva.length == 0 ) attiva = $('#slideshow img.inactive:next');

    var successiva = attiva.next().length ? attiva.next() : $('#slideshow a:first');

    attiva.addClass('disattiva');
    //attiva.removeClass('attiva');

    successiva.css({opacity: 0.0})
            .addClass('attiva')
            .removeClass('disattiva')
            .animate({opacity: 1.0}, 1000, function() {
        attiva.removeClass('attiva');
    });
}
$(function() {
    setInterval("slideSwitch()", 5000);
});
function hideRegistrazione() {
    $("#registrazioneUtente").hide('slow');
}
$(function() {
    $("#registrazioneUtente area").click(function() {
        $("#registrazioneUtente").hide('slow');
    });
    setInterval("hideRegistrazione()", 10000);
});

function notizieFlash() {
    var notiziaFlashIndex=0;
    var notizie = $('span.notiziaFlash');
    if (notizie != null && notizie.length > 0) {
        if ($.cookie("notiziaFlashIndex") != null) {
            notiziaFlashIndex = $.cookie("notiziaFlashIndex") % notizie.length;
        }

        $('span.notiziaFlash:visible').fadeOut('slow', function() {
            notiziaFlashIndex=(notiziaFlashIndex + 1) % notizie.length ;
            $('span.notiziaFlash').eq(notiziaFlashIndex).fadeIn('slow');
            $.cookie("notiziaFlashIndex", notiziaFlashIndex, { path: '/', expires: 1 });
        });
    }
}

$(function() {
    setInterval("notizieFlash()", 10000);
});




