// "NOW" clock for the Now page.
//
// A clock face with every hour position labeled NOW. A single smooth-sweep
// second hand orbits forever and never arrives anywhere — because wherever
// it points is now. The clock pulses very gently with each minute.
//
// rAF-driven for smooth motion. Pauses when off-screen.

(function () {
  const hand = document.querySelector("#nowclock .nc-hand");
  if (!hand) return;

  let rafId = null;

  function tick() {
    const d = new Date();
    // Seconds with sub-second precision so the hand sweeps continuously.
    const s = d.getSeconds() + d.getMilliseconds() / 1000;
    const deg = (s / 60) * 360;
    hand.setAttribute("transform", "rotate(" + deg.toFixed(2) + " 100 100)");
    rafId = requestAnimationFrame(tick);
  }

  function start() { if (!rafId) rafId = requestAnimationFrame(tick); }
  function stop()  { if (rafId) cancelAnimationFrame(rafId); rafId = null; }

  const nowView = document.querySelector('.view[data-view="now"]');
  function isActive() { return nowView && nowView.classList.contains("active"); }
  if (nowView && "MutationObserver" in window) {
    new MutationObserver(() => { if (isActive()) start(); else stop(); })
      .observe(nowView, { attributes: true, attributeFilter: ["class"] });
    if (isActive()) start();
  } else {
    start();
  }
  document.addEventListener("visibilitychange", () => {
    if (document.hidden) stop();
    else if (isActive()) start();
  });
})();
