// taglines.js — rotate the masthead tagline on every page load
// If the masthead has data-locked-tagline, uses that (locked articles).
// Otherwise picks randomly from config/taglines.json.
// Always writes the chosen tagline to data-current-tagline for lock capture.
// Falls back to hardcoded HTML if fetch fails or JS is disabled.

(function () {
  var el = document.querySelector('.masthead .tagline');
  if (!el) return;

  var masthead = document.querySelector('.masthead');

  // If article is locked, use the locked tagline — do not rotate.
  var locked = masthead && masthead.getAttribute('data-locked-tagline');
  if (locked) {
    el.textContent = locked;
    if (masthead) masthead.setAttribute('data-current-tagline', locked);
    return;
  }

  // Resolve path to config/taglines.json from any depth in the site.
  var scripts = document.querySelectorAll('script[src*="taglines.js"]');
  var base = '';
  if (scripts.length) {
    var src = scripts[scripts.length - 1].getAttribute('src');
    base = src.replace('taglines.js', '');
  }

  // Inline taglines — no fetch needed, works on file:// and HTTP equally
  var TAGLINES = [
    "Proving conclusively that AI is not funny.",
    "Est. Ben \u201cJammin\u201d Franklin",
    "Reporting Events Since Before They Occurred",
    "Chronology Is For Mere Mortals",
    "We Cannot Prove Benjamin Franklin Founded This Publication",
    "Independently Verified By Staff Who Prefer Not To Be Named",
    "All The News That Fits, And Some That Does Not",
    "Trusted By Readers Who Have Run Out Of Other Options",
    "Accuracy Is Our Goal. It Is Not Always Our Achievement.",
    "We Reached Out For Comment. No One Has Responded.",
    "Est. Approx. Records Lost In A Fire. We Cannot Confirm.",
    "Your Source For Events Described To Us By Confident People",
    "The News, Mostly",
    "Committed To The Story, Whatever It Turns Out To Be",
    "We Have Standards. We Review Them Periodically.",
    "All Facts Subject To The Availability Of Facts",
    "Named After A Greek Creature For Reasons We Stand By",
    "The Satyr Knows What It Is Doing. The Satyr Has Notes.",
    "Journalism, In The Broadest Reasonable Sense",
    "We Were There. Or Someone Was.",
    "Not Responsible For Physics",
    "Your Source For Events Described To Us By Confident AI",
    "Pure Artificial. Zero Intelligence.",
    "No Forest-Dwelling God Can Extinguish This Dumpster Fire.",
    "Something Ain't Right With That Horned Beast",
    "Satyr Considers Humans A Sustainable Food Source",
    "Every Sunday Is A Holiday, Somewhere",
    "The Buttal That Inspired A Rebuttal",
    "Free, But Worth Less"
  ];

  var pick = TAGLINES[Math.floor(Math.random() * TAGLINES.length)];
  el.textContent = pick;
  if (masthead) masthead.setAttribute('data-current-tagline', pick);

  // ── Publication dateline ──────────────────────────────────────────────────
  // On locked articles: shows locked pub date. On live articles: live date+time.
  var dateline = document.getElementById('pub-dateline');
  if (dateline) {
    var lockedDateline = masthead && masthead.getAttribute('data-locked-dateline');
    var lockedDate     = masthead && masthead.getAttribute('data-locked-pubdate');
    if (lockedDateline) {
      // Locked article — show exact frozen dateline (including milliseconds)
      dateline.textContent = lockedDateline;
    } else if (lockedDate) {
      // Fallback: locked but no dateline captured — show pub date only
      dateline.textContent = lockedDate;
    } else {
      // Live — show current date + time with seconds, ms, timezone — three rows
      var now = new Date();
      var dateStr = now.toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' });
      var timeStr = now.toLocaleTimeString('en-US', { hour: 'numeric', minute: '2-digit', second: '2-digit', hour12: true });
      var tz      = now.toLocaleTimeString('en-US', { timeZoneName: 'short' }).split(' ').pop();
      var ms      = String(now.getMilliseconds()).padStart(3, '0') + ' milliseconds';
      dateline.innerHTML =
        '<span class="dl-date">' + dateStr + '</span>' +
        '<span class="dl-time">' + timeStr + ' ' + tz + '</span>' +
        '<span class="dl-ms">' + ms + '</span>';
      var full = dateStr + ' \u00b7 ' + timeStr + ' ' + tz + ' \u00b7 ' + ms;
      if (masthead) masthead.setAttribute('data-current-dateline', full);
    }
  }

  // Brand watermark is now burned directly into image files.
})();
