/* ==========================================================================
   Augusta Pediatric Associates — Appointments and FAQ

   The contact card beside the copy is stuck to the window by the theme, which
   runs sticky-kit (`stick_in_parent`) over `.thb-fixed`. The theme computes its
   offset as `adminBar.outerHeight() + header.outerHeight()`, and with no admin
   bar on the page that sum is `NaN`, so it falls back to `0` — the card pins
   flush against the top edge of the window.

   This re-runs the same plugin with a real offset, so the card comes to rest a
   little way down the window instead of at the very top. The stop at the bottom
   is sticky-kit's own: it unpins where the column ends, level with the card at
   the foot of the page.
   ========================================================================== */

(function () {
  'use strict';

  var OFFSET = 110;              // where the card comes to rest, from the top
  var SELECTOR = '.apa-info-side > .vc_column-inner.thb-fixed';

  function init() {
    var $ = window.jQuery;
    if (!$ || !$.fn.stick_in_parent) return;

    // the theme leaves this off on small screens, where the column stacks
    if (!window.matchMedia('(min-width: 768px)').matches) return;

    var el = $(SELECTOR);
    if (!el.length) return;

    el.trigger('sticky_kit:detach');
    el.stick_in_parent({
      offset_top: OFFSET,
      spacer: '.sticky-content-spacer',
      recalc_every: 30
    });
  }

  // `load`, not `ready`: the theme starts its own copy on ready, and detaching
  // one that has not been attached yet leaves the card unstuck altogether.
  if (document.readyState === 'complete') init();
  else window.addEventListener('load', init);
})();
