/* ========== MEDIA CENTRE FUNCTIONALITY ========== */
function switchMediaTab(tabName) {
  // Hide all content
  const allContents = document.querySelectorAll(".media-content");
  allContents.forEach((content) => {
    content.classList.remove("active");
  });

  // Remove active class from all tabs
  const allTabs = document.querySelectorAll(".media-tab");
  allTabs.forEach((tab) => {
    tab.classList.remove("active");
  });

  // Show selected content
  const selectedContent = document.getElementById(tabName);
  if (selectedContent) {
    selectedContent.classList.add("active");
  }

  // Add active class to clicked tab
  const selectedTab = document.querySelector(`[data-tab="${tabName}"]`);
  if (selectedTab) {
    selectedTab.classList.add("active");
  }
}

/* ========== FILTER PRESS RELEASES ========== */
function filterPressReleases() {
  const year = document.getElementById("year-select").value;
  const month = document.getElementById("month-select").value;
  const items = document.querySelectorAll(".press-release-item");

  items.forEach((item) => {
    const date = item.getAttribute("data-date");
    let show = true;

    if (year && !date.startsWith(year)) {
      show = false;
    }

    if (month && !date.includes("-" + month + "-")) {
      show = false;
    }

    item.style.display = show ? "grid" : "none";
  });
}

/* ========== PDF MODAL FUNCTIONALITY ========== */
function openPdfModal(documentTitle) {
  const modal = document.getElementById("pdfModal");
  const title = document.getElementById("pdfTitle");
  const pdfPreview = document.getElementById("pdfPreview");

  title.textContent = documentTitle;

  // Using a sample PDF URL - in production, replace with actual PDF URLs
  pdfPreview.data =
    "https://www.w3.org/WAI/WCAG21/Techniques/pdf/img/table.pdf";

  modal.classList.add("active");
}

function closePdfModal() {
  const modal = document.getElementById("pdfModal");
  modal.classList.remove("active");
}

// Close modal when clicking outside
document.getElementById("pdfModal").addEventListener("click", function (event) {
  if (event.target === this) {
    closePdfModal();
  }
});

// Keyboard support for modal
document.addEventListener("keydown", function (event) {
  if (event.key === "Escape") {
    closePdfModal();
  }
});

// Keyboard navigation for tabs
const mediaTabs = document.querySelectorAll(".media-tab");
mediaTabs.forEach((tab, index) => {
  tab.addEventListener("keydown", (e) => {
    if (e.key === "ArrowLeft") {
      const prevIndex = (index - 1 + mediaTabs.length) % mediaTabs.length;
      mediaTabs[prevIndex].click();
      mediaTabs[prevIndex].focus();
    } else if (e.key === "ArrowRight") {
      const nextIndex = (index + 1) % mediaTabs.length;
      mediaTabs[nextIndex].click();
      mediaTabs[nextIndex].focus();
    }
  });
});

/* ========== EVENT REGISTRATION ========== */
function registerEvent(eventName) {
  alert(
    "Thank you for your interest in " +
      "AVENTURINE HOMES" +
      "!\n\nYou will receive a confirmation email shortly with event details and registration link."
  );
}

/* ========== GALLERY FILTER & MODAL ========== */
function filterGallery(type) {
  const items = document.querySelectorAll(".gallery-item");
  const buttons = document.querySelectorAll(".gallery-filter-btn");

  // Update active button
  buttons.forEach((btn) => btn.classList.remove("active"));
  event.target.classList.add("active");

  // Filter items
  items.forEach((item) => {
    if (type === "all" || item.getAttribute("data-type") === type) {
      item.classList.remove("hidden");
    } else {
      item.classList.add("hidden");
    }
  });
}

function openGalleryModal(imageSrc, imageTitle) {
  const modal = document.getElementById("galleryModal");
  const image = document.getElementById("galleryModalImage");
  const title = document.getElementById("galleryModalTitle");

  image.src = imageSrc;
  title.textContent = imageTitle;
  modal.classList.add("active");
}

function closeGalleryModal() {
  const modal = document.getElementById("galleryModal");
  modal.classList.remove("active");
}

function openVideoModal(videoUrl, videoTitle) {
  const modal = document.getElementById("videoModal");
  const iframe = document.getElementById("videoIframe");

  iframe.src = videoUrl;
  modal.classList.add("active");
}

function closeVideoModal() {
  const modal = document.getElementById("videoModal");
  const iframe = document.getElementById("videoIframe");

  iframe.src = "";
  modal.classList.remove("active");
}

// Close modals when clicking outside
document
  .getElementById("galleryModal")
  .addEventListener("click", function (event) {
    if (event.target === this) {
      closeGalleryModal();
    }
  });

document
  .getElementById("videoModal")
  .addEventListener("click", function (event) {
    if (event.target === this) {
      closeVideoModal();
    }
  });

// Keyboard support for modals
document.addEventListener("keydown", function (event) {
  if (event.key === "Escape") {
    closeGalleryModal();
    closeVideoModal();
  }
});

// -----------------------------------------------
function switchCoverageTab(tabName) {
  document
    .querySelectorAll(".coverage-tab")
    .forEach((tab) => tab.classList.remove("active"));
  document.querySelector(`[data-tab="${tabName}"]`).classList.add("active");

  document
    .querySelectorAll(".coverage-list")
    .forEach((list) => list.classList.remove("active"));
  document.getElementById(tabName).classList.add("active");
}

function filterCoverage() {
  let year = document.getElementById("coverage-year").value;
  let category = document.getElementById("coverage-category").value;

  document.querySelectorAll(".coverage-card").forEach((item) => {
    let matchesYear = !year || item.getAttribute("data-year") === year;
    let matchesCategory =
      !category || item.getAttribute("data-category") === category;

    item.style.display = matchesYear && matchesCategory ? "flex" : "none";
  });
}
