/* ---------- header / mobile menu ---------- */
function placeHeader() {
var isMobile = window.matchMedia('(max-width: 767px)').matches;
var target = isMobile
? document.querySelector('.header-menu-actions')
: document.querySelector('.header-actions--right');
if (!target || target.querySelector('.thm-consult-btn')) { return; }
/* Drop a stale copy from the other slot so a resize can't leave two. */
var stale = document.querySelectorAll('.thm-consult-btn.thm-in-header, .thm-consult-btn.thm-in-menu');
for (var i = 0; i < stale.length; i++) {
if (stale[i].parentElement !== target) { stale[i].parentNode.removeChild(stale[i]); }
}
target.appendChild(makeButton(isMobile ? 'thm-in-menu' : 'thm-in-header'));
}
/* ---------- telehealth page ---------- */
function onSectionPage() {
var path = window.location.pathname.replace(/\/+$/, '').toLowerCase();
for (var i = 0; i < SECTION_PATHS.length; i++) {
if (path === SECTION_PATHS[i].replace(/\/+$/, '').toLowerCase()) { return true; }
}
return false;
}
/* {el, mode} — 'in' appends inside el, 'after' inserts as a sibling. */
function findAnchor() {
var el;
if (PLACE_AFTER) {
el = document.querySelector(PLACE_AFTER);
if (el) { return { el: el, mode: 'after' }; }
}
/* The dedicated section on the telehealth page. It is a Fluid Engine
section: an empty CSS-Grid container. Mount INSIDE the grid, not on
the , or the button has no grid placement and never shows. */
el = document.querySelector('#telehealth-page-btn-section .fluid-engine');
if (el) { return { el: el, mode: 'in' }; }
/* Same section, older non-fluid markup. */
el = document.querySelector('#telehealth-page-btn-section .content-wrapper .content');
if (el) { return { el: el, mode: 'in' }; }
el = document.getElementById('telehealth-page-btn-section');
if (el) { return { el: el, mode: 'in' }; }
/* Under the page's first real heading. No #sections wrapper exists on
this template, so match .page-section directly. */
el = document.querySelector('.page-section h1, .page-section h2');
if (el) { return { el: el, mode: 'after' }; }
/* Last resort: end of the first section's content. */
el = document.querySelector('.page-section .content-wrapper');
if (el) { return { el: el, mode: 'in' }; }
return null;
}
function placeSection() {
if (!onSectionPage()) { return; }
if (document.querySelector('.thm-consult-btn.thm-in-section')) { return; }
var anchor = findAnchor();
if (!anchor) { return; }
var wrap = document.createElement('div');
wrap.className = 'thm-consult-wrap';
wrap.appendChild(makeButton('thm-in-section'));
if (anchor.mode === 'in') {
anchor.el.appendChild(wrap);
} else if (anchor.el.parentNode) {
anchor.el.parentNode.insertBefore(wrap, anchor.el.nextSibling);
}
}
function run() { placeHeader(); placeSection(); }
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', run);
} else {
run();
}
/* Squarespace re-renders the header on resize and loads pages over
AJAX, so re-assert placement rather than assuming one pass. */
window.addEventListener('resize', run);
window.addEventListener('mercury:load', run);
new MutationObserver(run).observe(document.documentElement, { childList: true, subtree: true });
})();