$(document).ready(function (){

    showError = function(data)
    {
        $div = $("<div></div>")
            .addClass("yiierror")
            .addClass("error")
            .html(data.message)

       $("#ajaxError")
            .css("display","none")
            .html($div)
            .fadeIn();

       $(".excluir").show();
       $(".excluir").next().hide();
    }

    $(".data").mask("99/99/9999");
    $(".cpf").mask("999.999.999-99");
    $(".cep").mask("99999-999");
    $(".telefone").mask("(99)9999-9999");
    $(".ano").mask("9999");

   $("div.confirmation").animate({
       opacity: 1.0
   },8000).fadeOut();


   $(".item").not(":first").find("div.titulo").next().hide();

   $(".item").find("div.titulo").click(function () {
       $(this).next().slideToggle();
   })

   


   $("a.excluir").click(function (e) {
      $this = $(this);
      e.preventDefault();

      if(confirm("Deseja realmente excluir esse registro?"))
          {
              $.ajax({
                  url:      $this.attr("href"),
                  data:     "ok=ok",
                  type:     "get",
                  dataType: "json",
                  beforeSend:   function() {
                      $this.hide();
                      $this.next().show()
                  },
                  success:      function(data) {
                      if(data.status != 0)
                          {
                              showError(data);
                          } else {
                              $(".item#"+$this.attr("id")).animate({
                                  "height": 0,
                                  "opacity" : 0
                              },500, function() {
                                  $(".item#"+$this.attr("id")).remove();
                                  var $count = $(".item").length;
                                  if(!$count)
                                      {
                                          $div = $("<div></div>")
                                                    .addClass("aviso")
                                                    .html("N&atilde;o h&aacute; registros cadastrados.")
                                          $(".padrao > .clear").append($div);
                                      }
                              })
                          }
                  }

              })
          }
    
   })

   $(".candidatar  a").click(function (e) {
       $this = $(this);
       if(confirm("Deseja realmente se candidatar a esta vaga?"))
           {
               $.ajax({
                   url: $this.attr("href"),
                   data: "OK=OK",
                   type: "get",
                   dataType: "json",
                   beforeSend:   function() {
                      $this.hide();
                      $this.next().show()
                  },
                  success:      function(data) {
                      if(data.status == 'success')
                          {
                              $this.next().hide();
                              //$this.parent().prev().removeClass("hide");

                              var $div = $("<div></div>")
                                .addClass("direito candidatado nomargin")
                                .html(
                                    "<ul><li>Candidatado</li><li class=\"icon\">&nbsp;</li></ul>"
                                );

                             $this.parent().parent().parent().before($div);
                             $this.parent().parent().parent().remove();

                          }
                      if(data.status == 'error')
                          {
                              showError(data);
                              $this.next().hide();
                              $this.show();
                          }
                  }
               })
           }
       e.preventDefault();
   })

   $("a#inativo").colorbox({
       href:    "#inativo-content",
       width:   "50%",
       inline:  true,
       height:  "40%"
   })

})
