$(function() {
    $('#noticias').before('<div id="nav" class="nav">').cycle({
        fx: 'fade',
        speed: 'fast',
        timeout: 30000,
        pager: '#nav'
    });
    $("[name=Comunicado]").trigger('click');
    $('#atualiza_fechar').click(function() {
        $('.atualiza').hide();
    });

    //CONTADOR
    // se tem mais de um jogo entao nao habilita o cycle e seta aos handlers do mouse
    var itens = $('.jogos_geral').size();

    // inicialmente esconde a navegacao
    $('#botao_nav').hide();

    if (itens > 1) {
        $('.jogos').cycle({
            fx: 'fade',
            delay: -1000,
            next: '.botao_right',
            prev: '.botao_left'
            //nowrap: 1
        });

        $('#botao_nav').show();

        $('.jogos_geral').each(function() {
            $(this).mouseover(function() {
                $('.jogos').cycle('pause');
                $(this).find('.jogos_fundo').show();
                $(this).find('.jogos_frente').hide();
            });
            
            $(this).mouseleave(function() {
                $('.jogos').cycle('resume');
                $(this).find('.jogos_frente').show();
                $(this).find('.jogos_fundo').hide();
            });
        });

    } else if (itens == 1) {
        $('.jogos_geral').each(function() {
            $(this).mouseenter(function() {
                $(this).find('.jogos_fundo').show();
                $(this).find('.jogos_frente').hide();
            });

            $(this).mouseleave(function() {
                $(this).find('.jogos_frente').show();
                $(this).find('.jogos_fundo').hide();
            });
        });
    }

    $('.contador').each(function() {
        var d = $(this).find(':input[name=dias]').val().replace(/^[0]+/g, "");
        var h = $(this).find(':input[name=horas]').val().replace(/^[0]+/g, "");
        var m = $(this).find(':input[name=minutos]').val().replace(/^[0]+/g, "");
        var s = $(this).find(':input[name=segundos]').val().replace(/^[0]+/g, "");

        if (d == "")
            d = 0;
        if (h == "")
            h = 0;
        if (m == "")
            m = 0;
        if (s == "")
            s = 0;

        $(this).contador({
            dias: d,
            horas: h,
            minutos: m,
            segundos: s,
            intervalo: 1000,
            tempoExpirou: function(elem) {
                elem.closest('.jogos_frente').removeClass('venda_aberta');
                elem.closest('.jogos_frente').addClass('venda_fechada');
                elem.hide();
            }
        });
    });

    $('.jogos_fundo').each(function() {
        $(this).hide();
    });

    $('.jogos_frente').each(function() {
        $(this).addClass('venda_aberta');
    });
});

