( function( window, document ) {

  function electronics_equipment_keepFocusInSidebar() {
    document.addEventListener( 'keydown', function( e ) {
      const electronics_equipment_nav = document.querySelector( '.header-widgets' );

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

      if ( ! shiftKey && tabKey && electronics_equipment_lastEl === electronics_equipment_activeEl ) {
        e.preventDefault();
        electronics_equipment_firstEl.focus();
      }

      if ( shiftKey && tabKey && electronics_equipment_firstEl === electronics_equipment_activeEl ) {
        e.preventDefault();
        electronics_equipment_lastEl.focus();
      }
    } );
  }

  electronics_equipment_keepFocusInSidebar();
} )( window, document );

function electronics_equipment_openSidebar() {
  jQuery(".header-widgets").addClass('show');
}
function electronics_equipment_closeSidebar() {
  jQuery(".header-widgets").removeClass('show');
}

// Menu Functions
function electronics_equipment_openNav() {
  jQuery(".sidenav").addClass('show');
}

function electronics_equipment_closeNav() {
  jQuery(".sidenav").removeClass('show');
}

/////////////////////// Focus handling ///////////////////////
(function(window, document) {
  function electronics_equipment_handleMobileMenuNavigation() {
    document.addEventListener('keydown', function(e) {
      if (window.innerWidth > 991) return;
      const nav = document.querySelector('.sidenav.show');
      if (!nav) return;
      const focusableElements = Array.from(nav.querySelectorAll(
        'a, button, [tabindex="0"], input, [tabindex]:not([tabindex="-1"])'
      )).filter(el => el.offsetParent !== null);

      if (focusableElements.length === 0) return;

      const firstElement = focusableElements[0];
      const lastElement = focusableElements[focusableElements.length - 1];
      const activeElement = document.activeElement;

      if (e.key === 'Tab') {
        if (!e.shiftKey && activeElement === lastElement) {
          e.preventDefault();
          firstElement.focus();
        } 
        else if (e.shiftKey && activeElement === firstElement) {
          e.preventDefault();
          lastElement.focus();
        }
        else if (!nav.contains(activeElement)) {
          e.preventDefault();
          firstElement.focus();
        }
        return;
      }

      if (e.key === 'Tab' && e.shiftKey) {
        const activeElement = document.activeElement;

        if (activeElement.closest('.dropdown-menu')) {
          e.preventDefault();
          
          //current submenu
          const currentSubmenu = activeElement.closest('.dropdown-menu');
          const submenuItems = Array.from(currentSubmenu.querySelectorAll('a, button, [tabindex="0"]'))
            .filter(el => el.offsetParent !== null);
          const currentIndex = submenuItems.indexOf(activeElement);
          if (currentIndex > 0) {
            submenuItems[currentIndex - 1].focus();
          } else {
            const parentDropdown = currentSubmenu.closest('.dropdown, .page_item_has_children');
            if (parentDropdown) {
              // Find all focusable elements in parent
              const allFocusable = Array.from(parentDropdown.querySelectorAll('a, button, [tabindex="0"]'))
                .filter(el => el.offsetParent !== null);
              
              // Filter to only direct children of parentDropdown
              const parentItems = allFocusable.filter(el => el.parentElement === parentDropdown);
              
              if (parentItems.length > 0) {
                parentItems[0].focus();
              }
            }
          }
        }
      }
    });
  }

  document.addEventListener('DOMContentLoaded', function() {
    electronics_equipment_handleMobileMenuNavigation();

    document.addEventListener('focusin', function(e) {
      if (window.innerWidth > 991) return;
      
      const focusedItem = e.target;
      const submenu = focusedItem.closest('.dropdown-menu');
      if (submenu) {
        submenu.style.display = 'block';
        submenu.classList.add('show');
      }
    });
  });
})(window, document);

/////////////////////// end ///////////////////////

var electronics_equipment_btn = jQuery('#button');

jQuery(window).scroll(function() {
  if (jQuery(window).scrollTop() > 300) {
    electronics_equipment_btn.addClass('show');
  } else {
    electronics_equipment_btn.removeClass('show');
  }
});

electronics_equipment_btn.on('click', function(e) {
  e.preventDefault();
  jQuery('html, body').animate({scrollTop:0}, '300');
});

jQuery(document).ready(function() {
    var owl = jQuery('#top-slider .owl-carousel');
    var sliderCount = parseInt( owl.data('slide-count'), 10 ) || 1;
    var sliderNav   = sliderCount > 1;
    owl.owlCarousel({
    margin: 0,
    nav: sliderNav,
    autoplay : sliderCount > 1,
    lazyLoad: false,
    autoplayTimeout: 5000,
    loop: sliderCount > 1,
    dots: false,
    navText : ['<i class="fas fa-arrow-left" aria-hidden="true"></i>','<i class="fas fa-arrow-right" aria-hidden="true"></i>'],
    responsive: {
      0: {
        items: 1
      },
      576: {
        items: 1
      },
      768: {
        items: 1
      },
      1000: {
        items: 1
      },
      1200: {
        items: 1
      }
    },
    autoplayHoverPause : false,
    mouseDrag: true,
  });

 if(document.getElementById("top-slider")) {   // Select the node that will be observed for mutations
     const targetNode = document.getElementById("top-slider");
 
     // Options for the observer (which mutations to observe)
     const config = { attributes: true, childList: true, subtree: true };
 
     // No-op callback (count-box removed in new slider design)
     const callback = (mutationList, observer) => {};
 
     // Create an observer instance linked to the callback function
     const observer = new MutationObserver(callback);
 
     // Start observing the target node for configured mutations
     observer.observe(targetNode, config);}

})

window.addEventListener('load', (event) => {
  jQuery(".loading").delay(2000).fadeOut("slow");
  jQuery(".loading2").delay(2000).fadeOut("slow");
});

jQuery('.header-search-wrapper .search-main').click(function(e){
  e.stopPropagation(); // prevent triggering document click
  jQuery('.search-form-main').toggleClass('active-search');
  jQuery('.search-form-main .search-field').focus();
});

// prevent closing when clicking inside search form
jQuery('.search-form-main').click(function(e){
  e.stopPropagation();
});

// close when clicking outside
jQuery(document).click(function(){
  jQuery('.search-form-main').removeClass('active-search');
});