/**
 * J-Bridge JavaScript
 * Handles J-Bridge specific functionality
 */

(function($) {
    'use strict';
    
    $(document).ready(function() {
        const jBridge = {
            init: function() {
                this.bindEvents();
                this.initAnimations();
            },
            
            bindEvents: function() {
                // FAQ functionality
                $('.faq-question').on('click', this.handleFaqClick.bind(this));
                
                // Video controls
                $('#play-button').on('click', this.handleVideoPlay.bind(this));
                $('#interview-video').on('ended', this.handleVideoEnded.bind(this));
                
                // Smooth scrolling for anchor links
                $('a[href^="#"]').on('click', this.handleSmoothScroll.bind(this));
            },
            
            initAnimations: function() {
                // Initialize AOS (Animate On Scroll) if available
                if (typeof AOS !== 'undefined') {
                    AOS.init({
                        duration: 800,
                        easing: 'ease-in-out',
                        once: true
                    });
                }
                
                // Custom animations
                this.initCustomAnimations();
            },
            
            initCustomAnimations: function() {
                // Animate elements on scroll
                $(window).on('scroll', this.throttle(this.handleScroll.bind(this), 100));
            },
            
            handleFaqClick: function(e) {
                const $question = $(e.currentTarget);
                const faqNumber = $question.data('faq');
                const $answerBubble = $('#answerBubble');
                const $questionBubble = $('#questionBubble');
                
                // FAQ data
                const faqData = {
                    1: {
                        question: nihongo_ajax.faq_free_trial || 'Is there a free trial?',
                        answer: nihongo_ajax.faq_free_trial_answer || 'Yes, we offer a free trial for all our courses.'
                    },
                    2: {
                        question: nihongo_ajax.faq_fee_structure || 'What is the fee structure?',
                        answer: nihongo_ajax.faq_fee_structure_answer || 'Our fees are competitive and transparent.'
                    },
                    3: {
                        question: nihongo_ajax.faq_online_only || 'Is it online only?',
                        answer: nihongo_ajax.faq_online_only_answer || 'Yes, all our courses are conducted online.'
                    },
                    4: {
                        question: nihongo_ajax.faq_beginners_okay || 'Are beginners okay?',
                        answer: nihongo_ajax.faq_beginners_okay_answer || 'Absolutely! We welcome learners of all levels.'
                    },
                    5: {
                        question: nihongo_ajax.faq_j_bridge_fees || 'What are J-Bridge fees?',
                        answer: nihongo_ajax.faq_j_bridge_fees_answer || 'J-Bridge has transparent and reasonable fees.'
                    },
                    6: {
                        question: nihongo_ajax.faq_visa_support || 'Do you provide visa support?',
                        answer: nihongo_ajax.faq_visa_support_answer || 'We provide guidance on visa-related matters.'
                    }
                };
                
                const faq = faqData[faqNumber];
                if (faq) {
                    $questionBubble.find('#questionTitle').text(faq.question);
                    $answerBubble.find('#answerText').text(faq.answer);
                    
                    // Show bubbles
                    $questionBubble.addClass('active');
                    $answerBubble.addClass('active');
                }
            },
            
            handleVideoPlay: function(e) {
                const $video = $('#interview-video');
                const $playButton = $('#play-button');
                const $overlay = $('.video-overlay');
                
                $video[0].play();
                $playButton.hide();
                $overlay.fadeOut();
            },
            
            handleVideoEnded: function() {
                const $playButton = $('#play-button');
                const $overlay = $('.video-overlay');
                
                $playButton.show();
                $overlay.fadeIn();
            },
            
            handleSmoothScroll: function(e) {
                e.preventDefault();
                
                const target = $(e.currentTarget.getAttribute('href'));
                if (target.length) {
                    $('html, body').animate({
                        scrollTop: target.offset().top - 80
                    }, 800);
                }
            },
            
            handleScroll: function() {
                const scrollTop = $(window).scrollTop();
                const windowHeight = $(window).height();
                
                // Parallax effects
                $('.parallax-bg').each(function() {
                    const $this = $(this);
                    const speed = $this.data('speed') || 0.5;
                    const yPos = -(scrollTop * speed);
                    $this.css('transform', `translateY(${yPos}px)`);
                });
                
                // Fade in elements
                $('.fade-in-on-scroll').each(function() {
                    const $this = $(this);
                    const elementTop = $this.offset().top;
                    const elementBottom = elementTop + $this.outerHeight();
                    
                    if (elementBottom >= scrollTop && elementTop <= scrollTop + windowHeight) {
                        $this.addClass('fade-in');
                    }
                });
            },
            
            throttle: function(func, limit) {
                let inThrottle;
                return function() {
                    const args = arguments;
                    const context = this;
                    if (!inThrottle) {
                        func.apply(context, args);
                        inThrottle = true;
                        setTimeout(() => inThrottle = false, limit);
                    }
                };
            }
        };
        
        // Initialize J-Bridge
        jBridge.init();
    });
    
})(jQuery);
