// Update intervals (in milliseconds) const INTERVALS = { TIME: 1000, // 1 second WEATHER: 900000, // 15 minutes CALENDAR: 300000, // 5 minutes BACKGROUND: 300000, // 5 minutes JOKE: 3600000 // 1 hour }; // Weather icon mapping (OpenWeatherMap icons to emoji or text) const WEATHER_ICONS = { '01d': '☀️', '01n': '🌙', '02d': '⛅', '02n': '☁️', '03d': '☁️', '03n': '☁️', '04d': '☁️', '04n': '☁️', '09d': '🌧️', '09n': '🌧️', '10d': '🌦️', '10n': '🌧️', '11d': '⛈️', '11n': '⛈️', '13d': '❄️', '13n': '❄️', '50d': '🌫️', '50n': '🌫️' }; // Initialize the application document.addEventListener('DOMContentLoaded', () => { // Start all update functions updateTime(); updateWeather(); updateCalendar(); updateBackground(); updateJoke(); // Set up intervals setInterval(updateTime, INTERVALS.TIME); setInterval(updateWeather, INTERVALS.WEATHER); setInterval(updateCalendar, INTERVALS.CALENDAR); setInterval(updateBackground, INTERVALS.BACKGROUND); setInterval(updateJoke, INTERVALS.JOKE); }); // Update time and date function updateTime() { const now = new Date(); // Format time (HH:MM) const hours = String(now.getHours()).padStart(2, '0'); const minutes = String(now.getMinutes()).padStart(2, '0'); document.getElementById('time').textContent = `${hours}:${minutes}`; // Format date (Day, Month Date) const options = { weekday: 'long', month: 'long', day: 'numeric' }; const dateString = now.toLocaleDateString('en-NZ', options); document.getElementById('date').textContent = dateString; } // Fetch and update weather async function updateWeather() { try { const response = await fetch('/api/weather'); if (!response.ok) throw new Error('Weather fetch failed'); const data = await response.json(); if (data.error) { console.error('Weather error:', data.error); return; } // Update current weather const { current, forecast } = data; document.getElementById('current-temp').textContent = `${current.temp}°`; document.getElementById('weather-icon').textContent = WEATHER_ICONS[current.icon] || '🌤️'; document.getElementById('weather-description').textContent = current.description; document.getElementById('feels-like').textContent = `${current.feels_like}°`; document.getElementById('humidity').textContent = `${current.humidity}%`; document.getElementById('wind-speed').textContent = `${current.wind_speed} km/h`; // Update forecast const forecastContainer = document.getElementById('forecast-container'); forecastContainer.innerHTML = ''; forecast.forEach(day => { const dayElement = document.createElement('div'); dayElement.className = 'forecast-day'; dayElement.innerHTML = `