// ===== Scroll to Top ====
window.addEventListener('scroll', function () {
  const returnToTop = document.getElementById('return-to-top');
  if (returnToTop) {
    if (window.scrollY >= 250) {
      returnToTop.style.display = 'block';
    } else {
      returnToTop.style.display = 'none';
    }
  }
});

const returnToTop = document.getElementById('return-to-top');
if (returnToTop) {
  returnToTop.addEventListener('click', function (e) {
    e.preventDefault();
    window.scrollTo({
      top: 0,
      behavior: 'smooth'
    });
  });
}

// ===== Dynamic Calendar =====
function initCalendar() {
  const monthYearLabel = document.getElementById('calendar-month-year');
  const daysGrid = document.getElementById('calendar-days-grid');
  const prevBtn = document.getElementById('calendar-prev-btn');
  const nextBtn = document.getElementById('calendar-next-btn');

  if (monthYearLabel && daysGrid) {
    const monthNames = [
      "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
      "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"
    ];

    // Get current date
    let currentDate = new Date();
    let currentMonth = currentDate.getMonth();
    let currentYear = currentDate.getFullYear();

    // Store today's date to highlight it
    const today = new Date();
    const todayDay = today.getDate();
    const todayMonth = today.getMonth();
    const todayYear = today.getFullYear();

    function renderCalendar(month, year) {
      // Set month and year title
      monthYearLabel.textContent = `${monthNames[month]} ${year}`;

      // Clear previous days
      daysGrid.innerHTML = '';

      // First day of the month (0 = Sunday, 1 = Monday, etc.)
      const firstDayIndex = new Date(year, month, 1).getDay();

      // Number of days in the month
      const totalDays = new Date(year, month + 1, 0).getDate();

      // Render empty cells for days of the week before the 1st
      for (let i = 0; i < firstDayIndex; i++) {
        const emptyCell = document.createElement('div');
        emptyCell.className = 'empty-cell';
        daysGrid.appendChild(emptyCell);
      }

      // Render days of the month
      for (let day = 1; day <= totalDays; day++) {
        const dayCell = document.createElement('div');
        dayCell.textContent = day;

        // Highlight today
        if (day === todayDay && month === todayMonth && year === todayYear) {
          dayCell.classList.add('today');
        }

        daysGrid.appendChild(dayCell);
      }
    }

    // Event listeners for prev and next buttons
    if (prevBtn) {
      prevBtn.addEventListener('click', function(e) {
        e.preventDefault();
        currentMonth--;
        if (currentMonth < 0) {
          currentMonth = 11;
          currentYear--;
        }
        renderCalendar(currentMonth, currentYear);
      });
    }

    if (nextBtn) {
      nextBtn.addEventListener('click', function(e) {
        e.preventDefault();
        currentMonth++;
        if (currentMonth > 11) {
          currentMonth = 0;
          currentYear++;
        }
        renderCalendar(currentMonth, currentYear);
      });
    }

    // Initial render
    renderCalendar(currentMonth, currentYear);
  }
}

if (document.readyState === 'loading') {
  document.addEventListener('DOMContentLoaded', initCalendar);
} else {
  initCalendar();
}
