/**
 * Theme Js file.
 */

jQuery(function ($) {
    "use strict";

    // Make menu functions global for inline onclick
    window.shopping_cart_woocommerce_menu_open_nav = function () {
        $("#mySidenav").addClass("open");
    };

    window.shopping_cart_woocommerce_menu_close_nav = function () {
        $("#mySidenav").removeClass("open");
    };

    // Search focus handler
    function searchFocusHandler() {
        const searchFirstTab = $('.inner_searchbox input[type="search"]');
        const searchLastTab = $('button.search-close');

        $(".open-search").on("click", function (e) {
            e.preventDefault();
            e.stopPropagation();
            $("body").addClass("search-focus");
            searchFirstTab.focus();
        });

        $("button.search-close").on("click", function (e) {
            e.preventDefault();
            e.stopPropagation();
            $("body").removeClass("search-focus");
            $(".open-search").focus();
        });

        searchLastTab.on("keydown", function (e) {
            if ($("body").hasClass("search-focus") && e.which === 9 && !e.shiftKey) {
                e.preventDefault();
                searchFirstTab.focus();
            }
        });

        searchFirstTab.on("keydown", function (e) {
            if ($("body").hasClass("search-focus") && e.which === 9 && e.shiftKey) {
                e.preventDefault();
                searchLastTab.focus();
            }
        });

        $(".inner_searchbox").on("keyup", function (e) {
            if ($("body").hasClass("search-focus") && e.keyCode === 27) {
                $("body").removeClass("search-focus");
                searchLastTab.focus();
            }
        });
    }

    searchFocusHandler();

    // Loader
    setTimeout(function () {
        $(".loader").fadeOut("slow");
    }, 1000);

    // Return to top
    $(window).on("scroll", function () {
        if ($(this).scrollTop() >= 50) {
            $("#return-to-top").fadeIn(200);
        } else {
            $("#return-to-top").fadeOut(200);
        }
    });

    $("#return-to-top").on("click", function () {
        $("body, html").animate({
            scrollTop: 0
        }, 500);
    });

    // Sticky header
    $(window).on("scroll", function () {
        var data_sticky = $(".innermenuboxupper").attr("data-sticky");

        if (data_sticky === "true") {
            if ($(this).scrollTop() > 1) {
                $(".innermenuboxupper").addClass("stick_head");
            } else {
                $(".innermenuboxupper").removeClass("stick_head");
            }
        }
    });
});