(function () {
  'use strict';

  var MOBILE_BP = 980;

  function isMobileNav() {
    return window.matchMedia('(max-width: ' + MOBILE_BP + 'px)').matches;
  }

  function initNav() {
    var toggle = document.querySelector('.solar-nav-toggle');
    var nav = document.getElementById('main-navigation');
    var overlay = document.querySelector('.solar-nav-overlay');

    if (!nav) {
      return;
    }

    var parents = nav.querySelectorAll('.menu-item-has-children, .page_item_has_children');

    function setOpen(open) {
      nav.classList.toggle('is-open', open);
      document.body.classList.toggle('solar-nav-open', open);

      if (!open) {
        nav.querySelectorAll('.submenu-open').forEach(function (li) {
          li.classList.remove('submenu-open');
        });
        clearHover();
      }

      if (toggle) {
        toggle.setAttribute('aria-expanded', open ? 'true' : 'false');
        toggle.classList.toggle('is-active', open);
      }
      if (overlay) {
        overlay.setAttribute('aria-hidden', open ? 'false' : 'true');
      }
    }

    function closeMobileNav() {
      setOpen(false);
    }

    function clearHover() {
      nav.querySelectorAll('.is-hover').forEach(function (li) {
        li.classList.remove('is-hover');
      });
    }

    function bindDesktopHover() {
      parents.forEach(function (li) {
        li.onmouseenter = function () {
          if (isMobileNav()) {
            return;
          }
          li.classList.add('is-hover');
        };
        li.onmouseleave = function () {
          if (isMobileNav()) {
            return;
          }
          li.classList.remove('is-hover');
        };
      });
    }

    if (toggle) {
      toggle.addEventListener('click', function (event) {
        event.preventDefault();
        event.stopPropagation();
        if (!isMobileNav()) {
          return;
        }
        setOpen(!nav.classList.contains('is-open'));
      });
    }

    if (overlay) {
      overlay.addEventListener('click', function (event) {
        event.preventDefault();
        closeMobileNav();
      });
    }

    var drawerClose = nav.querySelector('.solar-nav-drawer-close');
    if (drawerClose) {
      drawerClose.addEventListener('click', function (event) {
        event.preventDefault();
        event.stopPropagation();
        closeMobileNav();
      });
    }

    parents.forEach(function (li) {
      var link = null;
      var child = li.firstElementChild;
      while (child) {
        if (child.tagName === 'A') {
          link = child;
          break;
        }
        child = child.nextElementSibling;
      }
      if (!link) {
        return;
      }

      link.addEventListener('click', function (event) {
        if (!isMobileNav() || !nav.classList.contains('is-open')) {
          return;
        }

        // Parent items only toggle the dropdown — never navigate or close the drawer.
        event.preventDefault();
        event.stopPropagation();

        var willOpen = !li.classList.contains('submenu-open');

        Array.prototype.forEach.call(li.parentElement.children, function (sibling) {
          if (sibling !== li && sibling.classList) {
            sibling.classList.remove('submenu-open');
            sibling.querySelectorAll('.submenu-open').forEach(function (nested) {
              nested.classList.remove('submenu-open');
            });
          }
        });

        if (willOpen) {
          li.classList.add('submenu-open');
        } else {
          li.classList.remove('submenu-open');
          li.querySelectorAll('.submenu-open').forEach(function (nested) {
            nested.classList.remove('submenu-open');
          });
        }
      });
    });

    // Keep drawer taps from bubbling through to the dim overlay.
    nav.addEventListener('click', function (event) {
      event.stopPropagation();
    });

    document.addEventListener('keyup', function (event) {
      if (event.key !== 'Escape' || !nav.classList.contains('is-open')) {
        return;
      }

      var openSub = nav.querySelector('.submenu-open');
      if (openSub) {
        openSub.classList.remove('submenu-open');
        openSub.querySelectorAll('.submenu-open').forEach(function (nested) {
          nested.classList.remove('submenu-open');
        });
        return;
      }

      closeMobileNav();
    });

    var resizeTimer = null;
    window.addEventListener('resize', function () {
      window.clearTimeout(resizeTimer);
      resizeTimer = window.setTimeout(function () {
        if (!isMobileNav()) {
          closeMobileNav();
        } else {
          clearHover();
        }
      }, 100);
    });

    bindDesktopHover();
  }

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', initNav);
  } else {
    initNav();
  }
})();
