jQuery(document).ready(function($){
    // Scroll to Top 
	$(window).on('scroll', function() {
		if ($(this).scrollTop() < 100) {
			$('.topbutton') .fadeOut();
		} else {
			$('.topbutton') .fadeIn();
		}
	});

	$(".topbutton").on('click',function() {
		$('html ,body').animate({scrollTop : 0},"slow");
	});

	// navigation
    $(".menu-toggle").click(function() {
        $(".main-navigation .nav-menu").slideToggle("slow","linear");
    });

    // Keyboard navigation
	$(window).on("load resize", function () {
		if ($(window).width() < 992) {
		  $(".main-navigation").find("li").last().bind("keydown", function (e) {
			if (e.which === 9) {
				e.preventDefault();
				$("#masthead").find(".menu-toggle").focus();
			}
		   });
		} else {
		  $(".main-navigation").find("li").unbind("keydown");
		}
    });
	var primary_menu_toggle = $("#masthead .menu-toggle");
	  	primary_menu_toggle.on("keydown", function (e) {
		var tabKey = e.keyCode === 9;
		var shiftKey = e.shiftKey;

		if (primary_menu_toggle.hasClass("show")) {
		  if (shiftKey && tabKey) {
			e.preventDefault();
			$(".main-navigation").toggleClass("toggled");
			primary_menu_toggle.removeClass("show");
		  }
		}
	});
	
	// search
	$(".search-show").on('click',function() {
		$('.search .search-wrap .search-form').toggleClass("active");
   	});
	$(".search-wrap").find(".search-submit").bind("keydown", function (e) {
      var tabKey = e.keyCode === 9;
      if (tabKey) {
        e.preventDefault();
        $(".search-show").focus();
      }
    });
	$(".search-show").on("keydown", function (e) {
		var tabKey = e.keyCode === 9;
		var shiftKey = e.shiftKey;
		if ($(".search-wrap").hasClass("show")) {
		  if (shiftKey && tabKey) {
			e.preventDefault();
			$(".search-wrap").removeClass("show");
			$(".search-show").focus();
		  }
		}
    });
});