/* ==========================================================================
   Augusta Pediatric Associates — header dropdowns

   The theme binds `mouseover.hoverIntent` / `mouseout.hoverIntent` to every
   parent item and drives a GSAP timeline from it. hoverIntent only calls the
   open handler once the pointer has slowed to under 7px per 100ms sample, so
   sweeping across the menu at a normal speed often opened nothing; and the
   close handler is wired with no timeout, so one `mouseout` bubbling up out of
   a child element shut a panel that had opened.

   This stops hoverIntent from ever claiming the header items, leaving the
   panels to `menu-hover.css`, which opens them on `:hover` and `:focus-within`.
   The browser's own hit testing cannot miss the way a sampling heuristic can.

   The claim is made at parse time — this file sits at the end of `<body>`, so
   it runs before the theme's DOM-ready handler calls `.hoverIntent()`. Undoing
   the binding afterwards was tried first and is not reliable: the theme's menu
   init does not always land before `load`.

   Everything else the theme's menu init does — the `is_right` flip for a panel
   that would run off the right edge, the mobile menu, the mega-menu measuring —
   is bound outside hoverIntent and is left alone.
   ========================================================================== */

(function () {
  'use strict';

  var SELECTOR = '.thb-full-menu li.menu-item-has-children,' +
                 '.thb-header-menu li.menu-item-has-children';

  var $ = window.jQuery;
  if (!$) return;

  // ---- 1 · refuse the binding before it is made ---------------------------
  if ($.fn.hoverIntent && !$.fn.hoverIntent.__apaPatched) {
    var original = $.fn.hoverIntent;

    var patched = function () {
      // Only a set made up entirely of header parents is refused. The theme
      // binds these one item at a time, but it also calls `hoverIntent` on
      // `$('a')` for the link-hover sound, and that set must still get through.
      if (this.length && this.filter(SELECTOR).length === this.length) return this;
      return original.apply(this, arguments);
    };

    patched.__apaPatched = true;
    $.fn.hoverIntent = patched;
  }

  // ---- 2 · and clear anything already bound or already written ------------
  function release() {
    var items = $(SELECTOR);
    if (!items.length) return;

    items.off('.hoverIntent').removeClass('sfHover');

    // Anything GSAP wrote inline would outrank the resting state in the
    // stylesheet, so a panel opened before this ran would stay open.
    items.each(function () {
      var sub = this.querySelector(':scope > .sub-menu');
      if (!sub) return;
      sub.style.removeProperty('opacity');
      sub.style.removeProperty('visibility');
      Array.prototype.forEach.call(sub.querySelectorAll(':scope > li > a'), function (a) {
        a.style.removeProperty('opacity');
        a.style.removeProperty('transform');
      });
    });
  }

  $(release);                                   // DOM ready, after the theme
  window.addEventListener('load', release);     // and again once, later
})();
