/* Ateni Media Technologies Custom JavaScript */

document.addEventListener('DOMContentLoaded', function() {
    
    // Initialize Ateni-specific animations
    initAteniAnimations();
    
    // Initialize counter animations
    initAteniCounters();
    
    // Initialize enhanced testimonial slider
    initAteniTestimonialSlider();
    
    // Initialize team member interactions
    initTeamInteractions();
    
    // Smooth scroll for anchor links
    initSmoothScroll();
    
    // Form submission handler
    initContactForm();
});

function initAteniAnimations() {
    // Add animation class to elements when they come into view
    const observerOptions = {
        threshold: 0.1,
        rootMargin: '0px 0px -50px 0px'
    };
    
    const observer = new IntersectionObserver((entries) => {
        entries.forEach(entry => {
            if (entry.isIntersecting) {
                entry.target.classList.add('animated');
            }
        });
    }, observerOptions);
    
    // Observe elements with data-animate attribute
    document.querySelectorAll('[data-animate]').forEach(el => {
        observer.observe(el);
    });
}

function initAteniCounters() {
    // Initialize counter animation
    $('.counter-number').counterUp({
        delay: 10,
        time: 2000
    });
    
    // Add custom formatting for counters
    $('.counter-number').each(function() {
        const value = $(this).text();
        if (value.includes('+')) {
            $(this).html('<span class="counter-value">' + value.replace('+', '') + '</span><span class="counter-symbol">+</span>');
        } else if (value.includes('%')) {
            $(this).html('<span class="counter-value">' + value.replace('%', '') + '</span><span class="counter-symbol">%</span>');
        }
    });
}

function initAteniTestimonialSlider() {
    // Initialize testimonial slider with custom settings
    const testimonialSlider = new Swiper('#testiSlider1', {
        slidesPerView: 1,
        spaceBetween: 30,
        centeredSlides: true,
        loop: true,
        autoplay: {
            delay: 5000,
            disableOnInteraction: false,
        },
        pagination: {
            el: '.swiper-pagination',
            clickable: true,
        },
        breakpoints: {
            768: {
                slidesPerView: 2,
                spaceBetween: 30,
            },
            1200: {
                slidesPerView: 2.5,
                spaceBetween: 40,
            }
        },
        on: {
            init: function() {
                // Add active class to current slide
                this.slides[this.activeIndex].classList.add('active-slide');
            },
            slideChange: function() {
                // Update active class on slide change
                this.slides.forEach(slide => slide.classList.remove('active-slide'));
                this.slides[this.activeIndex].classList.add('active-slide');
            }
        }
    });
    
    // Pause autoplay on hover
    const sliderContainer = document.querySelector('#testiSlider1');
    if (sliderContainer) {
        sliderContainer.addEventListener('mouseenter', function() {
            testimonialSlider.autoplay.stop();
        });
        
        sliderContainer.addEventListener('mouseleave', function() {
            testimonialSlider.autoplay.start();
        });
    }
}

function initTeamInteractions() {
    // Team member card interactions
    const teamCards = document.querySelectorAll('.ateni-team');
    
    teamCards.forEach(card => {
        card.addEventListener('mouseenter', function() {
            this.style.transform = 'translateY(-10px)';
            this.style.boxShadow = '0 20px 40px rgba(0, 102, 204, 0.15)';
        });
        
        card.addEventListener('mouseleave', function() {
            this.style.transform = 'translateY(0)';
            this.style.boxShadow = '0 5px 15px rgba(0, 0, 0, 0.05)';
        });
    });
    
    // Team modal functionality
    const teamMembers = document.querySelectorAll('.team-content .box-title');
    teamMembers.forEach(member => {
        member.style.cursor = 'pointer';
        member.addEventListener('click', function() {
            const memberName = this.textContent;
            const memberRole = this.parentElement.querySelector('.team-desig').textContent;
            const memberDesc = this.parentElement.querySelector('.team-desc').textContent;
            
            // Show team member details (you can implement a modal here)
            console.log(`Selected: ${memberName} - ${memberRole}`);
            // In a real implementation, you would show a modal with more details
        });
    });
}

function initSmoothScroll() {
    // Smooth scroll for internal links
    document.querySelectorAll('a[href^="#"]').forEach(anchor => {
        anchor.addEventListener('click', function (e) {
            const href = this.getAttribute('href');
            
            // Only process internal links
            if (href !== '#' && href.startsWith('#')) {
                e.preventDefault();
                
                const targetId = href.substring(1);
                const targetElement = document.getElementById(targetId);
                
                if (targetElement) {
                    window.scrollTo({
                        top: targetElement.offsetTop - 100,
                        behavior: 'smooth'
                    });
                }
            }
        });
    });
}

function initContactForm() {
    // Contact form submission handler
    const contactForm = document.getElementById('contactForm');
    
    if (contactForm) {
        contactForm.addEventListener('submit', function(e) {
            e.preventDefault();
            
            // Get form data
            const formData = new FormData(this);
            const formDataObj = Object.fromEntries(formData.entries());
            
            // Simple validation
            let isValid = true;
            const requiredFields = ['name', 'email', 'message'];
            
            requiredFields.forEach(field => {
                if (!formDataObj[field] || formDataObj[field].trim() === '') {
                    isValid = false;
                    const input = this.querySelector(`[name="${field}"]`);
                    input.classList.add('error');
                }
            });
            
            if (!isValid) {
                alert('Please fill in all required fields.');
                return;
            }
            
            // Email validation
            const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
            if (!emailRegex.test(formDataObj.email)) {
                alert('Please enter a valid email address.');
                this.querySelector('[name="email"]').classList.add('error');
                return;
            }
            
            // Show loading state
            const submitBtn = this.querySelector('button[type="submit"]');
            const originalText = submitBtn.textContent;
            submitBtn.textContent = 'Sending...';
            submitBtn.disabled = true;
            
            // Simulate form submission (replace with actual AJAX call)
            setTimeout(() => {
                // Success message
                alert('Thank you for contacting Ateni Media Technologies! We will get back to you soon.');
                
                // Reset form
                this.reset();
                
                // Reset button
                submitBtn.textContent = originalText;
                submitBtn.disabled = false;
                
                // Remove error classes
                this.querySelectorAll('.error').forEach(el => {
                    el.classList.remove('error');
                });
            }, 1500);
        });
        
        // Remove error class on input
        contactForm.querySelectorAll('input, textarea').forEach(input => {
            input.addEventListener('input', function() {
                this.classList.remove('error');
            });
        });
    }
}

// Service card animation
function animateServiceCards() {
    const serviceCards = document.querySelectorAll('.service-card-highlight');
    
    serviceCards.forEach((card, index) => {
        // Add delay based on index
        card.style.animationDelay = `${index * 0.1}s`;
        
        // Add scroll animation
        const observer = new IntersectionObserver((entries) => {
            entries.forEach(entry => {
                if (entry.isIntersecting) {
                    entry.target.classList.add('animate-in');
                }
            });
        }, { threshold: 0.1 });
        
        observer.observe(card);
    });
}

// Call service card animation when DOM is loaded
document.addEventListener('DOMContentLoaded', animateServiceCards);

// Parallax effect for video background
function initParallax() {
    window.addEventListener('scroll', function() {
        const scrolled = window.pageYOffset;
        const videoBackground = document.querySelector('.video2-wrapp');
        
        if (videoBackground) {
            const rate = scrolled * 0.5;
            videoBackground.style.transform = `translate3d(0, ${rate}px, 0)`;
        }
    });
}

// Initialize parallax on larger screens only
if (window.innerWidth > 768) {
    initParallax();
}