//General funtion to submit all forms via ajax if is no action defined
$(document).on("submit", 'form', function() {

  if($(this).attr('action') == undefined){

    var formData = new FormData($(this)[0]);

    $("body").addClass("loading");

    $.ajax({
      url: '/app/elements/functions/actions.php',

      type: 'POST',

      data: formData,

      cache: false,

      contentType: false,

      processData: false,

      success: function(response){

        $("body").removeClass("loading");

        $('#resposta').append(response);

        setTimeout(function() {  $('#resposta').html('');}, 10000);

      }
    });

    return false;

  }
});

//To make Active item menu
$(function(){
  var current = location.pathname;
  $('#sidebar-menu li a').each(function(){
    var $this = $(this);
    //If the current path is like this link, make it active

    if($this.attr('href') == current){
      $this.addClass('active');
      $this.closest("ul").show();
      $this.closest("ul").closest("li").find('a').first().addClass('active').addClass('subdrop');
    }
  });
});

$(document).on("click", '.notification-message a', function() {
  var id = $(this).data('id');
  $.ajax({
      url: '/app/elements/functions/actions.php',
      type: 'POST',
      data: {'acao' : 'ler_notificacao', 'id' : id }
    });
});

$(document).on("click", '.clear-noti', function() {
  $.ajax({
      url: '/app/elements/functions/actions.php',
      type: 'POST',
      data: {'acao' : 'limpar_notificacao'},
      success: function(response){
        $('#resposta').append(response);
        setTimeout(function() {  $('#resposta').html('');}, 10000);
      }
    });
});
