(function(){ const timerEl = document.getElementById('timer'); const daysEl = document.getElementById('days'); const hoursEl = document.getElementById('hours'); const minutesEl = document.getElementById('minutes'); const secondsEl = document.getElementById('seconds'); function nowCST() { const now = new Date(); return new Date(now.toLocaleString('en-US', { timeZone: 'America/Chicago' })); } function nextEvent(dayOfWeek) { const cst = nowCST(); const target = new Date(cst); target.setDate(target.getDate() + ((dayOfWeek - target.getDay() + 7) % 7)); target.setHours(11,0,0,0); return target; } function getModeAndTarget() { const cstNow = nowCST(); const satStart = nextEvent(6); const sunStart = nextEvent(0); const satEnd = new Date(satStart); satEnd.setHours(18,0,0,0); const sunEnd = new Date(sunStart); sunEnd.setHours(18,0,0,0); if (cstNow >= satStart && cstNow < satEnd) return { mode: 'event', target: satEnd }; if (cstNow >= sunStart && cstNow < sunEnd) return { mode: 'event', target: sunEnd }; const candidates = [satStart, sunStart].filter(t => t > cstNow); if (candidates.length === 0) { satStart.setDate(satStart.getDate()+7); candidates.push(satStart); } candidates.sort((a,b)=>a-b); return { mode: 'countdown', target: candidates[0] }; } function pad(n, digits = 2) { return String(n).padStart(digits,'0'); } function update() { const { mode, target } = getModeAndTarget(); const cstNow = nowCST(); let diff = target - cstNow; if (diff < 0) diff = 0; const seconds = Math.floor((diff / 1000) % 60); const minutes = Math.floor((diff / (1000*60)) % 60); const hours = Math.floor((diff / (1000*60*60)) % 24); const days = Math.floor(diff / (1000*60*60*24)); daysEl.textContent = String(days); hoursEl.textContent = pad(hours); minutesEl.textContent = pad(minutes); secondsEl.textContent = pad(seconds); if (mode === 'event') timerEl.classList.add('rainbow'); else timerEl.classList.remove('rainbow'); } update(); setTimeout(function(){ update(); setInterval(update,1000); }, 1000 - (Date.now()%1000)); })();