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

    inicializa_banners();

    $('#form-contato').validadorFormularios({
        classReferencia: 'validar-campo',
        idTextarea: 'mensagem',
        submitAjax: true,
        ajaxUrl: BASE_URI+'contato/index'
    });

    $('#form-lojista').validadorFormularios({
        classReferencia: 'default-value2',
        valorPadrao: 'default-value2',
        idTextarea: 'mensagem',
        submitAjax: true,
        ajaxUrl: BASE_URI+'contato/lojista'
    });

    $('#form-trabalheconosco').validadorFormularios({
        classReferencia: 'valida-vazio'
    });

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

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


    $('#m_mapa .click-mapa').click(function(){
        var estadoClick = $(this).attr('rel');
        $(this).blur();
        location.href=BASE_URI+'contato-empresa/?estado='+estadoClick;
    })
    if($('.titulo-banner').length!= 0){
        var textH1 = $('.titulo-banner');
        var textValue = textH1.html().length;
        if(textValue > 30){ textH1.attr('style','margin-top:35px; font-size:50px;') }
        if(textValue > 35){ textH1.attr('style','margin-top:35px; font-size:40px;') }
        if(textValue > 45){ textH1.attr('style','margin-top:35px; font-size:30px;') }
    }
    
//    var Elemento = '';
//    var sumir = '';
//    $('.banner_mini_blocos .bloco').hover(function(){
//        Elemento = $(this);
//
//        var quebraElemento = Elemento.attr('id').split('-');
//
//        $.post(BASE_URI+'banners/trazarea/'+quebraElemento[1], null, function(result){
//            $('.banner_balao').show();
//            $('.banner_balao').html(result);
//
//            if($('.balao-texto-conteudo .titulo-banner').length!= 0){
//                var textH1 = $('.balao-texto-conteudo .titulo-banner');
//                var textValue = textH1.html().length;
//                if(jQuery.browser.msie){
////                    if(textValue > 10){ textH1.attr('style','margin-top:0px; font-size:50px;') }
////                    if(textValue > 20){ textH1.attr('style','margin-top:10px; font-size:40px;') }
////                    if(textValue > 30){ textH1.attr('style','margin-top:10px; font-size:30px;') }
//                }else{
////                    if(textValue > 50){ textH1.attr('style','margin-top:0px; font-size:50px;') }
////                    if(textValue > 70){ textH1.attr('style','margin-top:10px; font-size:40px;') }
////                    if(textValue > 80){ textH1.attr('style','margin-top:10px; font-size:30px;') }
//                }
//            }
//
//            var ElementoRel = Elemento.attr('rel');
//            var quebraRel = ElementoRel.split('b-')
//            $('#seta-banner').attr('class','bloco-'+quebraRel[1])
//
//            $('.banner_balao').hover(function(){
//                Elemento.addClass('sel');
//                sumir = false;
//            },function(){
//                Elemento.removeClass('sel');
//                $('.banner_balao').hide();
//            })
//
//        });
//    },
//    function(){});

    var tempo = startBanner();
    var tempo1 = startMarcas();
    $('#volta-banner').click(function(){
        contaBanners = contaBanners-1
        if(contaBanners < 0){
            contaBanners = banners.length-1;
        }
        var quebraBanner = banners[contaBanners].split('|');
        $('.sumir-todas-desc').hide();
        $('#banner_id_'+quebraBanner[1]).show();
        $('#banner').attr('style','background-image:url('+quebraBanner[0]+')');
        $(this).blur();
        return false;
    })

    var contaNoticias = 0;

    $('#passar-noticias').click(function(){
        contaNoticias = contaNoticias+1
        if(contaNoticias == noticias.length){
            contaNoticias = 0;
        }
        var noticiaId = noticias[contaNoticias];
        var left = cauculaWigth(460);

        jQuery('#div-loading').show();
        jQuery.blockUI({ message: jQuery('#loading'),css: { width:'460px', left: left+'px', top:'20%',border:'0;'} });

        jQuery.post(BASE_URI+'noticias/passanoticia/'+noticiaId, null, function(result){
            jQuery('#div-loading').hide();
            jQuery.unblockUI();
            jQuery('#conteiner-noticia-passa').html(result);
        });
 
        return false;
    })
    $('#voltar-noticias').click(function(){
        contaNoticias = contaNoticias-1
        if(contaNoticias < 0){
            contaNoticias = noticias.length-1;
        }
       var noticiaId = noticias[contaNoticias];

        var left = cauculaWigth(460);

        jQuery('#div-loading').show();
        jQuery.blockUI({ message: jQuery('#loading'),css: { width:'460px', left: left+'px', top:'20%',border:'0;'} });

        jQuery.post(BASE_URI+'noticias/passanoticia/'+noticiaId, null, function(result){
            jQuery('#div-loading').hide();
            jQuery.unblockUI();
            jQuery('#conteiner-noticia-passa').html(result);
        });

        return false;
    })

    var ElementoC = '';
    $('#clientes .blocos').hover(function(e){
        ElementoC = $(this);

        var x = e.pageX - $('#clientes .estacao').offset().left;

        var quebraElemento = ElementoC.attr('id').split('-');

        $.post(BASE_URI+'marcas/trazarea/'+quebraElemento[1], null, function(result){
            $('#clientes-balao').show();
            $('#clientes-balao').html(result);
            $('#clientes_seta').attr('style','margin-left:'+x+'px')
            $('#clientes-balao').hover(function(){
            },function(){
                $('#clientes-balao').hide();
            })
        });
    },
    function(){
        if(sumir){
            $('.banner_balao').hide();
        }
    });

    var ElementoC2 = '';
    $('.busca-area a').hover(function(e){
        ElementoC2 = $(this);
        ElementoC2.addClass('sel')
        $('.busca-topo').show();

        $('.busca-topo').hover(function(){
        },function(){
            ElementoC2.removeClass('sel');
            $('.busca-topo').hide();
        })
    },
    function(){});

    var ElementoC3 = '';
    $('.hover-extranet a').hover(function(e){
        ElementoC3 = $(this);
        ElementoC3.addClass('sel')
        $('.Extranet').show();

        $('.Extranet').hover(function(){
        },function(){
            ElementoC3.removeClass('sel');
            $('.Extranet').hide();
        })
    },
    function(){});

    $('.colecao .box').hover(function(e){
        $(this).addClass('active');
    },
    function(){
        $(this).removeClass('active');
    });

    $('.img-conteiner .img-pequena').hover(function(e){
        $(this).addClass('img-pequena-sel');
    },
    function(){
        $(this).removeClass('img-pequena-sel');
    });


    $('.abrir-submenu').click(function(){
        var offset = $(this).next().offset();
        var height = offset.height;
        $(this).toggleClass("active").next().toggle(function(){});
        $(this).blur();
        return false;
    });

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

        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;
    })
    $('.conteudo-aba-2').hide();

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

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 startBanner(){
    var time = window.setInterval('passabanner()', 4000);
    return time;
}
function startMarcas(){
    var time1 = window.setInterval('voltarMarcas()', 4000);
    return time1;
}


var contaBanners = 0;
var quebraBanner = '';
var arr_banners = [];

function inicializa_banners(){
    for(i=0; i<banners.length; i++)
    {
        var imageObj = new Image();
        quebraBanner = banners[i].split('|');
        imageObj.src=quebraBanner[0];
        arr_banners.push(imageObj);
    }
}

function passabanner(){

    contaBanners = contaBanners+1
    if(contaBanners == banners.length){
        contaBanners = 0;
    }
    var quebraBanner = banners[contaBanners].split('|');
    $('.sumir-todas-desc').hide();
    $('#banner_id_'+quebraBanner[1]).show();
    $('#banner').attr('style','background-image:url('+quebraBanner[0]+')');
    return false;
}

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);
    })
})

