<!-- OFFER ZONE MODULE (795x446px with fade image rotation) -->
<div id="offerZone" style="
  width: 100%;
  max-width: 795px;
  height: 446px;
  background-color: #000;
  position: relative;
  overflow: hidden;
">
</div>

<script>
  const offerFeedURL = "https://communitymarketplace.ca/rss-proxy.php?url=" + encodeURIComponent("https://givebackpromotions.org/index.php?option=com_jbusinessdirectory&task=directoryrss.getOffersRss&companyId=80");
  const zone = document.getElementById("offerZone");
  let images = [];
  let current = 0;

  function createImageElement(src, index) {
    const img = document.createElement("img");
    img.src = src;
    img.style.cssText = "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) => {
        // Try enclosure element first (standard RSS image format)
        const enclosure = item.querySelector("enclosure");
        let imgSrc = enclosure ? enclosure.getAttribute("url") : null;

        // Fallback: look for img tag in description
        if (!imgSrc) {
          const desc = item.querySelector("description");
          if (desc) {
            const match = desc.textContent.match(/<img[^>]+src=["x27]([^"x27]+)/);
            if (match) imgSrc = match[1];
          }
        }

        if (imgSrc) {
          const img = createImageElement(imgSrc, index);
          zone.appendChild(img);
          images.push(img);
        }
      });

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

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