document.addEventListener('DOMContentLoaded', function() {
// Get current date
const currentDate = new Date();
// For testing, you can set a specific date like this:
// const currentDate = new Date('2025-03-28'); // Uncomment to test USA 2025 on last day
// Get tab elements
const nextTab = document.querySelector('#next');
const upcomingTab = document.querySelector('#upcoming');
const pastTab = document.querySelector('#past');
const eventGrid = document.querySelector('.event-grid');
// Get all event cards from the upcoming and past tabs (initially)
const allEvents = Array.from(upcomingTab.querySelectorAll('.event-card')).concat(
Array.from(pastTab.querySelectorAll('.event-card'))
);
// Clear tabs and grid to reassign events
nextTab.innerHTML = '';
upcomingTab.innerHTML = '';
pastTab.innerHTML = '';
eventGrid.innerHTML = '';
// Separate events into upcoming and past based on current date
const upcomingEvents = [];
const pastEvents = [];
allEvents.forEach(event => {
const endDate = new Date(event.getAttribute('data-end-date'));
const startDate = new Date(event.getAttribute('data-start-date'));
// Set endDate to the end of the day (23:59:59) to include the full last day
endDate.setHours(23, 59, 59, 999);
if (endDate < currentDate) {
// Event has fully ended (after the last day)
event.classList.remove('upcoming', 'next');
event.classList.add('past');
pastEvents.push(event);
} else {
// Event is still ongoing or upcoming
event.classList.remove('past');
event.classList.add('upcoming');
upcomingEvents.push(event);
}
});
// Sort upcoming events by start date (ascending)
upcomingEvents.sort((a, b) => {
const dateA = new Date(a.getAttribute('data-start-date'));
const dateB = new Date(b.getAttribute('data-start-date'));
return dateA - dateB;
});
// Sort past events by start date (descending for most recent first)
pastEvents.sort((a, b) => {
const dateA = new Date(a.getAttribute('data-start-date'));
const dateB = new Date(b.getAttribute('data-start-date'));
return dateB - dateA;
});
// Set the "Next" event (first upcoming event, including ongoing events)
if (upcomingEvents.length > 0) {
const nextEvent = upcomingEvents.shift(); // Remove and get the first event
nextEvent.classList.remove('upcoming');
nextEvent.classList.add('next');
nextTab.appendChild(nextEvent.cloneNode(true));
} else {
nextTab.innerHTML = '