/*
| -------------------------------------------------------------------
| CÓDIGOS PADRÃO DOS SITES
| -------------------------------------------------------------------
| Se for copiar esse código mantenha os créditos
| Obrigado
| marlon@marlonrenan.com | StudioA Design & Web » www.studioaweb.com.br
|
|*/
$(document).ready(function(){

    // HACK CSS CROSS BROWSER
    $.each($.browser, function(i, val){
        $('html').addClass(i);
        return false;
    });

    // verificando se o usuário esta utilizando o ie6
    if($.browser.msie && $.browser.version=="6.0"){

        var html = '<div id="box-ie">';
        html+='<div id="box-ie-text">';
        html+='<h1>ATENÇÃO<br/> <span>Seu browser esta desatualizado</span></h1>';
        html+='Você esta usando o Internet Explorer 6. Este é um navegador muito antigo que não oferece suporte para websites modernos.';
        html+=' Para ter melhor experiência em nosso website (e muitos outros), por favor atualize para o Internet Explorer 8, ou migre para um outro alternativo, como Firefox, Chrome ou Safari.';
        html+='</div>';
        html+='<div id="box-ie-icones">';
        html+='<a href="http://br.mozdev.org/download/" target="_blank" id="firefox">Firefox</a>';
        html+='<a href="http://www.google.com/chrome/index.html?hl=pt-BR&brand=CHMB&utm_campaign=pt-BR&utm_source=pt-BR-ha-latam-br-sk&utm_medium=ha" target="_blank" id="chrome">Google Chrome</a>';
        html+='<a href="http://www.apple.com/safari/download/" target="_blank" id="safari">Safari</a>';
        html+='<a href="http://www.microsoft.com/brasil/windows/internet-explorer/?WT.mc_id=1579" target="_blank" id="ie8">IE8<!-- fix bug --></a>';
        html+='</div>';
        html+='<div id="box-ie-close">Fechar</div>';
        html+='</div>';
        $('body').prepend(html);
        $('#box-ie').fadeIn('slow', function(){
            $('#box-ie-close').click(function(){
                $('#box-ie').fadeOut('slow');
            });
        });
    }

    function vemail(obj){
        var Email = $(obj).val();
        var reEmail = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
        if (!reEmail.test(Email) || Email == "" || Email == null) {
            return false;
        } else {
            return true;
        }

    }

    
    // VERIFICANDO RETORNO DO CONTATO
    if($('#contato-mensagem').html()){

        window.setTimeout(function(){
            $('#contato-mensagem').slideUp('slow', function(){

            });
        }, 8000);

    }

    // PLUGIN DE MASCARA NO FORMULÁRIO
    if($('input[name="telefone"]').is(':visible')){
        $('input[name="telefone"]').mask('(99)9999-9999');
    }
    

    // VALIDANDO FORMULÁRIO DE CONTATO
    $('.form_contato').submit(function(ev){
        
        if($('input[name="nome"]').val()==''){
            $('input[name="nome"]').focus();
            return false;
        }
        if($('input[name="telefone"]').val()==''){
            $('input[name="telefone"]').focus();
            return false;
        }
        if(vemail('input[name="email"]')==false){
            alert('Formato de e-mail inválido! \n(Exemplo: nome@servidor.ext)');
            $('input[name="email"]').focus();
            return false;
        }
        if($('select[name="select-contato"]').val()==''){
            $('select[name="select-contato"]').focus();
            return false;
        }        
        if($('textarea[name="mensagem"]').val()==''){
            $('textarea[name="mensagem"]').focus();
            return false;
        }
        
    });
    
/*
| -------------------------------------------------------------------
| CONFIGURAÇÕES DO SITE
| -------------------------------------------------------------------
|*/

    // ==============================================================
    // VERIFICANDO SE DEVE EXIBIR INFORMAÇÃO DE RETORNO
    if($('#box-retorno').html()){
        
        $('#mascara').css('opacity',0.5);
        $('#mascara').css('height',$(document).height()+'px');

        //POSICIONA NO CENTRO
        var meioh = ($(window).width()/2)-($("#box-retorno").width()/2);
        var meiov = ($(window).height()/2)-($("#box-retorno").height()/2);
        $("#box-retorno").css({
            'margin-top':+meiov,
            'margin-left':+meioh
        });

        $('#mascara').fadeIn('fast', function(){            
            $('#box-retorno').fadeIn('fast', function(){
                setTimeout(function(){
                    $('#box-retorno').fadeOut('fast',function(){
                        $('#mascara').fadeOut('fast');
                    })
                }, 2000);
            });
        });
    }



    // removendo nome e e-mail da caixa de newsletter
    var valorescampos = {
        "newsnome"  :   "nome",
        "newsemail" :   "e-mail"
    };
    $.each(valorescampos,function(campo,valor){
        $('#'+campo).focus(function(){
            if($(this).val()==valor) $(this).val('');
        }).blur(function(){
            if($(this).val()=='') $(this).val(valor);
        });
    });

    // validando
    $('#box-newsletter').submit(function(ev){
        $.each(valorescampos,function(campo,valor){
            var campoval = $('#'+campo).val();
            if(campo=='newsemail'){
                var reEmail = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
                if (!reEmail.test(campoval) || campoval == "" || campoval == null) {
                    alert('Formato de e-mail inválido! \n(Exemplo: nome@servidor.ext)');
                    $('#'+campo).focus();
                    ev.preventDefault();
                    return false;
                }
            }
            if(campoval==valor || campoval==''){
                $('#'+campo).focus();
                ev.preventDefault();
                return false;
            }
        });
    });

    // verificando dados cadastrados
    if($('#html-planos').is(':visible')){
        if(!$('#html-planos p').html()){
            $('#html-planos').html('<i>Nenhum plano foi cadastrado até o momento.</i>');
            $('#html-planos').next().html('');
        }
    }

    // verificando aparelhos
    if($('#box-aparelhos').is(':visible')){
        $('.bloco-ap').attr('id','ap-first');
        if(!$('.bloco-ap').html()){
            $('#box-aparelhos').html('<i>Nenhum aparelho foi cadastrado até o momento.</i>');
            $('#box-aparelhos').next().html('');
        }
    }

    // =========================================================================
    // ACAO ANIMACAO MATERIAS
    var index = 0;
    var time = 15000;
    var tmat = $('.infomat').length;

    function timer(){

        this.usuinterval = '';
        this.intervalo_usu = function(){
            this.usuinterval = window.setTimeout(function(){
                slide(index)
            }, time);
        }
        this.clearintervalo_usu = function(){
            clearInterval(this.usuinterval);
        }

    }

    function slide(indice){
        
        index=indice;

        if(index==tmat) index=0;


        // ADICIONA CLASSE ATUAL
        $('.ativo-destaque').removeClass('ativo-destaque');
        $('#'+index).addClass('ativo-destaque');

        var objpai = $('.infomat').eq(index);
        var mattitulo = objpai.find('.boxmattitulo').text();
        var matresumo = objpai.find('.boxmatresumo').text();
        var matlink = objpai.find('.boxmatlink').text();
        var imgdest = objpai.find('.imgdest').html();

        // ESCREVENDO DADOS
        $('#title-not-desc').html(mattitulo);
        $('#resumoDest').html(matresumo);
        $('.link-mat').attr('href',matlink);
        
        // ====================================

        $('#fotoGrande').html(imgdest);

        index++;
        // ====================================
        // FUNCAO SETTIMEOUT
        timeusu.intervalo_usu();


    }

    if($('#box-dest-not').is(':visible')){

        // ACAO QUE ESCREVE BLOCOS COM DATA
        $('.boxmatdata').each(function(i){
            $('#menu-destaque-not').append('<a href="'+$('.boxmatlink').eq(i).text()+'" id="'+i+'" class="linkmatdesc">'+$(this).text().substr(0,5)+'</a>');

        });

        $('#menu-destaque-not a').stop(true, true).hover(
            function(){
                timeusu.clearintervalo_usu();
                slide($(this).attr('id'));
            });

        var timeusu = new timer();
        slide(index);

    }

    // seção eu quero
    if($('#combo-pags').is(':visible')){
        $('#combo-pags').change(function(){
            $('#pags-location').attr('href',$(this).val());
        });
        $('#pags-location').click(function(ev){
            ev.preventDefault();
            if($(this).attr('href')=='' || $(this).attr('href')=='#' ){
                alert('selecione a página que deseja visitar');
            } else {
                window.location=$(this).attr('href');
            }
        });
    }


    


});
