jQuery(document).ready(function ($) {

    $(document).ready(function () {
        butcher_meatshop_navTrap(); // Initialize on document ready
        $(window).on("resize", butcher_meatshop_navTrap); // Handle window resize

        // Toggle menu
        $(".menu-toggle").on("click", function () {
            $(".resp-nav").toggleClass("toggled");
            var expanded = $(this).attr("aria-expanded") === "true";
            $(this).attr("aria-expanded", !expanded);

            if (!expanded) {
                $(".resp-nav .menu-item a").first().focus(); // Move focus inside the menu
            }
        });

        // Focus management for submenus
        $(".main-navigation li.menu-item-has-children, .main-navigation li.page_item_has_children, .resp-nav li.menu-item > a").on("focusin", function () {
            $(this).addClass("focus");
        }).on("focusout", function () {
            $(this).removeClass("focus");
        });
});

function butcher_meatshop_navTrap() {
    var width = $(window).width();
    if (width < 992) {
        $(".main-navigation").off("keydown").on("keydown", function (e) {
            if ($(".resp-nav").hasClass("toggled")) {
                var focusableEls = $(".resp-nav a[href]:not([disabled]), .resp-nav button, .resp-nav li.menu-item > a").toArray();
                var firstFocusableEl = focusableEls[0];
                var lastFocusableEl = focusableEls[focusableEls.length - 1];
                var KEYCODE_TAB = 9;
                var KEYCODE_ESC = 27;

                // Handling Tab and Shift+Tab
                if (e.key === "Tab" || e.keyCode === KEYCODE_TAB) {
                    if (e.shiftKey) { // Shift + Tab
                        if (document.activeElement === firstFocusableEl) {
                            lastFocusableEl.focus();
                            e.preventDefault();
                        }
                    } else { // Tab
                        if (document.activeElement === lastFocusableEl) {
                            firstFocusableEl.focus();
                            e.preventDefault();
                        }
                    }
                }

                // Handling Escape Key
                if (e.keyCode === KEYCODE_ESC) {
                    $(".resp-nav").removeClass("toggled");
                    $(".menu-toggle").attr("aria-expanded", "false").focus();
                    e.preventDefault();
                }
            }
        });
    } else {
        $(".main-navigation").off("keydown");
    }
}

    // Preloader
    jQuery('#preloader').delay(1500).fadeOut();
    jQuery('#loader').delay(1500).fadeOut("slow");

    // Scroll to Top Button
    if ($('.go-to-top').length) {
        var scrollTrigger = $('body').position(); // px
        var goToTop = function () {
            var scrollTop = $(window).scrollTop();
            if (scrollTop > 150) {
                $('.footer-go-to-top').addClass('show');
            } else {
                $('.footer-go-to-top').removeClass('show');
            }
        };
        goToTop();
        $(window).on('scroll', function () {
            goToTop();
        });
        $('.go-to-top').on('click', function (e) {
            e.preventDefault();
            $('html, body').animate({
                scrollTop: scrollTrigger.top
            }, 700);
        });
    }
    // Initialize search focus functionality
    butcher_meatshop_search_focus();

});

// search
function butcher_meatshop_search_focus() {

  /* First and last elements in the menu */
  var butcher_meatshop_search_firstTab = jQuery('.serach_inner input[type="search"]');
  var butcher_meatshop_search_lastTab  = jQuery('button.search-close'); /* Cancel button will always be last */

  jQuery(".search-open").click(function(e){
    e.preventDefault();
    e.stopPropagation();
    jQuery('body').addClass("search-focus");
    butcher_meatshop_search_firstTab.focus();
  });

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

  /* Redirect last tab to first input */
  butcher_meatshop_search_lastTab.on('keydown', function (e) {
    if (jQuery('body').hasClass('search-focus'))
    if ((e.which === 9 && !e.shiftKey)) {
      e.preventDefault();
      butcher_meatshop_search_firstTab.focus();
    }
  });

  /* Redirect first shift+tab to last input*/
  butcher_meatshop_search_firstTab.on('keydown', function (e) {
    if (jQuery('body').hasClass('search-focus'))
    if ((e.which === 9 && e.shiftKey)) {
      e.preventDefault();
      butcher_meatshop_search_lastTab.focus();
    }
  });

  /* Allow escape key to close menu */
  jQuery('.serach_inner').on('keyup', function(e){
    if (jQuery('body').hasClass('search-focus'))
    if (e.keyCode === 27 ) {
      jQuery('body').removeClass('search-focus');
      butcher_meatshop_search_lastTab.focus();
    };
  });
}

jQuery(document).ready(function($) {
    var owl = $('#main-slider-wrap .owl-carousel');
    
    owl.owlCarousel({
        margin: 20,
        rtl: $('html').attr('dir') === 'rtl',
        nav: false, 
        autoplay: false,
        loop: true,
        dots: false,
        responsive: {
            0: { items: 1 },
            600: { items: 1 },
            1000: { items: 1 }
        }
    });

    function butcher_meatshop_updatePagination(index) {
        $('.pagination-item').removeClass('active');
        $('.pagination-item[data-slide="' + index + '"]').addClass('active');
    }

    owl.on('changed.owl.carousel', function(event) {
        var currentIndex = event.item.index - event.relatedTarget._clones.length / 2;
        var totalItems = event.item.count;
        var activeIndex = (currentIndex % totalItems + totalItems) % totalItems;
        butcher_meatshop_updatePagination(activeIndex);
    });

    $('.pagination-item').on('click', function() {
        var slideIndex = $(this).data('slide');
        owl.trigger('to.owl.carousel', [slideIndex, 300]);
    });
});

document.addEventListener('DOMContentLoaded', function() {
    const header = document.querySelector('.sticky-header');
    if (header) { // Check if header exists
        window.addEventListener('scroll', function() {
        if (window.scrollY > 0) {
            header.classList.add('stickyon');
        } else {
            header.classList.remove('stickyon');
        }
        });
    }
});