/* ==========================================================================
   Augusta Pediatric Associates — the Education Overview, as switch tabs

   Each bio page carried a WPBakery tab set: Undergraduate, Medical School,
   Pediatric Residency, Certifications. It is now the motion library's
   **Switch on Hover Tabs** (02, a measured replica of the Framer Marketplace
   component) — the rail item springs open on hover and the panel changes with
   it, with the two notches welding the active row to the panel.

   `tabswitch.js` and `tabswitch.css` are copied in verbatim. The content comes
   out of the markup that is already on the page rather than being retyped
   here, so every doctor keeps their own schools and years and nothing can
   drift between the nine pages.

   The panel is type, not a photograph: the component renders a non-URL `shot`
   as a plain div, which is exactly the hook needed.
   ========================================================================== */

(function () {
  'use strict';

  function read(container) {
    var titles = container.querySelectorAll('.vc_tta-tabs-list .vc_tta-title-text');
    var panels = container.querySelectorAll('.vc_tta-panel');
    if (!titles.length || titles.length !== panels.length) return null;

    var items = [];
    for (var i = 0; i < titles.length; i++) {
      var body = panels[i].querySelector('.vc_tta-panel-body');
      if (!body) return null;

      /* the panel's own copy, markup and all — these are short lines and
         sometimes several of them, and they are the doctor's own record */
      var html = body.innerHTML
        .replace(/<div[^>]*>/g, '')
        .replace(/<\/div>/g, '')
        .replace(/<span[^>]*>/g, '')
        .replace(/<\/span>/g, '')
        .trim();

      var label = titles[i].textContent.trim();
      items.push({
        title: label,
        body: '',
        shot: '<p class="apa-edu-label">' + label + '</p>' + html
      });
    }
    return items;
  }

  function boot() {
    if (typeof TabSwitch === 'undefined') return;

    var containers = document.querySelectorAll('.vc_tta-container');
    for (var i = 0; i < containers.length; i++) {
      var items = read(containers[i]);
      if (!items) continue;

      var mount = document.createElement('div');
      mount.className = 'tabswitch apa-edu';
      containers[i].parentNode.replaceChild(mount, containers[i]);

      new TabSwitch(mount, { items: items, index: 0, trigger: 'both' });
    }
  }

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