/**
 * Testimonial Carousel JavaScript
 * Handles testimonial carousel functionality
 */

(function($) {
    'use strict';
    
    $(document).ready(function() {
        const testimonialCarousel = {
            init: function() {
                this.bindEvents();
                this.startCarousel();
            },
            
            bindEvents: function() {
                $('.carousel-prev').on('click', this.prevSlide.bind(this));
                $('.carousel-next').on('click', this.nextSlide.bind(this));
                $('.carousel-dots .dot').on('click', this.goToSlide.bind(this));
                
                // Touch/swipe support
                this.initTouchEvents();
            },
            
            startCarousel: function() {
                const $slides = $('.carousel-slide');
                if ($slides.length === 0) return;
                
                this.currentIndex = 0;
                this.totalSlides = $slides.length;
                this.isAnimating = false;
                
                this.updateCarousel();
                
                // Auto-advance
                this.autoAdvance();
            },
            
            nextSlide: function() {
                if (this.isAnimating) return;
                
                this.currentIndex = (this.currentIndex + 1) % this.totalSlides;
                this.updateCarousel();
            },
            
            prevSlide: function() {
                if (this.isAnimating) return;
                
                this.currentIndex = this.currentIndex === 0 ? this.totalSlides - 1 : this.currentIndex - 1;
                this.updateCarousel();
            },
            
            goToSlide: function(e) {
                if (this.isAnimating) return;
                
                const index = $(e.target).data('index');
                this.currentIndex = index;
                this.updateCarousel();
            },
            
            updateCarousel: function() {
                this.isAnimating = true;
                
                const $slides = $('.carousel-slide');
                const $dots = $('.carousel-dots .dot');
                
                // Hide all slides
                $slides.removeClass('active');
                $dots.removeClass('active');
                
                // Show current slide
                $slides.eq(this.currentIndex).addClass('active');
                $dots.eq(this.currentIndex).addClass('active');
                
                // Reset animation flag
                setTimeout(() => {
                    this.isAnimating = false;
                }, 300);
            },
            
            autoAdvance: function() {
                setInterval(() => {
                    if (!this.isPaused) {
                        this.nextSlide();
                    }
                }, 6000);
            },
            
            initTouchEvents: function() {
                const $carousel = $('.hero-carousel');
                let startX = 0;
                let startY = 0;
                let endX = 0;
                let endY = 0;
                
                $carousel.on('touchstart', (e) => {
                    startX = e.originalEvent.touches[0].clientX;
                    startY = e.originalEvent.touches[0].clientY;
                });
                
                $carousel.on('touchend', (e) => {
                    endX = e.originalEvent.changedTouches[0].clientX;
                    endY = e.originalEvent.changedTouches[0].clientY;
                    
                    const diffX = startX - endX;
                    const diffY = startY - endY;
                    
                    // Check if horizontal swipe
                    if (Math.abs(diffX) > Math.abs(diffY) && Math.abs(diffX) > 50) {
                        if (diffX > 0) {
                            this.nextSlide();
                        } else {
                            this.prevSlide();
                        }
                    }
                });
                
                // Pause on hover
                $carousel.on('mouseenter', () => {
                    this.isPaused = true;
                });
                
                $carousel.on('mouseleave', () => {
                    this.isPaused = false;
                });
            }
        };
        
        // Initialize testimonial carousel
        testimonialCarousel.init();
    });
    
})(jQuery);
