document.addEventListener('DOMContentLoaded', function() {
    // Get carousel elements
    const carousel = document.querySelector('.el-carousel');
    const items = carousel.querySelectorAll('.el-carousel__item');
    const indicators = carousel.querySelectorAll('.el-carousel__indicator');
    const leftArrow = carousel.querySelector('.el-carousel__arrow--left');
    const rightArrow = carousel.querySelector('.el-carousel__arrow--right');
    
    let currentIndex = 0;
    const totalItems = items.length;
    const slideInterval = 3000; // Change slide every 3 seconds
    let slideTimer;

    // Function to show slide
    function showSlide(index) {
        // Reset all slides
        items.forEach(item => {
            item.style.transform = 'translateX(790px) scale(1)';
            item.classList.remove('is-active', 'is-animating');
        });
        
        // Reset all indicators
        indicators.forEach(indicator => {
            indicator.classList.remove('is-active');
        });

        // Calculate positions for previous, current, and next slides
        const prevIndex = (index - 1 + totalItems) % totalItems;
        const nextIndex = (index + 1) % totalItems;

        // Position slides
        items[prevIndex].style.transform = 'translateX(-790px) scale(1)';
        items[index].style.transform = 'translateX(0) scale(1)';
        items[nextIndex].style.transform = 'translateX(790px) scale(1)';

        // Add active classes
        items[index].classList.add('is-active', 'is-animating');
        indicators[index].classList.add('is-active');
        
        currentIndex = index;
    }

    // Function to go to next slide
    function nextSlide() {
        showSlide((currentIndex + 1) % totalItems);
    }

    // Function to go to previous slide
    function prevSlide() {
        showSlide((currentIndex - 1 + totalItems) % totalItems);
    }

    // Start automatic sliding
    function startSlideTimer() {
        stopSlideTimer(); // Clear any existing timer
        slideTimer = setInterval(nextSlide, slideInterval);
    }

    // Stop automatic sliding
    function stopSlideTimer() {
        if (slideTimer) {
            clearInterval(slideTimer);
        }
    }

    // Event listeners for arrows
    leftArrow.addEventListener('click', () => {
        prevSlide();
        startSlideTimer(); // Reset timer after manual navigation
    });

    rightArrow.addEventListener('click', () => {
        nextSlide();
        startSlideTimer(); // Reset timer after manual navigation
    });

    // Event listeners for indicators
    indicators.forEach((indicator, index) => {
        indicator.addEventListener('click', () => {
            showSlide(index);
            startSlideTimer(); // Reset timer after manual navigation
        });
    });

    // Pause automatic sliding when hovering over carousel
    carousel.addEventListener('mouseenter', stopSlideTimer);
    carousel.addEventListener('mouseleave', startSlideTimer);

    // Show first slide and start automatic sliding
    showSlide(0);
    startSlideTimer();
});

