(function ($) {
  'use strict';

  // ______________ Sidebar
  $(document).ready(function () {
    $('.app-sidebar a').each(function () {
      var pageUrl = window.location.href.split(/[?#]/)[0];
      if (this.href == pageUrl) {
        $(this).addClass('active');
        $(this)
          .parent()
          .addClass('active'); // add active to li of the current link
        $(this)
          .parent()
          .parent()
          .prev()
          .addClass('active'); // add active class to an anchor
        $(this)
          .parent()
          .parent()
          .prev()
          .click(); // click the item to make it drop
      }
    });
  });

  // ______________Full screen
  $('#fullscreen-button').on('click', function toggleFullScreen() {
    if (
      (document.fullScreenElement !== undefined &&
        document.fullScreenElement === null) ||
      (document.msFullscreenElement !== undefined &&
        document.msFullscreenElement === null) ||
      (document.mozFullScreen !== undefined && !document.mozFullScreen) ||
      (document.webkitIsFullScreen !== undefined &&
        !document.webkitIsFullScreen)
    ) {
      if (document.documentElement.requestFullScreen) {
        document.documentElement.requestFullScreen();
      } else if (document.documentElement.mozRequestFullScreen) {
        document.documentElement.mozRequestFullScreen();
      } else if (document.documentElement.webkitRequestFullScreen) {
        document.documentElement.webkitRequestFullScreen(
          Element.ALLOW_KEYBOARD_INPUT
        );
      } else if (document.documentElement.msRequestFullscreen) {
        document.documentElement.msRequestFullscreen();
      }
    } else {
      if (document.cancelFullScreen) {
        document.cancelFullScreen();
      } else if (document.mozCancelFullScreen) {
        document.mozCancelFullScreen();
      } else if (document.webkitCancelFullScreen) {
        document.webkitCancelFullScreen();
      } else if (document.msExitFullscreen) {
        document.msExitFullscreen();
      }
    }
  });

  // ______________mCustomScrollbar
  $('.vscroll').mCustomScrollbar();
  $('.app-sidebar').mCustomScrollbar({
    theme: 'minimal',
    autoHideScrollbar: true,
    scrollbarPosition: 'outside'
  });

  // ______________Loader
  $(window).on('load', function (e) {
    $('#global-loader').fadeOut('slow');
  });

  // ______________Cover Image
  $('.cover-image').each(function () {
    var attr = $(this).attr('data-image-src');
    if (typeof attr !== typeof undefined && attr !== false) {
      $(this).css('background', 'url(' + attr + ') center center');
    }
  });

  // ______________ Back to Top
  $(window).on('scroll', function (e) {
    if ($(this).scrollTop() > 0) {
      $('#back-to-top').fadeIn('slow');
    } else {
      $('#back-to-top').fadeOut('slow');
    }
  });
  $('#back-to-top').on('click', function (e) {
    $('html, body').animate(
      {
        scrollTop: 0
      },
      600
    );
    return false;
  });


  var toggleSidebar = function () {
    var w = $(window);
    if (w.outerWidth() <= 1024) {
      $('body').addClass('sidebar-gone');
      $(document)
        .off('click', 'body')
        .on('click', 'body', function (e) {
          if (
            $(e.target).hasClass('sidebar-show') ||
            $(e.target).hasClass('search-show')
          ) {
            $('body').removeClass('sidebar-show');
            $('body').addClass('sidebar-gone');
            $('body').removeClass('search-show');
          }
        });
    } else {
      $('body').removeClass('sidebar-gone');
    }
  };
  toggleSidebar();
  $(window).resize(toggleSidebar);




  // ______________Dropdown
  $('.dropdown-toggle').dropdown();
})(jQuery);
