/* Applies editable site settings (phone/email/address/socials) to the shared
   chrome that common.js injects — the top bar and the footer ONLY.
   It must never touch links inside page content (e.g. a faculty member's own
   email in their bio), so every query is scoped to the .topbar / .footer roots. */
(function () {
  var s = window.NTPPA_SETTINGS;
  if (!s) return;

  function bindIn(root) {
    if (!root) return;
    if (s.phone) root.querySelectorAll('a[href^="tel:"]').forEach(function (a) {
      a.textContent = s.phone; a.setAttribute('href', 'tel:' + s.phone.replace(/[^0-9+]/g, ''));
    });
    if (s.email) root.querySelectorAll('a[href^="mailto:"]').forEach(function (a) {
      a.textContent = s.email; a.setAttribute('href', 'mailto:' + s.email);
    });
    if (s.facebook) root.querySelectorAll('a[href*="facebook.com"]').forEach(function (a) { a.setAttribute('href', s.facebook); });
    if (s.youtube)  root.querySelectorAll('a[href*="youtube.com"]').forEach(function (a) { a.setAttribute('href', s.youtube); });
  }

  function apply() {
    try {
      // scope strictly to the injected chrome — NOT document-wide
      bindIn(document.querySelector('.topbar'));
      bindIn(document.querySelector('.footer'));
      var addr = document.querySelector('.topbar-addr');
      if (addr && s.address) addr.innerHTML = s.address.replace(/,\s*/g, '<br>');
    } catch (e) {}
  }

  if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', function () { setTimeout(apply, 60); });
  else setTimeout(apply, 60);
})();
