/* Cadastro de nesletter */
$(function(){
    $('#assinar').click(function(){
        $('#cancelar_ass').attr('checked','');
        $('#assinar').attr('checked','checked');
    })
    $('#cancelar_ass').click(function(){
        $('#assinar').attr('checked','');
        $('#cancelar_ass').attr('checked','checked');
    })

    $("#form-newsletter").bind('submit', function(){
        if(ValidaFormNewsletter()){
            var left = cauculaWigth(460);

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

            var data = $(this).serialize();
            jQuery.post(BASE_URI+'newsletters/subscribe', data, function(result){
                jQuery('#div-loading').hide();
                jQuery.unblockUI();
                alertNewsletter(result);
            });
        }
        return false;
    });
});
/* Mensagens de alerta da nesletter */
function alertNewsletter(msg)
{
    if(msg == 'sucesso'){
        alert('E-mail cadastrado com sucesso em nossa newsletter !')
    }else if(msg == 'error-1'){
        alert('Esse e-mail já está cadastrado em nossa newsletter!')
    }else if(msg == 'error-3'){
        alert('Esse e-mail já foi cancelado!')
    }else if(msg == 'error-4'){
        alert('Esse e-mail não está cadastrado em nossa newsletter!')
    }else if(msg == 'liberado'){
        alert('E-mail liberado com sucesso!')
    }else if(msg == 'cancelado'){
        alert('E-mail cancelado com sucesso!')
    }else{
        alert('Erro ao tentar cadastrar newsletter !')
    }
}

/* Valida Form */
function ValidaFormNewsletter()
{
    if($("#form-newsletter").find('input[name=email]').val() == 'Seu e-mail'){ alert('Por favor preencha o seu E-mail!'); return false; }
    if($("#form-newsletter").find('input[name=email]').val() == ''){ alert('Por favor preencha o seu E-mail!'); return false; }
    if(!echeck($("#form-newsletter").find('input[name=email]').val())){return false;}
    return true;
}
 /* Form Indicação */
 $(function(){
    $('#form-indicacao').submit(function(){
        $(this).find('input').blur();

        if($(this).find('input[name=seu_nome]').val() == '' || $(this).find('input[name=seu_nome]').val() == 'seu nome'){
            alert('Por favor preencha seu nome!');
            return false;
        }
        if($(this).find('input[name=seu_email]').val() == '' || $(this).find('input[name=seu_email]').val() == 'seu e-mail'){
            alert('Por favor preencha seu email!');
            return false;
        }
        if($(this).find('input[name=amigo_nome]').val() == '' || $(this).find('input[name=amigo_nome]').val() == 'nome do(a) amigo(a)'){
            alert('Por favor preencha o nome de seu amigo!');
            return false;
        }
        if($(this).find('input[name=amigo_email]').val() == '' || $(this).find('input[name=amigo_email]').val() == 'e-mail do(a) amigo(a)'){
            alert('Por favor preencha o email de seu amigo!');
            return false;
        }

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

        var data = $(this).serialize();
        jQuery.post(BASE_URI+'contato/indicar', data, function(result){
            jQuery('#div-loading').hide();
            jQuery.unblockUI();
            alert(result);
            $('#form-indicacao').clearForm();
        });
        return false;
    });
});
 $(function(){
    $('#form-coments').submit(function(){
        
        if($(this).find('input[name=name]').val() == ''){
            alert('Por favor preencha seu nome!');
            return false;
        }
        if($(this).find('input[name=email]').val() == ''){
            alert('Por favor preencha seu email!');
            return false;
        }
//        if($(this).find('input[name=website]').val() == ''){
//            alert('Por favor preencha o Website!');
//            return false;
//        }
        if($(this).find('#comment').val() == ''){
            alert('Por favor preencha a Mensagem');
            return false;
        }
    });
});
 
/* pega os valores do tamalnho da tela.... */
function f_clientWidth() {
    return f_filterResults (
        window.innerWidth ? window.innerWidth : 0,
        document.documentElement ? document.documentElement.clientWidth : 0,
        document.body ? document.body.clientWidth : 0
    );
}
function f_filterResults(n_win, n_docel, n_body) {
    var n_result = n_win ? n_win : 0;
    if (n_docel && (!n_result || (n_result > n_docel)))
        n_result = n_docel;
    return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
function cauculaWigth(widthPop)
{
    var widthPop = widthPop;
    var widthJanela = f_clientWidth();
    var resultato = (widthJanela-widthPop)/2;
    return resultato;
}
jQuery(function(){
    jQuery('.bt-rss').each(function(){
       jQuery(this).bind('click', function(){
            var left = cauculaWigth(400);
            jQuery('#div-conteiner-pop').hide();
            jQuery('#loading').show();
            jQuery.blockUI({ message: jQuery('#div-loading'),css: { width:'400px', left: left+'px', top:'20%',border:'0;'} });
            jQuery.get(BASE_URI+'noticias/popuprss', null, function(result){
               jQuery('#loading').hide();
               jQuery('#div-conteiner-pop').show();
               jQuery('#div-conteiner-pop').html(result);
            });
            return false;
        })
    })
});
jQuery(function(){
    jQuery('.bt-indique').each(function(){
       jQuery(this).bind('click', function(){
            var left = cauculaWigth(400);
            jQuery('#div-conteiner-pop').hide();
            jQuery('#loading').show();
            jQuery.blockUI({ message: jQuery('#div-loading'),css: { width:'400px', left: left+'px', top:'10%',border:'0;'} });
            jQuery.get(BASE_URI+'contato/indique', null, function(result){
               jQuery('#loading').hide();
               jQuery('#div-conteiner-pop').show();
               jQuery('#div-conteiner-pop').html(result);
               jQuery('#form-indicacao #URL').val(URL_ATUAL);
            });
            return false;
        })
    })
});

jQuery(function(){
    jQuery('.bt-newsletter').each(function(){
       jQuery(this).bind('click', function(){
            var left = cauculaWigth(400);
            jQuery('#div-conteiner-pop').hide();
            jQuery('#loading').show();
            jQuery.blockUI({ message: jQuery('#div-loading'),css: { width:'400px', left: left+'px', top:'10%',border:'0;'} });
            jQuery.get(BASE_URI+'newsletters/', null, function(result){
               jQuery('#loading').hide();
               jQuery('#div-conteiner-pop').show();
               jQuery('#div-conteiner-pop').html(result);
            });
            return false;
        })
    })
});

 $(function(){
    $('#select-vaga').each(function(){
        $(this).find(".newList a").click(function(){
            var ElementoRel = '';
            jQuery('#selecione-uma-vaga :selected').each(function(i, selected){
                ElementoVal = jQuery(selected).val();
            });
            if(ElementoVal != 0){
                var left = cauculaWigth(400);
                jQuery('#div-loading').show();
                jQuery.blockUI({ message: jQuery('#loading'),css: { width:'400px', left: left+'px', top:'20%',border:'0;'} });

                jQuery.post(BASE_URI+'trabalheconosco/trazvaga/'+ElementoVal, null, function(result){
                    jQuery('#div-loading').hide();
                    jQuery.unblockUI();
                    jQuery('#vaga-selecionada').html(result);
                });
                return false;
            }
        });
    });
 });

 $(function(){
    $('.box .gostei').click(function(){

            jQuery(this).blur();
            var ElementoRel = jQuery(this).attr('rel');
            var left = cauculaWigth(400);
            jQuery('#div-loading').show();
            jQuery.blockUI({ message: jQuery('#loading'),css: { width:'400px', left: left+'px', top:'20%',border:'0;'} });

            jQuery.post(BASE_URI+'photos/gostei/'+ElementoRel, null, function(result){
                jQuery('#div-loading').hide();
                jQuery.unblockUI();
                jQuery('.box .gostei').html(result);
            });
            return false;
        });
    });

 $(function(){
    $('.box .download').click(function(){

            jQuery(this).blur();
            var ElementoRel = jQuery(this).attr('rel');
            var ElementoC = jQuery(this).attr('conta');
            jQuery(this).attr('conta',(parseInt(ElementoC)+1));

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

            jQuery.post(BASE_URI+'photos/downloadsX/'+ElementoRel, null, function(result){
                jQuery('#div-loading').hide();
                jQuery.unblockUI();
                jQuery('.box .download').html('('+(parseInt(ElementoC)+1)+')');
            });
        });
    });

/* Checa o e-mail */
function echeck(str) {
    var at="@"
    var dot="."
    var lat=str.indexOf(at)
    var lstr=str.length
    var ldot=str.indexOf(dot)
    if (str.indexOf(at)==-1){
       alert("O e-mail informado é inválido.")
       return false
    }

    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
       alert("O e-mail informado é inválido.")
       return false
    }

    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        alert("O e-mail informado é inválido.")
        return false
    }

     if (str.indexOf(at,(lat+1))!=-1){
        alert("O e-mail informado é inválido.")
        return false
     }

     if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        alert("O e-mail informado é inválido.")
        return false
     }

     if (str.indexOf(dot,(lat+2))==-1){
        alert("O e-mail informado é inválido.")
        return false
     }

     if (str.indexOf(" ")!=-1){
        alert("O e-mail informado é inválido.")
        return false
     }
    return true
}
jQuery(function(){
    jQuery('.banner-top-1').each(function(){
       jQuery(this).bind('click', function(){
            var href = $(this).attr('href')
            jQuery.get(BASE_URI+'votacao/votar', null, function(result){
               //console.log(result)
               //alert(result);
               location.href=href;
            });
            return false;
        })
    })
});
