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

    $(document).ready(function () {
        seo_specialist_navTrapp(); // Initialize on document ready
        $(window).on('resize', seo_specialist_navTrapp); // Handle window resize
    
        // Event handler for the menu toggle button
        $('.menu-toggle').on('click', function () {
            $('.resp-nav').toggleClass('toggled');
            // Update aria-expanded attribute
            var expanded = $(this).attr('aria-expanded') === 'true';
            $(this).attr('aria-expanded', !expanded);
        });
    
        // Handle focus and blur events to show/hide submenus on focus
        $('.main-navigation ul#responsive-menu li.menu-item-has-children').on('focusin', function () {
            $(this).addClass('focus');
        }).on('focusout', function () {
            $(this).removeClass('focus');
        });
    });
    
    function seo_specialist_navTrapp() {
        var width = $(window).width();
        if (width < 992) {
            $('.resp-nav').off('keydown').on('keydown', function (e) {
                if ($('.resp-nav').hasClass('toggled')) {
                    var focusableEls = $('.resp-nav a[href]:not([disabled]), .resp-nav button');
                    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');
                        // Optionally, you might want to focus the first focusable element when closing
                        firstFocusableEl.focus(); 
                        e.preventDefault(); // Prevent default behavior
                    }
                }
            });
        } else {
            // If window size is larger than 992, remove event listener
            $('.resp-nav').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);
        });
    }

    // Main Slider
    var owl = jQuery('#main-slider-wrap .owl-carousel');
    owl.owlCarousel({
        margin: 20,
        rtl: $('html').attr('dir') === 'rtl',  // Set RTL based on HTML direction
        nav: true,
        autoplay: true,
        lazyLoad: false,
        autoplayTimeout: 2000,
        loop: true,
        dots: false,
        responsive: {
            0: {
                items: 1
            },
            600: {
                items: 1
            },
            1000: {
                items: 1
            }
        },
        autoplayHoverPause: true,
        mouseDrag: true
    });

    // Expert Carousel
    var owlExpert = jQuery('#main-expert-wrap .owl-carousel');
    owlExpert.owlCarousel({
        margin: 20,
        nav: false,
        autoplay: true,
        lazyLoad: false,
        autoplayTimeout: 2000,
        rtl: $('html').attr('dir') === 'rtl',  // Set RTL based on HTML direction
        loop: true,
        dots: false,
        responsive: {
            0: {
                items: 1
            },
            600: {
                items: 3
            },
            1024: {
                items: 3 
            },
            1200: {
                items: 4
            }
        },
        autoplayHoverPause: true,
        mouseDrag: true
    });

    // Initialize search focus functionality
    seo_specialist_search_focus();

});


// search
function seo_specialist_search_focus() {

  /* First and last elements in the menu */
  var seo_specialist_search_firstTab = jQuery('.serach_inner input[type="search"]');
  var seo_specialist_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");
    seo_specialist_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 */
  seo_specialist_search_lastTab.on('keydown', function (e) {
    if (jQuery('body').hasClass('search-focus'))
    if ((e.which === 9 && !e.shiftKey)) {
      e.preventDefault();
      seo_specialist_search_firstTab.focus();
    }
  });

  /* Redirect first shift+tab to last input*/
  seo_specialist_search_firstTab.on('keydown', function (e) {
    if (jQuery('body').hasClass('search-focus'))
    if ((e.which === 9 && e.shiftKey)) {
      e.preventDefault();
      seo_specialist_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');
      seo_specialist_search_lastTab.focus();
    };
  });
}

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');
        }
        });
    }
});