function filter(anno) {
    if (anno === undefined) { anno = 0; }
    $(".eventi_table tr:hidden").show();
    if (anno == 'all') {
    } else if (anno == 'next') {
        $(".eventi_table tr.past").hide();
    } else if (anno == 'past') {
        $(".eventi_table tr.next").hide();
    } else {
        $(".eventi_table tr").each(function(){
            $(".eventi_table tr .date:not(:contains('" + anno + "'))").parent().hide();
        });
    }
}

function filterNews(anno) {
    if (anno === undefined) { anno = 0; }
    $(".newsItem:hidden").show();
    if (anno == 'all') {
    } else {
        $(".newsItem").each(function(){
            $(".newsItem .date:not(:contains('" + anno + "'))").parent().parent().hide();
        });
    }
}

function loadBeachStats(anno) {
    var s_open = dati_beach[anno]['squadre']['open'];
    var s_open_width = s_open / dati_beach_fondo_scala['squadre'] * dati_beach_larghezza_massima;
    if (dati_beach[anno]['squadre']['slam'] != undefined) {
        var s_slam = dati_beach[anno]['squadre']['slam'];
        var s_slam_width = s_slam / dati_beach_fondo_scala['squadre'] * dati_beach_larghezza_massima;
    } else {
        var s_slam = 0;
        var s_slam_width = 0;
    }
    if (dati_beach[anno]['squadre']['u19'] != undefined) {
        var s_u19 = dati_beach[anno]['squadre']['u19'];
        var s_u19_width = s_u19 / dati_beach_fondo_scala['squadre'] * dati_beach_larghezza_massima;
    } else {
        var s_u19 = 0;
        var s_u19_width = 0;
    }
    if (dati_beach[anno]['squadre']['u14'] != undefined) {
        var s_u14 = dati_beach[anno]['squadre']['u14'];
        var s_u14_width = s_u14 / dati_beach_fondo_scala['squadre'] * dati_beach_larghezza_massima;
    } else {
        var s_u14 = 0;
        var s_u14_width = 0;
    }
    $('.beach_stats #s_slam').animate({'width':s_slam_width+'px'});
    $('.beach_stats #s_u19').animate({'width':s_u19_width+'px'});
    $('.beach_stats #s_u14').animate({'width':s_u14_width+'px'});
    var tot_squadre = parseInt(s_open) + parseInt(s_slam) + parseInt(s_u19) + parseInt(s_u14);
    $('.beach_stats #s_open').html(tot_squadre).animate({'width':s_open_width+'px'});
    var campi = dati_beach[anno]['campi'];
    var campi_width = campi / dati_beach_fondo_scala['campi'] * dati_beach_larghezza_massima;
    $('.beach_stats #campi').html(campi).animate({'width':campi_width+'px'});
    var partite = dati_beach[anno]['partite'];
    var partite_width = partite / dati_beach_fondo_scala['partite'] * dati_beach_larghezza_massima;
    $('.beach_stats #partite').html(partite).animate({'width':partite_width+'px'});
    var tchoukers = dati_beach[anno]['tchoukers'];
    var tchoukers_width = tchoukers / dati_beach_fondo_scala['tchoukers'] * dati_beach_larghezza_massima;
    $('.beach_stats #tchoukers').html(tchoukers).animate({'width':tchoukers_width+'px'});
}

$(document).ready(function() {
    
    $(".scrollable_home").scrollable({vertical: true, circular: true, speed: 500}).autoscroll({ autoplay: true, interval: 5000, autopause: true }).navigator();
    
    $(".scrollable_beach").append('<a class="next"></a><a class="prev"></a>');
    $(".scrollable_beach").scrollable({
        speed: 500,
        items: 'ul',
        onBeforeSeek: function(e, t) {
            if (t >= $(".scrollable_beach li").length) {
                return;
            }
            var anno = $(".scrollable_beach li").slice(t,t+1).attr('rel');
            loadBeachStats(anno);
        }
    });


// $value['partite'] / $scala['partite'] * $larghezza_massima


    $(".scrollable_beach").scrollable().end(0);
    
    $('.navi a').hover(function() {
        $(".scrollable_home ").scrollable().stop();
        $(".scrollable_home ").scrollable().seekTo($(this).index(), 150);
        $(this).blur();
    }, function() {
        $(".scrollable_home").scrollable().play();
    });
    $('.navi a').click(function() {
        var i = $(this).index() + 1;
        var link = $('.scrollable_home .items').children().slice(i).attr('href');
        if (link != undefined)
            document.location = link;
    });
    
    $('#footer .icon').css({opacity: 0.5});
    $('#footer .icon').hover(function() {
        $(this).stop(true, false).animate({opacity: 1}, 250);
    }, function() {
        $(this).stop(true, false).animate({opacity: 0.5}, 250);
    });
    
    $('.eventiFilter').click(function(){
        $('.eventiFilter').parent('.active').removeClass('active');
        $(this).parent().addClass('active');
        $('html, body').animate({scrollTop:225}, 'slow');
        filter($(this).attr('rel'));
        return false;
    });
    $('.newsFilter').click(function(){
        $('.newsFilter').parent('.active').removeClass('active');
        $(this).parent().addClass('active');
        $('html, body').animate({scrollTop:225}, 'slow');
        filterNews($(this).attr('rel'));
        return false;
    });
    /*
    $('.eventi .menu a').click(function() {
        $(this).next('ul').slideToggle();
        return false;
    });
    */
    $('.menu ul').hide();
    $('.menu .active').parents('ul').show();
    $('.menu .active > ul').show();
});
