// Menu
function home_tutor_openNav() {
  jQuery(".sidenav").addClass('show');
}
function home_tutor_closeNav() {
  jQuery(".sidenav").removeClass('show');
}

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

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

      if ( ! shiftKey && tabKey && home_tutor_lastEl === home_tutor_activeEl ) {
        e.preventDefault();
        home_tutor_firstEl.focus();
      }

      if ( shiftKey && tabKey && home_tutor_firstEl === home_tutor_activeEl ) {
        e.preventDefault();
        home_tutor_lastEl.focus();
      }
    } );
  }
  home_tutor_keepFocusInMenu();
} )( window, document );

// scroll
jQuery(document).ready(function () {
	jQuery(window).scroll(function () {
    if (jQuery(this).scrollTop() > 0) {
      jQuery('#scrollbutton').fadeIn();
    } else {
      jQuery('#scrollbutton').fadeOut();
    }
	});
	jQuery(window).on("scroll", function () {
	  document.getElementById("scrollbutton").style.display = "block";
	});
	jQuery('#scrollbutton').click(function () {
    jQuery("html, body").animate({
      scrollTop: 0
    }, 600);
    return false;
	});
});

// preloader
jQuery(function($){
  setTimeout(function(){
    $(".frame").delay(1000).fadeOut("slow");
  });
});

// Header search
jQuery('document').ready(function($){
  $('.main-search span a').click(function(){
    $(".searchform_page").slideDown(500);
    home_tutor_Keyboard_loop($('.searchform_page'));
  });

  $('.home-tutor-close a').click(function(){
    $(".searchform_page").slideUp(500);
  });
});

var home_tutor_Keyboard_loop = function (elem) {
  var home_tutor_tabbable = elem.find('select, input, textarea, button, a').filter(':visible');

  var home_tutor_firstTabbable = home_tutor_tabbable.first();
  var home_tutor_lastTabbable = home_tutor_tabbable.last();
  home_tutor_firstTabbable.focus();

  /*redirect last tab to first input*/
  home_tutor_lastTabbable.on('keydown', function (e) {
    if ((e.which === 9 && !e.shiftKey)) {
      e.preventDefault();
      home_tutor_firstTabbable.focus();
    }
  });

  /*redirect first shift+tab to last input*/
  home_tutor_firstTabbable.on('keydown', function (e) {
    if ((e.which === 9 && e.shiftKey)) {
      e.preventDefault();
      home_tutor_lastTabbable.focus();
    }
  });

  /* allow escape key to close insiders div */
  elem.on('keyup', function (e) {
    if (e.keyCode === 27) {
      elem.hide();
    };
  });
};

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

  var home_tutor_scrollTop = window.scrollY || document.documentElement.scrollTop;
  var home_tutor_windowHeight = window.innerHeight;
  var home_tutor_documentHeight = document.documentElement.scrollHeight;

  var home_tutor_isBottom = home_tutor_scrollTop + home_tutor_windowHeight >= home_tutor_documentHeight - 100;

  if (home_tutor_scrollTop >= 100 && !home_tutor_isBottom) {
    home_tutor_sticky.classList.add('copyright-fixed');
  } else {
    home_tutor_sticky.classList.remove('copyright-fixed');
  }
});

// Title Color
jQuery(() => {
  jQuery("#banner .banner-content .banner-title").each(function () {
    const w = jQuery(this).text().trim().split(" ");
    jQuery(this).html(
      w.length > 1
        ? `<span class="highlight-text">${w.slice(0,2).join(" ")}</span> ${w.slice(2).join(" ")}`
        : w[0]
    );
  });
});
