// Menus 
function vw_locksmith_menu_open_nav() {
  jQuery(".sidenav").addClass('show');
}
function vw_locksmith_menu_close_nav() {
  jQuery(".sidenav").removeClass('show');
}

( function( window, document ) {
  function vw_locksmith_keepFocusInMenu() {
    document.addEventListener( 'keydown', function( e ) {
      const vw_locksmith_nav = document.querySelector( '.sidenav' );

      if ( ! vw_locksmith_nav || ! vw_locksmith_nav.classList.contains( 'show' ) ) {
        return;
      }
      const elements = [...vw_locksmith_nav.querySelectorAll( 'input, a, button' )],
        vw_locksmith_lastEl = elements[ elements.length - 1 ],
        vw_locksmith_firstEl = elements[0],
        vw_locksmith_activeEl = document.activeElement,
        tabKey = e.keyCode === 9,
        shiftKey = e.shiftKey;

      if ( ! shiftKey && tabKey && vw_locksmith_lastEl === vw_locksmith_activeEl ) {
        e.preventDefault();
        vw_locksmith_firstEl.focus();
      }

      if ( shiftKey && tabKey && vw_locksmith_firstEl === vw_locksmith_activeEl ) {
        e.preventDefault();
        vw_locksmith_lastEl.focus();
      }
    } );
  }
  vw_locksmith_keepFocusInMenu();
  } )( window, document );

  jQuery('document').ready(function($){
  // preloader
  setTimeout(function () {
    jQuery("#preloader").fadeOut("slow");
  },1000);

  /*sticky sidebar*/
  window.addEventListener('scroll', function() {
    var sticky = document.querySelector('.sidebar-sticky');
    if (!sticky) return;

    var scrollTop = window.scrollY || document.documentElement.scrollTop;
    var windowHeight = window.innerHeight;
    var documentHeight = document.documentElement.scrollHeight;

    var isBottom = scrollTop + windowHeight >= documentHeight-100;

    if (scrollTop >= 100 && !isBottom) {
      sticky.classList.add('sidebar-fixed');
    } else {
      sticky.classList.remove('sidebar-fixed');
    }
  });

  // Sticky Header
  $(window).scroll(function(){
		var sticky = $('.header-sticky'),
			scroll = $(window).scrollTop();

		if (scroll >= 100) sticky.addClass('header-fixed');
		else sticky.removeClass('header-fixed');
	});
  // Sticky Copyright
  $(window).scroll(function(){
    var sticky = $('.copyright-sticky'),
      scroll = $(window).scrollTop();

    if (scroll >= 100) sticky.addClass('copyright-fixed');
    else sticky.removeClass('copyright-fixed');
  });
});

// search
jQuery(document).ready(function () {
    function vw_locksmith_search_loop_focus(element) {
    var vw_locksmith_focus = element.find('select, input, textarea, button, a[href]');
    var vw_locksmith_firstFocus = vw_locksmith_focus[0];  
    var vw_locksmith_lastFocus = vw_locksmith_focus[vw_locksmith_focus.length - 1];
    var KEYCODE_TAB = 9;

    element.on('keydown', function vw_locksmith_search_loop_focus(e) {
      var isTabPressed = (e.key === 'Tab' || e.keyCode === KEYCODE_TAB);

      if (!isTabPressed) { 
        return; 
      }

      if ( e.shiftKey ) /* shift + tab */ {
        if (document.activeElement === vw_locksmith_firstFocus) {
          vw_locksmith_lastFocus.focus();
            e.preventDefault();
          }
        } else /* tab */ {
        if (document.activeElement === vw_locksmith_lastFocus) {
          vw_locksmith_firstFocus.focus();
            e.preventDefault();
          }
        }
    });
  }
  jQuery('.search-box span a').click(function(){
      jQuery(".serach_outer").slideDown(1000);
      vw_locksmith_search_loop_focus(jQuery('.serach_outer'));
  });

  jQuery('.closepop a').click(function(){
      jQuery(".serach_outer").slideUp(1000);
  });
});

// Scroller
jQuery(document).ready(function () {
	jQuery(window).scroll(function () {
    if (jQuery(this).scrollTop() > 100) {
      jQuery('.scrollup i').fadeIn();
    } else {
      jQuery('.scrollup i').fadeOut();
    }
	});
	jQuery('.scrollup i').click(function () {
    jQuery("html, body").animate({
      scrollTop: 0
    }, 600);
    return false;
	});
});