// Navigation
$("#menu-toggle-open").click(function(e) {
       $("#wrapper-xs").hide();
       $("#wrapper-lg").show();
});

$("#menu-toggle-close").click(function(e) {
      $("#wrapper-xs").show();
       $("#wrapper-lg").hide();
});

$("#menu-burger-open").click(function(e) {
       $("#wrapper-mobile").show();
       $("#menu-burger-open").hide();
       $("#menu-burger-close").show();
});

$("#menu-mobile-toggle-close").click(function(e) {
      $("#menu-burger-close").hide();
      $("#menu-burger-open").show();
      $("#wrapper-mobile").hide();
});

$("#menu-burger-close").click(function(e) {
      $("#menu-burger-close").hide();
      $("#menu-burger-open").show();
      $("#wrapper-mobile").hide();
});

function openNav() {
    document.getElementById("main").style.marginLeft = "200px";
}

function closeNav() {
    document.getElementById("main").style.marginLeft= "60px";
}

// dataTables
$(document).ready( function () {
    $('#myTable').DataTable();
} );

// Popover
$('.popover-aide').popover({ trigger: "hover" });

// Modal multi step
sendEvent = function(sel, step) {
    $(sel).trigger('next.m.' + step);
}

// Modal Edit mode
function hide(target) {
    document.getElementById(target).style.display = 'none';
}

function show(target) {
    document.getElementById(target).style.display = 'inline-block';
}

// Form with steps
$(document).ready(function () {
  var navListItems = $('div.setup-panel div a'),
          allWells = $('.setup-content'),
          allNextBtn = $('.nextBtn'),
  		  allPrevBtn = $('.prevBtn');

  allWells.hide();

  navListItems.click(function (e) {
      e.preventDefault();
      var $target = $($(this).attr('href')),
              $item = $(this);

      if (!$item.hasClass('disabled')) {
          navListItems.removeClass('btn-primary').addClass('btn-default');
          $item.addClass('btn-primary');
          allWells.hide();
          $target.show();
          $target.find('input:eq(0)').focus();
      }
  });

  allPrevBtn.click(function(){
      var curStep = $(this).closest(".setup-content"),
          curStepBtn = curStep.attr("id"),
          prevStepWizard = $('div.setup-panel div a[href="#' + curStepBtn + '"]').parent().prev().children("a");

          prevStepWizard.removeAttr('disabled').trigger('click');
  });

  allNextBtn.click(function(){
      var curStep = $(this).closest(".setup-content"),
          curStepBtn = curStep.attr("id"),
          nextStepWizard = $('div.setup-panel div a[href="#' + curStepBtn + '"]').parent().next().children("a"),
          curInputs = curStep.find("input[type='text'],input[type='url'],input[type='password'],input[type='checkbox']"),
          isValid = true;

      $(".form-group").removeClass("has-error");
      for(var i=0; i<curInputs.length; i++){
          if (!curInputs[i].validity.valid){
              isValid = false;
              $(curInputs[i]).closest(".form-group").addClass("has-error");
          }
      }

      if (isValid)
          nextStepWizard.removeAttr('disabled').trigger('click');
  });

  $('div.setup-panel div a.btn-primary').trigger('click');
});
