var centro;
var meuMapa;
var marcador;
var nivelZoom;
var tipoDoMapa;
var vUltimaString = '';
var vQtdeExecutada = 0;

$(document).ready(function()
{
  
  $('#sim-opn').click(function(){
    
    votar('S'); 
  });
  $('#nao-opn').click(function(){
    
    
     votar('N');
  });
  
$.printa_fundo_box_ajuda_erro = function(){

$("input,select,textarea,checkbox").bind('focus', function(){
var tmp = $(this).parent('.bloco-cada-linha-input');

if ($(this).attr('type') == 'radio') // se for radio
var tmp = $(this).parent().parent('.bloco-cada-linha-input');
if ($(this).attr('type') == 'checkbox') // se for checkbox
var tmp = $(this).parent().parent().parent().parent().parent();

// mostra aba de ajuda/erro
$(tmp).children('.bloco-cada-linha-erro').fadeOut(function(){
$(tmp).children('.bloco-cada-linha-ajuda').fadeIn();
});

$(tmp).children('.bloco-cada-linha-erro-invertido')
.fadeOut(
function()
{
$(tmp).children('.bloco-cada-linha-ajuda-invertido')
.fadeIn();
});

if ($(this).hasClass('com-fundo')
&& $(this).attr('type') != 'checkbox')
{
$(tmp).css('background', '#FCE48A');
}
/*
* else { // Não faz nada }
*/
});

$("input,select,textarea,checkbox").bind('blur', function()
{
var tmp = $(this).parent('.bloco-cada-linha-input');

if ($(this).attr('type') == 'radio') // se for radio
var tmp = $(this).parent().parent('.bloco-cada-linha-input');
if ($(this).attr('type') == 'checkbox') // se for checkbox
var tmp = $(this).parent().parent().parent().parent().parent();


// esconde aba de ajuda/erro
if (!$(tmp).children('.bloco-cada-linha-erro').is(':hidden'))
{
$(tmp).children('.bloco-cada-linha-ajuda').hide();
$(tmp).children('.bloco-cada-linha-erro').fadeOut();

}
else
{
$(tmp).children('.bloco-cada-linha-ajuda').fadeOut(function()
{
$(tmp).children('.bloco-cada-linha-erro').fadeOut();
});
}

if (!$(tmp).children('.bloco-cada-linha-erro-invertido').is(
':hidden'))
{
$(tmp).children('.bloco-cada-linha-ajuda-invertido').hide();
$(tmp).children('.bloco-cada-linha-erro-invertido').fadeOut();

}
else
{
$(tmp).children('.bloco-cada-linha-ajuda-invertido').fadeOut(
function()
{
$(tmp).children('.bloco-cada-linha-erro-invertido')
.fadeOut();
});
}

if ($(this).hasClass('com-fundo'))
{
$(tmp).css('background', '#F2F1E5');
}
else
{
$(tmp).css('background', '#FFF5E1');
}
if ($(this).hasClass('sem-fundo'))
{
  $(tmp).css('background', '#FFF');
}
});
}
// <!-- FUNDO AMARELO && HELP's -->
  
  
  $('#campo_estado').change(function()
  {
    $('#campo_cidade').attr('disabled', 'disabled');
    $('#campo_cidade').html('<option>Selecione...</option>');
    if ($('#campo_estado').attr('value'))
    {
      $('#campo_cidade').html('<option>Carregando...</option>');
      $.post('ajax/buscaCidadesAJAX.php',
      {
        uf_id : $(this).attr('value')
      }, function(data)
      {
        $('#campo_cidade').html('<option>Selecione...</option>');
        if (data.erro)
        {
          alert(data.erro);
        }
        else
        {
          $('#campo_cidade').removeAttr('disabled');
          var tam = (data.cidades).length;
          for (i = 0; i < tam; i++)
          {
            var html = $('#campo_cidade').html();
            $('#campo_cidade').html(
                html + '<option value="' + data.cidades[i].id + '">'
                    + data.cidades[i].nome + '</option>');
          }
        }
      }, "json");
    }
  });

 $("#campo_area").autocomplete("ajax/listaAreaAJAX.php",
  {
    whidth : 260,
    selectFist : false
  });
  
  area = 1;
  $("#campo_area").blur(function (area){
    var area = $("#campo_area").val(); 
  $("#campo_funcao").autocomplete("ajax/listaFuncoesAJAX.php?area="+area,
  {
    whidth : 260,
    selectFist : false
  });
 });

   $('#campo_tipo_vaga').change(function()
  {

    if ($(this).attr('value') == "Estágio")
    {
      $('#quantidadeVagas').hide(); 
      $('#div_vigencia').hide();
      $('#funcao').show();
      $('#campo_funcao').val('Estágio');
      $('#campo_funcao').attr('disabled', 'disabled');
      $('#campo_tipo_remuneracao').val('Bolsa de Estágio');
      $('#campo_situacao02').attr('checked', 'checked');
      $('#campo_situacao02').attr('disable', 'disable');
      $('#funcao_hidden').val("Estágio");
    }
    else
    {

      if ($(this).attr('value') == "Temporário"){
        $('#div_vigencia').show();
        $('#funcao_hidden').val('1');
        $('#quantidadeVagas').hide();
        $('#funcao').hide();
        
      }
      else{
      $('#quantidadeVagas').show();
      $('#div_vigencia').hide();
      $('#funcao_hidden').val('1');
      $('#funcao').hide();
      $('#desconhecidapeloform').hide();
      $('#campo_funcao').attr('value', '');
      $('#campo_funcao').removeAttr('disabled');
      $('#campo_tipo_remuneracao').val('Selecione...');
      $('#campo_situacao02').removeAttr('checked');
    }
    }
  });
  
  $('#campo_data').mask('99/99/9999');
  $('#campo_cpf').mask('999.999.999-99');
  $('#campo_telefone').mask('(99) 9999-9999');
  $('#campo_telefoneC').mask('(99) 9999-9999');
  
  $('#btnCandidatar').click(function()
  {
    if($('#campo_cpf').attr('value'))
    {
      $.post('ajax/queroEstaVagaAJAX.php', { cpf: $('#campo_cpf').attr('value'), idVaga: $('#idVaga').attr('value') }, function(data)
      {
        if (data.sucess == 1)
          alert("Você se candidatou para essa vaga com sucesso");

        if (data.sucess == 2)
          alert("Cpf nao cadastrado");

        if (data.sucess == 3)
          alert("Você ja se candidatou para esta vaga");

        $('#campo_cpf').attr('value', '');

      }, "json");
  }
  else
    alert('Informe o Cpf');
  });

  $('#form-cadastra-empresa').submit(function()
  {
    $('.bloco-cada-linha-ajuda').fadeOut();
    $('.bloco-cada-linha-erro').fadeOut();
    $('#bnt-concluir-cadastra-empresa').fadeOut(
        function()
        {
          $('#loading').fadeIn();
          $.post('ajax/cadastraEmpresaAJAX.php', $(
              '#form-cadastra-empresa').serialize(), function(data)
          {

            if (data.erro)
            {
              if (data.campo != 'campo_tipo')
              {
                var tmp = $('#' + data.campo).parent().children(
                    '.bloco-cada-linha-erro');
                $('#' + data.campo).focus();
              }
              else
                var tmp = $('#' + data.campo + '01').parent().parent()
                    .children('.bloco-cada-linha-erro');

              $(tmp).html((unescape(data.erro)).replace('+', ' '));
              $(tmp).fadeIn();

              $('#loading').fadeOut(function()
              {
                $('#bnt-concluir-cadastra-empresa').fadeIn();
              });
            }
            else
            {
              if (data.sucess)
              {

                $('#loading').fadeOut(
                    function()
                    {
                      $('#bnt-concluir-cadastra-empresa').fadeIn(
                          function()
                          {
                            $('#sucess').fadeIn();
                            document.getElementById('form-cadastra-empresa').reset();
                          });
                    });
              }
              else
              {
                alert(data.msg);
                $('#loading').fadeOut(
                    function()
                    {
                      $('#bnt-concluir-cadastra-empresa').fadeIn();
                      document.getElementById('form-cadastra-empresa')
                          .reset();
                    });
              }
            }
          }, "json");
        });

    return false;
  });

  $('#form-publique-vaga').submit(function()
  {

    $.post('ajax/publicaVagaAJAX.php', $('#form-publique-vaga')
        .serialize(), function(data)
    {

      if (data.erro)
      {
        if (data.campo != 'campo_tipo_vaga'
            && data.campo != 'campo_situacao')
        {
          var tmp = $('#' + data.campo).parent().children(
              '.bloco-cada-linha-erro');
          $('#' + data.campo).focus();
        }
        else
          var tmp = $('#' + data.campo + '01').parent().parent()
              .children('.bloco-cada-linha-erro');

        $(tmp).html((unescape(data.erro)).replace('+', ' '));
        $(tmp).fadeIn();
      }
      else
      {

        if (data.sucess)
        {
          alert("Vaga Publicada com sucesso");
        }
        document.getElementById('form-publique-vaga').reset();
        $('#campo_funcao').removeAttr('disabled');
        $('#div_razao_social').show();
        $('#div_telefone').show();
        $('#div_tipo').show();
        $('#div_cidade').show();
        $('#div_estado').show();
        $('#div_vigencia').hide();

        $('a[href=#top]').click(function()
        {
          $('html, body').animate(
          {
            scrollTop : 0
          }, 'slow');
          return false;
        });

      }
    }, "json");
    return false;
  });

  $('#campo_email').blur(function()
  {
    $.post('ajax/buscaDadosUsuarioAdmAJAX.php',
    {
      email : $('#campo_email').attr('value')
    }, function(data)
    {

      // <!--Se existir o e-mail cadastrado ele preenche o
      // campo nome esconde os campos ... e seta valores
      // nulos para os mesmos-->
      if (data.sucess)
      {
        $('#campo_nome').val(data.usuarioInfo.nome);

        $('#data_nascText').hide();
        $('#data_nascInp').hide();
        $('#campo_dataNasc').val('null');

        $('#sexoText').hide();
        $('#sexoInp').hide();
        $('#campo_sexo01').attr('checked', 'checked');

        $('#estadoText').hide();
        $('#estadoInp').hide();
        $('#campo_estado').val('null');

        $('#cidadeText').hide();
        $('#cidadeInp').hide();
        $('#campo_cidade').val('null');
        $('#campo_tipo').val('null');

        $('#campo_nome').blur();
        $('#campo_telefone').focus();
      }
    }, "json");
  });

  $('#campo_cnpj').blur(function()
  {

    $.post('ajax/buscaDadosUsuarioAdmCNPJAJAX.php',
    {
      cnpj : $('#campo_cnpj').attr('value')
    }, function(data)
    {

      // <!--Se existir o e-mail cadastrado ele preenche o
      // campo nome esconde os campos ... e seta valores
      // nulos para os mesmos-->
      if (data.sucess)
      {

        $('#div_razao_social').hide();
        $('#razao_social').val('null');

        $('#div_telefone').hide();
        $('#campo_telefone').val('null');

        $('#div_tipo').hide();
        $('#campo_tipo').val('null');

        $('#div_cidade').hide();
        $('#campo_cidade').val('null');

        $('#div_estado').hide();
        $('#campo_estado').val('null');

        $('#empresa').val('1');

      }
      else
      {

      }

    }, "json");
  });

  $('#form-contato').submit(function()
  {
    $('#btnEviarMensagem').fadeOut(
        function()
        {
          $.post('ajax/realizaContatoAJAX.php', $('#form-contato')
              .serialize(), function(data)
          {

            if (data.erro)
            {
              if (data.campo != 'campo_tipo')
              {
                var tmp = $('#' + data.campo).parent().children(
                    '.bloco-cada-linha-erro');
                $('#' + data.campo).focus();
              }
              else
                var tmp = $('#' + data.campo + '01').parent().parent()
                    .children('.bloco-cada-linha-erro');

              $(tmp).html((unescape(data.erro)).replace('+', ' '));
              $(tmp).fadeIn();
              $('#btnEviarMensagem').fadeIn();
            }
            else
            {
              alert("Contato enviado com sucesso");
              document.getElementById('form-contato').reset();

              $('#data_nascText').show();
              $('#data_nascInp').show();

              $('#sexoText').show();
              $('#sexoInp').show();

              $('#estadoText').show();
              $('#estadoInp').show();

              $('#cidadeText').show();
              $('#cidadeInp').show();

              $('#btnEviarMensagem').fadeIn();

            }

          }, "json");

        });
    // alert("teste");
    return false;

  });

  $('#idSelectMaisVagas').change(function()
  {
    $.get('ajax.php',
    {
      fnc : 'BlocoMaisVagas',
      vlr : $(this).attr('value')
    }, function(pRetorno)
    {
      $('#idBlocoDeVagas').empty().html(pRetorno);
    });
  });

});//$(document).ready(function()

function votar(voto){
  
   $.post("ajax/votoAJAX.php", {voto:voto}, function(retornoAjax){
     
     if(retornoAjax.retorno){
       
       alert('Obrigado pelo voto');
     }
     else{
       
       alert('Obrigado você ja votou');
      
     }
     
     $('#container-bloco-pesquisa-opn').hide();
     
   },'json');
}

// Função para cadastrar Newsletter por Ajax usando jQuery
function CadastrarNewsletter(pFormulario)
{
  if ((pFormulario.edtNome.value.trim() == '')
      || (pFormulario.edtNome.value.trim() == 'nome'))
  {
    exibirMensagem('Informe o seu nome!');
    pFormulario.edtNome.value = '';
    pFormulario.edtNome.focus();
    return false;
  }

  if ((pFormulario.edtEmail.value.trim() == '')
      || (pFormulario.edtEmail.value.trim() == 'email'))
  {
    exibirMensagem('Informe o seu e-mail!');
    pFormulario.edtEmail.value = '';
    pFormulario.edtEmail.focus();
    return false;
  }

  $.get('cadastrarNewsletter.php',
  {
    nome : pFormulario.edtNome.value.trim(),
    email : pFormulario.edtEmail.value.trim()
  }, function(pRetorno)
  {
  if (pRetorno.Mensagem != "")
    {
      exibirMensagem(pRetorno.Mensagem);
      pFormulario.edtNome.value = 'nome';
      pFormulario.edtEmail.value = 'email';
    }
  }, "json");
}

// Função para apagar o campo quando o usuário entrar no campo
function onFocusNewsLetter(pCampo, pPadrao)
{
  if (pCampo.value.trim() == pPadrao)
    pCampo.value = '';
}

// Função para preencher o campo quando o usuário sair no campo
function onBlurNewsLetter(pCampo, pPadrao)
{
  if (pCampo.value.trim() == '')
    pCampo.value = pPadrao;
}

function exibirMensagem(pMensagem)
{
  alert(pMensagem);
}

function criarMapa()
{ // metodo que carrega o mapa padrao
  centro = new GLatLng(-20.454927, -54.603945); // seta as coordenadas
  // padrão: agencia 13 de
  // maio, campo grande
  nivelZoom = 16; // define o nivel de zoom - 0-17
  tipoDoMapa = G_NORMAL_MAP; // seta o tipo de mapa. Tem tbem G_SATELLITE_MAP
  // e G_HYBRID_MAP
  reiniciar(); // executa as configurações setadas
}

function reiniciar()
{
  marcador = null;
  marcador = new GMarker(centro); // seta o marcador padrao sobre a coordenada
  // designada
  meuMapa = null;
  meuMapa = new GMap2(document.getElementById("idDivMapa")); // instancia o
  // mapa na div
  // mapa1
  meuMapa.setCenter(centro, nivelZoom); // seta a visualização central do
  // mapa
  meuMapa.setMapType(tipoDoMapa); // seta o tipo de mapa
  meuMapa.addOverlay(marcador); // seta um novo marcador sobre a coordenada
}

// metodo que realiza a mudança do mapa
function mudaLatLong($lat, long)
{
  centro = null;
  centro = new GLatLng($lat, long); // captura a nova coordenada
  reiniciar(); // executa a mudança
}
