document.addEventListener('DOMContentLoaded', function () {
    const menuLinks = document.querySelectorAll('.navbar-nav .menu-item-has-children > a');

    menuLinks.forEach(function (link) {
        link.setAttribute('aria-haspopup', 'true');
        link.setAttribute('aria-expanded', 'false');

        link.addEventListener('focus', function () {
            let submenu = link.nextElementSibling;
            if (submenu) submenu.style.display = 'block';
        });

        link.addEventListener('blur', function () {
            let submenu = link.nextElementSibling;
            if (submenu) submenu.style.display = 'none';
        });

        // Keyboard down arrow support
        link.addEventListener('keydown', function (e) {
            if (e.key === 'ArrowDown') {
                e.preventDefault();
                let submenu = link.nextElementSibling;
                if (submenu) {
                    submenu.style.display = 'block';
                    submenu.querySelector('a').focus();
                }
            }
        });
    });
});


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

    // Handle toggle for submenu
    $('.primary-menu .menu-item-has-children > a').on('click', function(e){
      var $parentLi = $(this).parent('li');

      if($parentLi.find('.sub-menu').length){
        e.preventDefault(); // stop link

        // Close siblings
        $parentLi.siblings().removeClass('open').find('.sub-menu').slideUp(200);

        // Toggle current
        $parentLi.toggleClass('open');
        $parentLi.children('.sub-menu').first().stop(true,true).slideToggle(200);
      }
    });

  });
})(jQuery);