<!-- OFFER ZONE MODULE (745x419px with fade image rotation) -->
<div id="offerZone" style="
  width: 745px;
  height: 419px;
  background-color: #000;
  position: relative;
  overflow: hidden;
">
</div>

<script>
  const offerFeedURL = "https://lafitnessoshawa.givebackpromotions.org/la-fitness-oshawa-sponsors/sponsor-offers?task=directoryrss.getOffersRss";
  const zone = document.getElementById("offerZone");
  let images = [];
  let current = 0;

  function createImageElement(src, index) {
    const img = document.createElement("img");
    img.src = src;
    img.style = `
      position: absolute;
      top: 0; left: 0;
      width: 100%; height: 100%;
      object-fit: cover;
      opacity: ${index === 0 ? 1 : 0};
      transition: opacity 1s ease-in-out;
    `;
    return img;
  }

  function rotateImages() {
    const total = images.length;
    if (total < 2) return;

    images[current].style.opacity = 0;
    current = (current + 1) % total;
    images[current].style.opacity = 1;
  }

  async function loadRSSImages() {
    try {
      const res = await fetch(offerFeedURL);
      const text = await res.text();
      const parser = new DOMParser();
      const xml = parser.parseFromString(text, "text/xml");
      const items = xml.querySelectorAll("item");

      items.forEach((item, index) => {
        const imgTag = item.querySelector("description")?.textContent.match(/<img[^>]+src="([^"]+)"/);
        if (imgTag && imgTag[1]) {
          const img = createImageElement(imgTag[1], index);
          zone.appendChild(img);
          images.push(img);
        }
      });

      if (images.length > 1) {
        setInterval(rotateImages, 15000); // 15 seconds per image
      }
    } catch (e) {
      console.error("RSS feed error:", e);
    }
  }

  document.addEventListener("DOMContentLoaded", loadRSSImages);
</script>

