/**
 * Navigation JavaScript
 * Handles navigation menu functionality
 */

(function($) {
    'use strict';
    
    $(document).ready(function() {
        const navigation = {
            init: function() {
                this.bindEvents();
                this.initMobileMenu();
                this.initSmoothScrolling();
                this.initActiveStates();
            },
            
            bindEvents: function() {
                // Mobile menu toggle
                $('.mobile-menu-toggle').on('click', this.toggleMobileMenu.bind(this));
                
                // Close mobile menu when clicking outside
                $(document).on('click', (e) => {
                    if (!$(e.target).closest('.header, .mobile-nav').length) {
                        this.closeMobileMenu();
                    }
                });
                
                // Close mobile menu when clicking on a link
                $('.mobile-nav a').on('click', this.closeMobileMenu.bind(this));
                
                // Language switcher
                $('.language-switcher a').on('click', this.handleLanguageSwitch.bind(this));
            },
            
            initMobileMenu: function() {
                const $mobileToggle = $('.mobile-menu-toggle');
                const $mobileNav = $('.mobile-nav');
                
                // Add ARIA attributes
                $mobileToggle.attr('aria-expanded', 'false');
                $mobileNav.attr('aria-hidden', 'true');
            },
            
            toggleMobileMenu: function(e) {
                e.preventDefault();
                
                const $mobileToggle = $('.mobile-menu-toggle');
                const $mobileNav = $('.mobile-nav');
                const isOpen = $mobileNav.hasClass('active');
                
                if (isOpen) {
                    this.closeMobileMenu();
                } else {
                    this.openMobileMenu();
                }
            },
            
            openMobileMenu: function() {
                const $mobileToggle = $('.mobile-menu-toggle');
                const $mobileNav = $('.mobile-nav');
                const $body = $('body');
                
                $mobileNav.addClass('active');
                $mobileToggle.addClass('active');
                $mobileToggle.attr('aria-expanded', 'true');
                $mobileNav.attr('aria-hidden', 'false');
                $body.addClass('mobile-menu-open');
                
                // Prevent body scroll
                $body.css('overflow', 'hidden');
            },
            
            closeMobileMenu: function() {
                const $mobileToggle = $('.mobile-menu-toggle');
                const $mobileNav = $('.mobile-nav');
                const $body = $('body');
                
                $mobileNav.removeClass('active');
                $mobileToggle.removeClass('active');
                $mobileToggle.attr('aria-expanded', 'false');
                $mobileNav.attr('aria-hidden', 'true');
                $body.removeClass('mobile-menu-open');
                
                // Restore body scroll
                $body.css('overflow', '');
            },
            
            initSmoothScrolling: function() {
                // Smooth scrolling for anchor links
                $('a[href^="#"]').on('click', function(e) {
                    const target = $(this.getAttribute('href'));
                    if (target.length) {
                        e.preventDefault();
                        
                        const headerHeight = $('.header').outerHeight() || 0;
                        const targetOffset = target.offset().top - headerHeight - 20;
                        
                        $('html, body').animate({
                            scrollTop: targetOffset
                        }, 800, 'swing');
                    }
                });
            },
            
            initActiveStates: function() {
                // Update active states on scroll
                $(window).on('scroll', this.throttle(this.updateActiveStates.bind(this), 100));
                
                // Update active states on page load
                this.updateActiveStates();
                
                // Header scroll effect
                this.initHeaderScrollEffect();
            },
            
            initHeaderScrollEffect: function() {
                const $header = $('.header');
                const scrollThreshold = 50;
                
                $(window).on('scroll', this.throttle(function() {
                    const scrollTop = $(window).scrollTop();
                    
                    if (scrollTop > scrollThreshold) {
                        $header.addClass('scrolled');
                    } else {
                        $header.removeClass('scrolled');
                    }
                }, 10));
            },
            
            updateActiveStates: function() {
                const scrollTop = $(window).scrollTop();
                const windowHeight = $(window).height();
                const documentHeight = $(document).height();
                
                // Update navigation active states based on scroll position
                $('.nav-link').each(function() {
                    const $link = $(this);
                    const href = $link.attr('href');
                    
                    if (href && href.startsWith('#')) {
                        const target = $(href);
                        if (target.length) {
                            const targetTop = target.offset().top;
                            const targetBottom = targetTop + target.outerHeight();
                            
                            if (scrollTop >= targetTop - 100 && scrollTop < targetBottom - 100) {
                                $link.addClass('active');
                            } else {
                                $link.removeClass('active');
                            }
                        }
                    }
                });
            },
            
            handleLanguageSwitch: function(e) {
                // Language switching is handled by PHP, but we can add visual feedback
                const $link = $(e.currentTarget);
                const $switcher = $link.closest('.language-switcher');
                
                // Add loading state
                $switcher.addClass('switching');
                
                // Remove loading state after a short delay
                setTimeout(() => {
                    $switcher.removeClass('switching');
                }, 500);
            },
            
            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 navigation
        navigation.init();
    });
    
})(jQuery);
