var AF = {}

fecharAlert = function() {
    $('.flash_alert').fadeOut('fast');
}

//tratamento para as páginas: Bares, Baladas 
AF.handleLocais = function() {
    $('#locais .local:last').addClass('ultimo');
}

AF.onReady = function() {
    AF.handleLocais();

    AF.handleAlertas();

    AF.bannerPrincipal.init();

    AF.handleAbasHome();

    $.ajaxSetup({ cache: false });
}

AF.handleAbasHome = function() {
    var o = {}
    o.widget = $('#home_cultura #abas');

    o.itens = o.widget.find('.abaItem');
    o.conteudos = o.widget.find('.abaConteudo');

    o.conteudos.eq(0).show();

    o.itens.click(function() {
        o.target = $(this).attr('rel');
        o.conteudos.hide().filter('#aba-' + o.target).fadeIn('fast');
    });
}

AF.handleAlertas = function() {
    var o = {}
    o.widget = $('.flash_alert');

    if (o.widget.length) {
        //identificar classe que estilizará o alerta
        switch (o.widget.attr('class').split(' ')[1]) {
            case 'flash_bad': o.type = 'fatal'; break;
            default: o.type = 'success';
        }
        
        //copiar a mensagem de um DIV escondido, para o alerta
        $('body').jAlert($('#mensagem', o.widget).html(), o.type);
    }
}

//utilizado para alterar a lista de coberturas filtrando por local
AF.buscarCoberturasPorLocal = function(local, alvo) {
    $(alvo).attr("disabled", "disabled").html('<option><em>Carregando festas</em></option>');
    $.get(
        BASE + '/coberturas/getCoberturasPorLocal/' + local + '/ajax',
        function(options) {
            $(alvo).html(options).removeAttr("disabled");
        }
    )
}

AF.carregarCobertura = function(slug) {
    if (slug == 0) return;

    window.location.href = BASE + '/fotos/ver/' + slug;
}

AF.bannerPrincipal = {}
AF.bannerPrincipal.intervalo = null;
AF.bannerPrincipal.tempoIntervalo = 5000;

AF.bannerPrincipal.resetSlideshow = function() {
    clearInterval(AF.bannerPrincipal.intervalo);
    AF.bannerPrincipal.intervalo = setInterval(AF.bannerPrincipal.trocarBanner, AF.bannerPrincipal.tempoIntervalo);
}

AF.bannerPrincipal.trocarBanner = function(trigger) { //se Trigger nulo, carrega o seguinte ao .ativo
    var o = {}
    o.widget = $('#home_tv');
    o.navItens = $('.nav a', o.widget);
    o.imgItens = $('.item', o.widget);
    o.infoItens = $('.info p', o.widget);

    if (typeof trigger == 'object') {
        if ($(trigger).hasClass('ativo')) return;

        o.index = $(trigger).index();
    } else {
        o.index = o.navItens.filter('.ativo').index();

        if (o.index == o.navItens.length - 1)
            o.index = 0;
        else
            o.index++;

    }

    //troca as imagens
    o.imgItens.filter('.ativo').removeClass('ativo').fadeOut();
    o.imgItens.eq(o.index).addClass('ativo').fadeIn();

    //troca a navegacao
    o.navItens.filter('.ativo').removeClass('ativo');
    o.navItens.eq(o.index).addClass('ativo')

    //troca a descricao
    o.infoItens.filter('.ativo').removeClass('ativo').fadeOut('normal', function() {
        o.infoItens.eq(o.index).addClass('ativo').fadeIn();
    });
}

AF.bannerPrincipal.init = function() {
    var o = {}
    o.widget = $('#home_tv');
    o.navItens = $('.nav a', o.widget);
    o.imgItens = $('.item', o.widget);
    o.infoItens = $('.info p', o.widget);

    AF.bannerPrincipal.resetSlideshow();

    o.navItens.each(function() {
        $(this).click(function() {
            AF.bannerPrincipal.trocarBanner(this);
            AF.bannerPrincipal.resetSlideshow();
        });
    });

    o.imgItens.click(function() {
        window.location.href = $(this).attr('link');
    });

    o.infoItens.eq(0).show();
    o.imgItens.eq(0).show();
}

AF.onWindowLoad = function() {
    //nada por enquanto
}

$(window).load(AF.onWindowLoad);

$(document).ready(AF.onReady);