$(document).ready(function() {

    var wind = $(window);

    wow = new WOW({
        boxClass: 'wow',
        animateClass: 'animated',
        offset: 200,
        mobile: false,
        live: false
    });
    wow.init();


    // ---------- to top -----------
    $('.to-top').click(function() {
        $('html, body').animate({
            scrollTop: 0
        }, 1000);
        return false;
    });


});


// ------------ swiper sliders -----------
$(document).ready(function() {

    // ------------ tc-header-slider4 -----------
    var swiper = new Swiper('.partners-slider', {
        slidesPerView: "auto",
        spaceBetween: 30,
        centeredSlides: true,
        pagination: false,
        navigation: false,
        mousewheel: false,
        keyboard: true,
        speed: 15000,
        allowTouchMove: false,
        a11y: false,
        autoplay: {
            delay: 1,
        },
        loop: true,
                breakpoints: {
            0: {
                slidesPerView: 1,
            },
            480: {
                slidesPerView: 2,
            },
            787: {
                slidesPerView: 3,
            },
            991: {
                slidesPerView: 4,
            },
            1200: {
                slidesPerView: 4,
            }
        }
    });


});


/* --------------------------------------------------------------
                        [ gsap scripts ]
-------------------------------------------------------------- */

// Run only if window width > 991px
if (window.innerWidth > 991) {

    $(function () {
        gsap.registerPlugin(ScrollTrigger, ScrollSmoother);

        // ---------- ScrollSmoother ----------
        let smoother = ScrollSmoother.create({
            wrapper: '.smooth-wrapper',
            content: '.smooth-content',
            smooth: 1.1,
            effects: true,
        });

        ScrollTrigger.refresh();

        // gs image parallax
        const parallaxImages = document.querySelectorAll('.gs-img-parallax');
        parallaxImages.forEach((container) => {
            const img = container.querySelector('img');
            container.style.overflow = 'hidden';
            // effect Parallax + Zoom
            gsap.to(img, {
                yPercent: -20,
                scale: 1.2,
                ease: 'none',
                scrollTrigger: {
                    trigger: container,
                    start: 'top bottom',
                    end: 'bottom top',
                    scrub: 1,
                    // pin: true,
                }
            });
        });

    });

    // ----------- pin cards ----------
    const items = gsap.utils.toArray(".pin-card");
    const lastCard = items[items.length - 1];

    items.forEach((item, index) => {

        let tl = gsap.timeline({
            scrollTrigger: {
                trigger: item,
                start: "top top",
                endTrigger: lastCard,
                end: "bottom top+=500",
                pin: true,
                pinSpacing: false,
                scrub: true,
            }
        });
    });

    // horizontal scroll
    let horizontal = document.querySelector(".horizontal");
    gsap.to(".horizontal", {
        x: () => -(horizontal.scrollWidth - window.innerWidth),
        scrollTrigger: {
            trigger: "#horizontal-scoll",
            pin: true,
            scrub: 1,
            start: "top -40%",
            end: () => "+=" + (horizontal.scrollWidth - window.innerWidth),
            invalidateOnRefresh: true,
        }
    });


    // flipScroll1 animation
    const flipScrollCards = document.querySelectorAll('.flip-scroll-st1');

    flipScrollCards.forEach((card) => {
        gsap.to(card,
            {
                y: 0,
                rotateX: 0,
                scale: 1,
                opacity: 1,
                ease: "power1.out",
                scrollTrigger: {
                    trigger: card,
                    start: "top 90%",
                    end: "bottom top",
                    scrub: 1,
                    invalidateOnRefresh: true,
                }
            }
        );
    });


    // letters animation 
    window.addEventListener("load", () => {

        const smoother = window.ScrollSmoother ? ScrollSmoother.get() : null;

        document.querySelectorAll(".letters-line").forEach(title => {

            if (title.dataset.lettersProcessed) return;
            title.dataset.lettersProcessed = true;

            function processTextNodes(element) {
                const walker = document.createTreeWalker(element, NodeFilter.SHOW_TEXT, null, false);
                const textNodes = [];
                let node;
                while (node = walker.nextNode()) {
                    if (node.textContent.trim() !== '') textNodes.push(node);
                }

                textNodes.forEach(textNode => {
                    const text = textNode.textContent;
                    const parent = textNode.parentNode;

                    const words = text.split(/(\s+)/);
                    const fragment = document.createDocumentFragment();

                    words.forEach(word => {
                        if (word.trim() === "") {
                            fragment.appendChild(document.createTextNode(word));
                        } else {
                            const wordWrapper = document.createElement("span");
                            wordWrapper.className = "word";
                            wordWrapper.style.display = "inline-block";
                            wordWrapper.style.whiteSpace = "nowrap";
                            wordWrapper.innerHTML = word;

                            fragment.appendChild(wordWrapper);
                        }
                    });

                    parent.replaceChild(fragment, textNode);
                });
            }

            processTextNodes(title);

            gsap.fromTo(
                title.querySelectorAll(".word"),
                {
                    scale: 2,
                    filter: "blur(100px)",
                    opacity: 0,
                    y: 50
                },
                {
                    scrollTrigger: {
                        trigger: title,
                        scroller: smoother ? smoother.scrollContainer : window,
                        start: "top 80%",
                        toggleActions: "play none none reverse",
                    },
                    scale: 1,
                    filter: "blur(0px)",
                    opacity: 1,
                    y: 0,
                    duration: 0.8,
                    delay: -0.3,
                    stagger: 0.1,
                    ease: "power4.out"
                }
            );
        });

        ScrollTrigger.refresh();
    });
}


// loader scripts 
const particlesContainer = document.getElementById('particles');
const numParticles = 12;
const radius = 150;

for (let i = 0; i < numParticles; i++) {
    const particle = document.createElement('div');
    particle.className = 'particle';
    particlesContainer.appendChild(particle);
    
    const angle = (360 / numParticles) * i;
    
    // Position particle on circle
    const x = radius * Math.cos(angle * Math.PI / 180);
    const y = radius * Math.sin(angle * Math.PI / 180);
    
    gsap.set(particle, {
        x: x,
        y: y
    });
    
    // Rotate around center
    gsap.to(particle, {
        rotation: 360,
        transformOrigin: `${-x}px ${-y}px`,
        repeat: -1,
        duration: 4,
        ease: 'none',
        delay: i * 0.1
    });
}


window.addEventListener("load", () => {
    $('.loader-wrapper').fadeOut('slow', function() {
        $(this).remove();
    });
});
