// Aos

AOS.init({
    once: true,
})

// Fixed header

const header = document.querySelector('.header');

function checkWindowResizeForMenu() {
    if (!window.matchMedia('(max-width: 992px)').matches) {
        if (window.scrollY > 300 || document.documentElement.scrollTop > 500) {
            header.classList.add('fixed');
        } else {
            header.classList.remove('fixed');
        }
    } else {
        if (header.classList.contains('fixed')) {
            header.classList.remove('fixed');
        }
    }
}

checkWindowResizeForMenu();

window.addEventListener('resize', checkWindowResizeForMenu);
window.addEventListener('scroll', checkWindowResizeForMenu);

// Mobile Menu

const openMenuBtn = document.querySelector('[data-burger-button]');
const mobileMenu = document.querySelector('[data-nav-menu]');

if (openMenuBtn && mobileMenu) {
    openMenuBtn.addEventListener('click', () => {
        mobileMenu.classList.toggle('active');
        openMenuBtn.classList.toggle('active');
    })
}

// Count numbers

function animateNumber(number) {
    const target = +number.getAttribute('data-target-count');
    let count = 0;
    const increment = target / 80;

    const updateNumber = () => {
        count += increment;
        if (count < target) {
            number.textContent = Math.ceil(count);
            requestAnimationFrame(updateNumber);
        } else {
            number.textContent = target;
        }
    };

    updateNumber();
}

function handleIntersection(entries, observer) {
    entries.forEach(entry => {
        if (entry.isIntersecting) {
            animateNumber(entry.target);
            observer.unobserve(entry.target);
        }
    });
}

document.addEventListener("DOMContentLoaded", () => {
    const observer = new IntersectionObserver(handleIntersection, {
        threshold: 0.9
    });

    const numbers = document.querySelectorAll('[data-target-count]');
    numbers.forEach(number => {
        observer.observe(number);
    });

    var lightbox = GLightbox({
        selector: '.glightbox'
    });
    var lightboxInlineIframe = GLightbox({
        selector: '.glightbox4'
    });

    particlesJS('particles-js', {
        "particles": {
            "number": {
                "value": 50,
                "density": {
                    "enable": true,
                    "value_area": 1000
                }
            },
            "color": {
                "value": "#ffffff"
            },
            "shape": {
                "type": "circle",
                "stroke": {
                    "width": 0,
                    "color": "#ffffff"
                },
                "polygon": {
                    "nb_sides": 4
                },
                "image": {
                    "src": "img/github.svg",
                    "width": 100,
                    "height": 100
                }
            },
            "opacity": {
                "value": 0.5,
                "random": false,
                "anim": {
                    "enable": false,
                    "speed": 1,
                    "opacity_min": 0.1,
                    "sync": false
                }
            },
            "size": {
                "value": 15,
                "random": true,
                "anim": {
                    "enable": false,
                    "speed": 103.0969030969031,
                    "size_min": 0.1,
                    "sync": false
                }
            },
            "line_linked": {
                "enable": true,
                "distance": 150,
                "color": "#ffffff",
                "opacity": 0.4,
                "width": 3.1565905665290908
            },
            "move": {
                "enable": true,
                "speed": 6,
                "direction": "none",
                "random": false,
                "straight": false,
                "out_mode": "out",
                "bounce": false,
                "attract": {
                    "enable": false,
                    "rotateX": 600,
                    "rotateY": 1200
                }
            }
        },
        "interactivity": {
            "detect_on": "window",
            "events": {
                "onhover": {
                    "enable": true,
                    "mode": "repulse"
                },
                "onclick": {
                    "enable": true,
                    "mode": "push"
                },
                "resize": true
            },
            "modes": {
                "grab": {
                    "distance": 400,
                    "line_linked": {
                        "opacity": 1
                    }
                },
                "bubble": {
                    "distance": 400,
                    "size": 40,
                    "duration": 2,
                    "opacity": 8,
                    "speed": 3
                },
                "repulse": {
                    "distance": 200,
                    "duration": 0.4
                },
                "push": {
                    "particles_nb": 4
                },
                "remove": {
                    "particles_nb": 2
                }
            }
        },
        "retina_detect": true
    })

    // const langBlock = document.getElementById('lang');
    // const span = langBlock.querySelector('span');

    // langBlock.addEventListener('click', function() {
    //     const currentLang = this.getAttribute('data-lang');

    //     if (currentLang === 'en') {
    //         span.textContent = 'RU';
    //         this.setAttribute('data-lang', 'ru');
    //     } else {
    //         span.textContent = 'EN';
    //         this.setAttribute('data-lang', 'en');
    //     }
    // });
});

// Parallax

const parallaxImages = document.querySelectorAll('[data-parallax-img]');

parallaxImages.forEach(parallax => {

    if (parallax) {

        document.addEventListener("mousemove", (e) => {

            const x = (e.clientX / window.innerWidth - 0.5) * 40;
            const y = (e.clientY / window.innerHeight - 0.5) * 40;

            parallax.style.transform = `translate(${x}px, ${y}px)`;
        });

    }

})

// Slider

const swiper1 = new Swiper('.production-slider', {
    spaceBetween: 12,
    slidesPerGroup: 1,
    speed: 1500,
    pagination: {
        el: '.production-pagination',
        clickable: true,
    },
    breakpoints: {
        320: {
            slidesPerView: 1,
        },
        480: {
            slidesPerView: 'auto',
        },
        1250: {
            slidesPerView: 3,
        },
    }
});

const swiper2 = new Swiper('.partners-slider', {
    slidesPerGroup: 1,
    speed: 1500,
    slidesPerView: 'auto',
    breakpoints: {
        320: {
            spaceBetween: 24,
        },
        480: {
            spaceBetween: 32,
        },
    }
});

const swiper3 = new Swiper('.certificates-slider', {
    slidesPerGroup: 1,
    speed: 1500,
    slidesPerView: 'auto',
    breakpoints: {
        320: {
            spaceBetween: 24,
        },
        480: {
            spaceBetween: 32,
        },
    }
});