<div class="scroll-animation-section" 
     data-pixels-per-image="{{ section.settings.pixels_per_image | default: 30 }}"
     data-total-images="{{ section.settings.total_images | default: 118 }}"
     data-start-image="{{ section.settings.start_image | default: 1 }}"
     data-desktop-image-base="{{ section.settings.desktop_image_url_base | default: 'https://cdn.shopify.com/s/files/1/0655/8256/3385/files/Exploded_desktop' }}"
     data-mobile-image-base="{{ section.settings.mobile_image_url_base | default: 'https://cdn.shopify.com/s/files/1/0655/8256/3385/files/Exploded_mobile' }}"
     data-image-extension="{{ section.settings.image_extension | default: '.jpg' }}"
     style="
       background-color: {{ section.settings.background_color }};
       margin-top: {{ section.settings.desktop_margin_top }}px;
       margin-bottom: {{ section.settings.desktop_margin_bottom }}px;
     ">
  
  <div class="scroll-animation-container">
    <div class="scroll-animation-sticky">
      <div class="scroll-animation-image-container">
        <img class="scroll-animation-image desktop-image" src="{{ section.settings.desktop_image_url_base | default: 'https://cdn.shopify.com/s/files/1/0655/8256/3385/files/Exploded_desktop' }}{{ section.settings.start_image | default: 1 | prepend: '000' | slice: -3, 3 }}{{ section.settings.image_extension | default: '.jpg' }}" alt="Animation frame">  
        <img class="scroll-animation-image mobile-image" src="{{ section.settings.mobile_image_url_base | default: 'https://cdn.shopify.com/s/files/1/0655/8256/3385/files/Exploded_mobile' }}{{ section.settings.start_image | default: 1 | prepend: '000' | slice: -3, 3 }}{{ section.settings.image_extension | default: '.jpg' }}" alt="Animation frame">
        
        <!-- Desktop Content -->
        <div class="video-banner-content desktop-content scroll-trigger animate--slide-in desktop-{{ section.settings.desktop_text_position_vertical }} desktop-{{ section.settings.desktop_text_position_horizontal }}"
             style="
               text-align: {{ section.settings.desktop_text_align }};
               color: {{ section.settings.desktop_text_color }};
               width: {{ section.settings.desktop_text_width }}%;
             ">
          <div class="video-banner-heading">
            <h2 class="h3" style="font-size: {{ section.settings.desktop_heading_size }}px; color: {{ section.settings.desktop_heading_color }};">{{ section.settings.desktop_heading }}</h2>
          </div>
          <div class="video-banner-Content" style="font-size: {{ section.settings.desktop_content_size }}px;">
            {{ section.settings.desktop_content }}
          </div>
        </div>
        
        <!-- Mobile Content -->
        <div class="video-banner-content mobile-content scroll-trigger animate--slide-in mobile-{{ section.settings.mobile_text_position_vertical }} mobile-{{ section.settings.mobile_text_position_horizontal }}"
             style="
               text-align: {{ section.settings.mobile_text_align }};
               color: {{ section.settings.mobile_text_color }};
               width: {{ section.settings.mobile_text_width }}%;
             ">
          <div class="video-banner-heading">
            <h2 class="h3" style="font-size: {{ section.settings.mobile_heading_size }}px; color: {{ section.settings.mobile_heading_color }};">{{ section.settings.mobile_heading }}</h2>
          </div>
          <div class="video-banner-Content" style="font-size: {{ section.settings.mobile_content_size }}px;">
            {{ section.settings.mobile_content }}
          </div>
        </div>
        
        <!-- Desktop Overlay Badge -->
        {% if section.settings.desktop_overlay_image %}
          <div class="overlay-badge desktop-overlay-badge" 
               style="
                 top: {{ section.settings.desktop_overlay_top }}%;
                 {% if section.settings.desktop_overlay_horizontal_position == 'left' %}
                   left: {{ section.settings.desktop_overlay_horizontal_value }}%;
                 {% else %}
                   right: {{ section.settings.desktop_overlay_horizontal_value }}%;
                 {% endif %}
                 width: {{ section.settings.desktop_overlay_width }}px;
               ">
            <img src="{{ section.settings.desktop_overlay_image | img_url: 'master' }}" alt="Overlay badge" loading="lazy">
          </div>
        {% endif %}
        
        <!-- Mobile Overlay Badge -->
        {% if section.settings.mobile_overlay_image %}
          <div class="overlay-badge mobile-overlay-badge" 
               style="
                 top: {{ section.settings.mobile_overlay_top }}%;
                 {% if section.settings.mobile_overlay_horizontal_position == 'left' %}
                   left: {{ section.settings.mobile_overlay_horizontal_value }}%;
                 {% else %}
                   right: {{ section.settings.mobile_overlay_horizontal_value }}%;
                 {% endif %}
                 width: {{ section.settings.mobile_overlay_width }}px;
               ">
            <img src="{{ section.settings.mobile_overlay_image | img_url: 'master' }}" alt="Overlay badge" loading="lazy">
          </div>
        {% endif %}
      </div>
    </div>
  </div>
  
</div>

<style>
  .scroll-animation-section {
    position: relative;
    width: 100%;
    height: calc({{ section.settings.pixels_per_image | default: 30 }}px * {{ section.settings.total_images | default: 118 }} + {{ section.settings.desktop_height | default: 900 }}px);
  }
  
  .scroll-animation-container {
    position: relative;
    width: 100%;
    height: 100%;
  }
  
  .scroll-animation-sticky {
    position: sticky;
    top: 0;
    width: 100%;
    height: {{ section.settings.desktop_height | default: 900 }}px;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  
  .scroll-animation-image-container {
    width: 100%;
    height: 100%;
    position: relative;
  }
  
  .scroll-animation-image {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
  
  .video-banner-content {
    position: absolute;
    padding: 30px;
    z-index: 2;
  }
  
  /* Desktop positioning */
  .desktop-top {
    top: 50px;
  }
  
  .desktop-middle {
    top: 50%;
    transform: translateY(-50%);
  }
  
  .desktop-bottom {
    bottom: 50px;
  }
  
  .desktop-left {
    left: 50px;
  }
  
  .desktop-center {
    left: 50%;
    transform: translateX(-50%);
  }
  
  .desktop-center.desktop-middle {
    transform: translate(-50%, -50%);
  }
  
  .desktop-right {
    right: 50px;
  }
  
  /* Animation */
  .animate--slide-in {
    opacity: 1;
    transition: opacity 0.5s ease, transform 0.5s ease;
  }
  
  /* Overlay Badge */
  .overlay-badge {
    position: absolute;
    z-index: 3;
  }
  
  .overlay-badge img {
    width: 100%;
    height: auto;
  }
  
  /* Mobile styles */
  .mobile-content {
    display: none;
  }
  
  .mobile-image {
    display: none;
  }
  
  .mobile-overlay-badge {
    display: none;
  }
  
  @media screen and (max-width: 768px) {
    .scroll-animation-section {
      height: calc({{ section.settings.pixels_per_image | default: 30 }}px * {{ section.settings.total_images | default: 118 }} + {{ section.settings.mobile_height | default: 700 }}px);
      margin-top: {{ section.settings.mobile_margin_top }}px;
      margin-bottom: {{ section.settings.mobile_margin_bottom }}px;
    }
    
    .scroll-animation-sticky {
      height: {{ section.settings.mobile_height | default: 700 }}px;
    }
    
    .desktop-image {
      display: none;
    }
    
    .mobile-image {
      display: block;
    }
    
    .desktop-content {
      display: none;
    }
    
    .desktop-overlay-badge {
      display: none;
    }
    
    .mobile-content {
      display: block;
      padding: 20px;
    }
    
    .mobile-overlay-badge {
      display: block;
    }
    
    /* Mobile positioning */
    .mobile-top {
      top: 30px;
    }
    
    .mobile-middle {
      top: 50%;
      transform: translateY(-50%);
    }
    
    .mobile-bottom {
      bottom: 30px;
    }
    
    .mobile-left {
      left: 20px;
    }
    
    .mobile-center {
      left: 50%;
      transform: translateX(-50%);
    }
    
    .mobile-center.mobile-middle {
      transform: translate(-50%, -50%);
    }
    
    .mobile-right {
      right: 20px;
    }
  }
</style>

<script>
  document.addEventListener('DOMContentLoaded', function() {
    const scrollAnimationSections = document.querySelectorAll('.scroll-animation-section');
    
    scrollAnimationSections.forEach(section => {
      const pixelsPerImage = parseInt(section.getAttribute('data-pixels-per-image'));
      const totalImages = parseInt(section.getAttribute('data-total-images'));
      const startImage = parseInt(section.getAttribute('data-start-image'));
      const desktopImageBase = section.getAttribute('data-desktop-image-base');
      const mobileImageBase = section.getAttribute('data-mobile-image-base');
      const imageExtension = section.getAttribute('data-image-extension');
      
      const desktopImages = [];
      const mobileImages = [];
      
      // Preload all images
      for (let i = 0; i < totalImages; i++) {
        const imageNumber = startImage + i;
        const formattedNumber = imageNumber.toString().padStart(3, '0');
        
        const desktopImageUrl = `${desktopImageBase}${formattedNumber}${imageExtension}`;
        const mobileImageUrl = `${mobileImageBase}${formattedNumber}${imageExtension}`;
        
        const desktopImg = new Image();
        desktopImg.src = desktopImageUrl;
        desktopImages.push(desktopImageUrl);
        
        const mobileImg = new Image();
        mobileImg.src = mobileImageUrl;
        mobileImages.push(mobileImageUrl);
      }
      
      const desktopImageElement = section.querySelector('.desktop-image');
      const mobileImageElement = section.querySelector('.mobile-image');
      const stickyElement = section.querySelector('.scroll-animation-sticky');
      
      let lastScrollTop = 0;
      let currentImageIndex = 0;
      
      function updateImageOnScroll() {
        const sectionRect = section.getBoundingClientRect();
        const stickyRect = stickyElement.getBoundingClientRect();
        
        // Calculate how far we've scrolled into the section
        const scrollPosition = window.scrollY;
        const sectionTop = scrollPosition + sectionRect.top;
        const sectionHeight = sectionRect.height;
        const viewportHeight = window.innerHeight;
        
        // Only activate when the sticky element is in view
        if (stickyRect.top <= 0 && stickyRect.bottom >= 0) {
          // Calculate which image to show based on scroll position
          const scrollIntoSection = scrollPosition - sectionTop;
          const scrollProgress = Math.min(Math.max(scrollIntoSection / (sectionHeight - viewportHeight), 0), 1);
          
          // Calculate image index (0 to totalImages-1)
          const newImageIndex = Math.min(
            Math.floor(scrollProgress * totalImages),
            totalImages - 1
          );
          
          // Only update the image if the index has changed
          if (newImageIndex !== currentImageIndex) {
            currentImageIndex = newImageIndex;
            desktopImageElement.src = desktopImages[currentImageIndex];
            mobileImageElement.src = mobileImages[currentImageIndex];
          }
        }
        
        lastScrollTop = scrollPosition;
      }
      
      window.addEventListener('scroll', updateImageOnScroll);
      window.addEventListener('resize', updateImageOnScroll);
      
      // Initial update
      updateImageOnScroll();
    });
  });
</script>
{% schema %}
{
  "name": "Scroll Animation",
  "settings": [
    {
      "type": "header",
      "content": "Animation Settings"
    },
    {
      "type": "range",
      "id": "pixels_per_image",
      "min": 10,
      "max": 100,
      "step": 5,
      "default": 30,
      "label": "Pixels per image",
      "info": "Controls the speed of the animation. Lower value = faster animation."
    },
    {
      "type": "color",
      "id": "background_color",
      "label": "Background Color",
      "default": "#000000"
    },
    {
      "type": "header",
      "content": "Image Sequence Settings"
    },
    {
      "type": "text",
      "id": "desktop_image_url_base",
      "label": "Desktop Image URL Base",
      "default": "https://cdn.shopify.com/s/files/1/0655/8256/3385/files/Exploded_desktop",
      "info": "Base URL for desktop images (without the number and extension)"
    },
    {
      "type": "text",
      "id": "mobile_image_url_base",
      "label": "Mobile Image URL Base",
      "default": "https://cdn.shopify.com/s/files/1/0655/8256/3385/files/Exploded_mobile",
      "info": "Base URL for mobile images (without the number and extension)"
    },
    {
      "type": "text",
      "id": "image_extension",
      "label": "Image Extension",
      "default": ".jpg",
      "info": "File extension including the dot (e.g., .jpg, .png)"
    },
    {
      "type": "range",
      "id": "start_image",
      "min": 1,
      "max": 100,
      "step": 1,
      "default": 1,
      "label": "Starting Image Number"
    },
    {
      "type": "range",
      "id": "total_images",
      "min": 2,
      "max": 200,
      "step": 2,
      "default": 118,
      "label": "Total Number of Images"
    },
    {
      "type": "header",
      "content": "Desktop Settings"
    },
    {
      "type": "range",
      "id": "desktop_height",
      "min": 500,
      "max": 1200,
      "step": 50,
      "default": 900,
      "label": "Height (px)"
    },
    {
      "type": "range",
      "id": "desktop_margin_top",
      "min": 0,
      "max": 100,
      "step": 5,
      "default": 0,
      "label": "Top Margin (px)"
    },
    {
      "type": "range",
      "id": "desktop_margin_bottom",
      "min": 0,
      "max": 100,
      "step": 5,
      "default": 0,
      "label": "Bottom Margin (px)"
    },
    {
      "type": "header",
      "content": "Desktop Text Content"
    },
    {
      "type": "text",
      "id": "desktop_heading",
      "label": "Heading",
      "default": "SOUND BY BOSE"
    },
    {
      "type": "color",
      "id": "desktop_heading_color",
      "label": "Heading Color",
      "default": "#ffffff"
    },
    {
      "type": "richtext",
      "id": "desktop_content",
      "label": "Content",
      "default": "<p>We teamed up with Bose to bring you a high-fidelity audio experience defined by clarity of sound and depth of range. Everything from ear gels to audio architecture is optimized for ideal performance. No compromises, just great sound.</p>"
    },
    {
      "type": "color",
      "id": "desktop_text_color",
      "label": "Text Color",
      "default": "#ffffff"
    },
    {
      "type": "range",
      "id": "desktop_text_width",
      "min": 10,
      "max": 100,
      "step": 5,
      "default": 50,
      "label": "Text Box Width (%)"
    },
    {
      "type": "select",
      "id": "desktop_text_position_vertical",
      "label": "Vertical Position",
      "options": [
        {
          "value": "top",
          "label": "Top"
        },
        {
          "value": "middle",
          "label": "Middle"
        },
        {
          "value": "bottom",
          "label": "Bottom"
        }
      ],
      "default": "top"
    },
    {
      "type": "select",
      "id": "desktop_text_position_horizontal",
      "label": "Horizontal Position",
      "options": [
        {
          "value": "left",
          "label": "Left"
        },
        {
          "value": "center",
          "label": "Center"
        },
        {
          "value": "right",
          "label": "Right"
        }
      ],
      "default": "left"
    },
    {
      "type": "select",
      "id": "desktop_text_align",
      "label": "Text Alignment",
      "options": [
        {
          "value": "left",
          "label": "Left"
        },
        {
          "value": "center",
          "label": "Center"
        },
        {
          "value": "right",
          "label": "Right"
        }
      ],
      "default": "left"
    },
    {
      "type": "range",
      "id": "desktop_heading_size",
      "min": 16,
      "max": 60,
      "step": 1,
      "default": 24,
      "label": "Heading Size (px)"
    },
    {
      "type": "range",
      "id": "desktop_content_size",
      "min": 12,
      "max": 30,
      "step": 1,
      "default": 16,
      "label": "Content Size (px)"
    },
    {
      "type": "header",
      "content": "Desktop Overlay Badge"
    },
    {
      "type": "image_picker",
      "id": "desktop_overlay_image",
      "label": "Overlay Image"
    },
    {
      "type": "range",
      "id": "desktop_overlay_width",
      "min": 50,
      "max": 500,
      "step": 10,
      "default": 150,
      "label": "Width (px)"
    },
    {
      "type": "select",
      "id": "desktop_overlay_horizontal_position",
      "label": "Horizontal Position From",
      "options": [
        {
          "value": "left",
          "label": "Left"
        },
        {
          "value": "right",
          "label": "Right"
        }
      ],
      "default": "right"
    },
    {
      "type": "range",
      "id": "desktop_overlay_horizontal_value",
      "min": 0,
      "max": 100,
      "step": 1,
      "default": 10,
      "label": "Horizontal Position (%)"
    },
    {
      "type": "range",
      "id": "desktop_overlay_top",
      "min": 0,
      "max": 100,
      "step": 1,
      "default": 10,
      "label": "Position from Top (%)"
    },
    {
      "type": "header",
      "content": "Mobile Settings"
    },
    {
      "type": "range",
      "id": "mobile_height",
      "min": 300,
      "max": 900,
      "step": 50,
      "default": 700,
      "label": "Height (px)"
    },
    {
      "type": "range",
      "id": "mobile_margin_top",
      "min": 0,
      "max": 100,
      "step": 5,
      "default": 0,
      "label": "Top Margin (px)"
    },
    {
      "type": "range",
      "id": "mobile_margin_bottom",
      "min": 0,
      "max": 100,
      "step": 5,
      "default": 0,
      "label": "Bottom Margin (px)"
    },
    {
      "type": "header",
      "content": "Mobile Text Content"
    },
    {
      "type": "text",
      "id": "mobile_heading",
      "label": "Heading",
      "default": "SOUND BY BOSE"
    },
    {
      "type": "color",
      "id": "mobile_heading_color",
      "label": "Heading Color",
      "default": "#ffffff"
    },
    {
      "type": "richtext",
      "id": "mobile_content",
      "label": "Content",
      "default": "<p>We teamed up with Bose to bring you a high-fidelity audio experience defined by clarity of sound and depth of range. Everything from ear gels to audio architecture is optimized for ideal performance. No compromises, just great sound.</p>"
    },
    {
      "type": "color",
      "id": "mobile_text_color",
      "label": "Text Color",
      "default": "#ffffff"
    },
    {
      "type": "range",
      "id": "mobile_text_width",
      "min": 10,
      "max": 100,
      "step": 5,
      "default": 80,
      "label": "Text Box Width (%)"
    },
    {
      "type": "select",
      "id": "mobile_text_position_vertical",
      "label": "Vertical Position",
      "options": [
        {
          "value": "top",
          "label": "Top"
        },
        {
          "value": "middle",
          "label": "Middle"
        },
        {
          "value": "bottom",
          "label": "Bottom"
        }
      ],
      "default": "top"
    },
    {
      "type": "select",
      "id": "mobile_text_position_horizontal",
      "label": "Horizontal Position",
      "options": [
        {
          "value": "left",
          "label": "Left"
        },
        {
          "value": "center",
          "label": "Center"
        },
        {
          "value": "right",
          "label": "Right"
        }
      ],
      "default": "left"
    },
    {
      "type": "select",
      "id": "mobile_text_align",
      "label": "Text Alignment",
      "options": [
        {
          "value": "left",
          "label": "Left"
        },
        {
          "value": "center",
          "label": "Center"
        },
        {
          "value": "right",
          "label": "Right"
        }
      ],
      "default": "left"
    },
    {
      "type": "range",
      "id": "mobile_heading_size",
      "min": 16,
      "max": 60,
      "step": 1,
      "default": 20,
      "label": "Heading Size (px)"
    },
    {
      "type": "range",
      "id": "mobile_content_size",
      "min": 12,
      "max": 30,
      "step": 1,
      "default": 14,
      "label": "Content Size (px)"
    },
    {
      "type": "header",
      "content": "Mobile Overlay Badge"
    },
    {
      "type": "image_picker",
      "id": "mobile_overlay_image",
      "label": "Overlay Image"
    },
    {
      "type": "range",
      "id": "mobile_overlay_width",
      "min": 50,
      "max": 300,
      "step": 10,
      "default": 100,
      "label": "Width (px)"
    },
    {
      "type": "select",
      "id": "mobile_overlay_horizontal_position",
      "label": "Horizontal Position From",
      "options": [
        {
          "value": "left",
          "label": "Left"
        },
        {
          "value": "right",
          "label": "Right"
        }
      ],
      "default": "right"
    },
    {
      "type": "range",
      "id": "mobile_overlay_horizontal_value",
      "min": 0,
      "max": 100,
      "step": 1,
      "default": 10,
      "label": "Horizontal Position (%)"
    },
    {
      "type": "range",
      "id": "mobile_overlay_top",
      "min": 0,
      "max": 100,
      "step": 1,
      "default": 10,
      "label": "Position from Top (%)"
    }
  ],
  "presets": [
    {
      "name": "Scroll Animation"
    }
  ]
}
{% endschema %}