const slider = document.getElementById('workflow-slider');
let isAutoplay = true;
let autoplayInterval;

// Move Slider Logic
function moveSlider(direction) {
    const scrollAmount = slider.offsetWidth > 768 ? 350 : 300; // Adjust based on card width
    if (direction === 'next') {
        // If at the end, loop back to start
        if (slider.scrollLeft + slider.offsetWidth >= slider.scrollWidth - 10) {
            slider.scrollTo({ left: 0, behavior: 'smooth' });
        } else {
            slider.scrollBy({ left: scrollAmount, behavior: 'smooth' });
        }
    } else {
        slider.scrollBy({ left: -scrollAmount, behavior: 'smooth' });
    }
}

// Autoplay Functions
function startAutoplay() {
    autoplayInterval = setInterval(() => {
        moveSlider('next');
    }, 3000); // Change every 3 seconds
}

function stopAutoplay() {
    clearInterval(autoplayInterval);
}

// Update Active Class on Scroll
slider.addEventListener('scroll', () => {
    const cards = document.querySelectorAll('.workflow-step-card');
    const sliderCenter = slider.scrollLeft + (slider.offsetWidth / 2);

    cards.forEach(card => {
        const cardCenter = card.offsetLeft + (card.offsetWidth / 2);
        if (Math.abs(sliderCenter - cardCenter) < 150) {
            card.classList.add('active');
        } else {
            card.classList.remove('active');
        }
    });
});

// Initialize
startAutoplay();


// benefit
AOS.init({
    duration: 1000, // Duration of animations in ms
    easing: 'ease-out-cubic', // Behance-style smooth easing
    offset: 120, // Offset from the element position to trigger animation
    once: true, // Only run once
});

// testimonial
let currentIndex = 0;
const cards = document.querySelectorAll('.testimonial-card');

function updateCards() {
    cards.forEach((card, i) => {
        card.classList.remove('active', 'next', 'hidden-top');

        if (i === currentIndex) {
            card.classList.add('active');
        } else if (i === (currentIndex + 1) % cards.length) {
            card.classList.add('next');
        } else {
            card.classList.add('hidden-top');
        }
    });
}

function nextTestimonial() {
    currentIndex = (currentIndex + 1) % cards.length;
    updateCards();
}

function prevTestimonial() {
    currentIndex = (currentIndex - 1 + cards.length) % cards.length;
    updateCards();
}

// Auto rotate every 5 seconds
setInterval(nextTestimonial, 5000);

// last images section
const gallery = document.getElementById('visualGallery');
const dots = document.querySelectorAll('.dot-last-sec');
let index = 0;

// Autoplay Logic
setInterval(() => {
    if (window.innerWidth < 768) {
        index = (index + 1) % dots.length;
        const scrollAmount = index * (gallery.offsetWidth * 0.85);

        gallery.scrollTo({
            left: scrollAmount,
            behavior: 'smooth'
        });
    }
}, 3000);

// Scroll Listener for Dots
gallery.addEventListener('scroll', () => {
    if (window.innerWidth < 768) {
        index = Math.round(gallery.scrollLeft / (gallery.offsetWidth * 0.85));
        dots.forEach((dot, i) => {
            dot.classList.toggle('active', i === index);
        });
    }
});
