$(function(){
    $('.mask-fone').mask('(99) 9999-9999'); //telefone
    $('.mask-data').mask('99/99/9999'); //data
    $('.default-value').defaultFields('#666', '#666');
    $('.default-value2').defaultFields('#462929', '#462929');

    $('#form-contato').validadorFormularios({
        classReferencia: 'validar-campo',
        idTextarea: 'mensagem',
        submitAjax: true,
        ajaxUrl: BASE_URI+'contato/index'
    });
    $('#form-representante').validadorFormularios({
        classReferencia: 'validar-campo',
        idTextarea: 'mensagem',
        submitAjax: true,
        ajaxUrl: BASE_URI+'contato/representante'
    });
    $('#form-newsletter-pop').validadorFormularios({
        classReferencia: 'validar-campo',
        submitAjax: true,
        ajaxUrl: BASE_URI+'newsletters/subscribe'
    });
    
    $('#form-trabalheconosco').validadorFormularios({
        classReferencia: 'valida-vazio'
    });

    $('select.select-style-1').sSelect({ddMaxHeight: '300px', token: 'select-style-1'});
    $('select.select-style-2').sSelect({ddMaxHeight: '300px', token: 'select-style-2'});
    $('select.select-style-3').sSelect({ddMaxHeight: '300px', token: 'select-style-3'});

    $('#bt_submit_form_news').click(function(){
        $('#form-newsletter').submit();return false;
    })
    
    if($('.tweets').length > 0){
        twitterAPI({
            'Utilizador':TWITTER_NOME,
            'Tweets':TWITTER_VIEWS,
            'containerID':'.tweets'
        });
    }

    $('#assinar').click(function(){
        $('#cancelar_ass').attr('checked','');
        $('#assinar').attr('checked','checked');
    })
    $('#cancelar_ass').click(function(){
        $('#assinar').attr('checked','');
        $('#cancelar_ass').attr('checked','checked');
    })

    jQuery('.abas-trabalhe').click(function(){
        var idAba = jQuery(this).attr('id');
        var quebraIdAba = idAba.split('_');

        if(quebraIdAba[1] == 1){
            jQuery('.abas').addClass('abas-vagas');
            jQuery('.abas').removeClass('abas-curriculo');
        }else{
            jQuery('.abas').removeClass('abas-vagas');
            jQuery('.abas').addClass('abas-curriculo');
        }

        jQuery('.abas-trabalhe').removeClass('aba-sel');
        jQuery(this).addClass('aba-sel')

        jQuery('.conteiner-abas').hide();
        jQuery('.conteudo-aba-'+quebraIdAba[1]).show();

        jQuery(this).blur();
        return false;
    })

    $('.setas-abrir-banner').click(function(){
        $('#banner').slideDown('slow');
        $('.setas-abrir-banner').hide();
        return false;
    });
    $('.seta-fechar-banner').click(function(){
        $('#banner').slideUp('slow');
        $('.setas-abrir-banner').show();
        return false;
    });
    $('.pagina-banner-colecoes').click(function(){
        var ElementoRel = $(this).attr('rel');

        $('.pagina-banner-colecoes').removeClass('sel');
        $('.pagina-banner-colecoes').addClass('paginas');
        $(this).removeClass('paginas');
        $(this).addClass('sel');

        $('#banner .estacao .vagao').fadeOut('fast');
        $('#banner .estacao .vagao').eq(ElementoRel).fadeIn('slow');

        return false;
    });
    $('.controler-banner').click(function(){
        var ElementoRel = $('.paginador-banner .sel').attr('rel');
        var TotalBanners = $('.paginador-banner .pagina-banner-colecoes').length;

        $('#banner .estacao .vagao').fadeOut('fast');
        $('.pagina-banner-colecoes').removeClass('sel');
        $('.pagina-banner-colecoes').addClass('paginas');

        if($(this).hasClass('passa')){
            if((TotalBanners-1) != parseFloat(ElementoRel)){
                $('.pagina-banner-colecoes').eq((parseFloat(ElementoRel)+1)).addClass('sel');
                $('.pagina-banner-colecoes').eq((parseFloat(ElementoRel)+1)).removeClass('paginas');
                $('#banner .estacao .vagao').eq((parseFloat(ElementoRel)+1)).fadeIn('slow');
            }else{
                $('.pagina-banner-colecoes').eq(0).addClass('sel');
                $('.pagina-banner-colecoes').eq(0).removeClass('paginas');
                $('#banner .estacao .vagao').eq(0).fadeIn('slow');
            }
        }else{
            if(0 != parseFloat(ElementoRel)){
                $('.pagina-banner-colecoes').eq((parseFloat(ElementoRel)-1)).addClass('sel');
                $('.pagina-banner-colecoes').eq((parseFloat(ElementoRel)-1)).removeClass('paginas');
                $('#banner .estacao .vagao').eq((parseFloat(ElementoRel)-1)).fadeIn('slow');
            }else{
                $('.pagina-banner-colecoes').eq(TotalBanners-1).addClass('sel');
                $('.pagina-banner-colecoes').eq(TotalBanners-1).removeClass('paginas');
                $('#banner .estacao .vagao').eq(TotalBanners-1).fadeIn('slow');
            }
        }

        return false;
    });

    window.setTimeout('menuNovoTamanho()', 500);

    $('.conteudo-aba-2').hide();

    $('.click-mapa').each(function(){
       $(this).bind('click', function(){
            var estado = $(this).attr('rel');

            var selectText = $('#form-busca-representante .select-style-1 li a').html();
            $('#form-busca-representante .select-style-1').find('li a').each(function(){
                if($(this).html() == estado){
                    $(this).click();
                }
            });
            return false;
        })
    })

    $('#passa-menu').bind('click', function(){passar();return false;});
    $('#volta-menu').bind('click', function(){voltar();return false;});
});

function menuNovoTamanho(){
     $('#meio').each(function(){
        var tamanhoDivMeio = ($(this).height()-285);
        $('.menu-internas').height(tamanhoDivMeio);
        $('.menu-internas #compartilhe').attr('style','margin-top:'+(tamanhoDivMeio-50)+'px; margin-left:25px;')
    })
}

var busy = false;
var busy1 = false;
function passar(){
    if(busy == true)
            return false;
        busy = true;
        var elemento = $('.menu-baixo .estacao ul li:first');
        var novoElemento = elemento.clone();
        var width = elemento.width();
        novoElemento.css('width', width+'px');
        novoElemento.css('margin-right', '0px');
        $('.menu-baixo .estacao ul').append(novoElemento);

        elemento.animate({marginLeft: '-'+width+'px'}, 500, function(){elemento.remove();busy = false;});
        return false;
}
function voltar(){
     if(busy1 == true) return false;
        busy1 = true;
        var elemento = $('.menu-baixo .estacao ul li:last');
        var novoElemento = elemento.clone();
        var width = elemento.width();
        novoElemento.css('width', width+'px');
        novoElemento.css('margin-left','-'+width+'px');
        $('.menu-baixo .estacao ul').prepend(novoElemento);
        elemento.remove();

        $('.menu-baixo .estacao ul li:first').animate({marginLeft: '0px'},500, function(){busy1 = false;});
        return false;
}
//-----JS do crosscontent
$(function(){
    $('#passa-cross-marcas').bind('click', function(){passarCross();$(this).blur();return false;});
    window.setTimeout('alongabarra()', 500);
});


function alongabarra(){
    if($('#internas').length!= 0){
        var tamanho = ($('#internas').height()+52);
        $('.linha-compartilhar').height(tamanho+'px')
    }
}

var busy2 = false;
function passarCross(){
    if(busy2 == true)
            return false;
        busy2 = true;
        var elemento = $('.cross-content .meio .estacao .trilho .vagao:first');
        var novoElemento = elemento.clone();
        var width = elemento.width();
        novoElemento.css('width', width+'px');
        novoElemento.css('margin-right', '0px');
        $('.cross-content .meio .estacao .trilho').append(novoElemento);

        elemento.animate({marginLeft: '-'+width+'px'}, 500, function(){elemento.remove();busy2 = false;});
        return false;
}

//-----JS das noticias
var ElementoAtual = '';
$(function(){
    $('.passar-foto').live('click', function(){
        ElementoAtual = $(this);
        passarFoto();
        $(this).blur();
        return false;
    });
    $('.voltar-foto').live('click', function(){
        ElementoAtual = $(this);
        voltarFoto();
        $(this).blur();
        return false;
    });
    $('.noticias-h .estacao .vagao').live('click',function(){
        var ElementoIMG = $(this).children();
        var imgGrande = ElementoIMG.parent().parent().parent().parent().prev().children();

        imgGrande.attr('src',ElementoIMG.attr('src'));
        return false;
    })

});
var busy = false;
var busy1 = false;
function passarFoto(){
    if(busy == true)
            return false;
        busy = true;
        var estacao = ElementoAtual.prev();
        var trilho = estacao.find('.trilho');

        var elemento = estacao.find('.trilho .vagao:first');
        var novoElemento = elemento.clone();
        var width = elemento.width();
        novoElemento.css('width', width+'px');
        novoElemento.css('margin-right', elemento.css('margin-right'));
        trilho.append(novoElemento);

        elemento.animate({marginLeft: '-'+width+'px'}, 500, function(){elemento.remove();busy = false;});
        return false;
}
function voltarFoto(){
     if(busy1 == true) return false;
        busy1 = true;

        var estacao = ElementoAtual.prev().prev();
        var trilho = estacao.find('.trilho');

        var elemento = estacao.find('.trilho .vagao:last');
        var novoElemento = elemento.clone();
        var width = elemento.width();

        var quebra = elemento.css('margin-left').split('px')
        var marginLeftX = (width+quebra[0]);

        novoElemento.css('width', width+'px');
        novoElemento.css('margin-left','-'+marginLeftX+'px');
        trilho.prepend(novoElemento);
        elemento.remove();

        estacao.find('.trilho .vagao:first').animate({marginLeft: quebra[0]+'px'},500, function(){busy1 = false;});
        return false;
}


//-----JS das noticias
var ElementoAtual = '';
$(function(){
    $('#passa-marcas').bind('click', function(){
        $('#clientes-balao').hide();
        passarMarcas();
        $(this).blur();
        return false;
    });
    $('#volta-marcas').bind('click', function(){
        $('#clientes-balao').hide();
        voltarMarcas();
        $(this).blur();
        return false;
    });
});

var busy = false;
var busy1 = false;
function passarMarcas(){
    if(busy == true)
            return false;
        busy = true;
        var elemento = $('#clientes .estacao .trilho .vagao:first');
        var novoElemento = elemento.clone();
        var width = elemento.width();
        novoElemento.css('width', width+'px');
        novoElemento.css('margin-right', '50px');
        $('#clientes .estacao .trilho').append(novoElemento);

        elemento.animate({marginLeft: '-'+width+'px'}, 500, function(){elemento.remove();busy = false;});
        return false;
}
function voltarMarcas(){
     if(busy1 == true) return false;
        busy1 = true;
        var elemento = $('#clientes .estacao .trilho .vagao:last');
        var novoElemento = elemento.clone();
        var width = elemento.width();
        novoElemento.css('width', width+'px');
        novoElemento.css('margin-left','-'+(width+50)+'px');
        $('#clientes .estacao .trilho').prepend(novoElemento);
        elemento.remove();

        $('#clientes .estacao .trilho .vagao:first').animate({marginLeft: '0px'},500, function(){busy1 = false;});
        return false;
}

$(document).ready(function(){

    var AtualSRC = '';
    var ElementAtual = '';
    var ElementoLink = '';
    $('.MOUSE-HOVER').hover(function(){
        ElementoLink = $(this);
        var ElementIMG = $(this).find('img');
        AtualSRC = ElementIMG.attr('src');
        ElementAtual = ElementIMG;
        var NovoElementIMG = ElementIMG.attr('rel');

        ElementIMG.attr('src', NovoElementIMG);
        //ElementoLink.removeClass('link_off');

    },function(){
        //ElementoLink.addClass('link_off');
        ElementAtual.attr('src',AtualSRC);
    })
})

