$(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;
    });


    // ---------- navbar scroll -----------
    wind.on("scroll", function () {

        var bodyScroll = wind.scrollTop(),
            navbar = $(".navbar")

        if (bodyScroll > 200) {

            navbar.addClass("nav-scroll");

        } else {

            navbar.removeClass("nav-scroll");
        }
    });


    // show_hide_password 
    $(".show_hide_password .show_pass").on('click', function (event) {
        event.preventDefault();
        if ($(this).siblings("input").attr("type") == "text") {
            $(this).siblings("input").attr('type', 'password');
            $(this).addClass("fa-eye-slash");
            $(this).removeClass("fa-eye");
        } else if ($(this).siblings("input").attr("type") == "password") {
            $(this).siblings("input").attr('type', 'text');
            $(this).removeClass("fa-eye-slash");
            $(this).addClass("fa-eye");
        }
    });


});



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

    // ------------ fields-slider -----------
    var swiper = new Swiper('.fields-slider', {
        slidesPerView: 5,
        spaceBetween: 30,
        // centeredSlides: true,
        speed: 1500,
        pagination: false,
        navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
        },
        mousewheel: false,
        keyboard: true,
        // autoplay: false,
        autoplay: {
            delay: 3000,
            disableOnInteraction: false,
            pauseOnMouseEnter: false,
        },
        loop: false,
        breakpoints: {
            0: {
                slidesPerView: 1,
            },
            480: {
                slidesPerView: 2,
            },
            787: {
                slidesPerView: 3,
            },
            991: {
                slidesPerView: 3,
            },
            1200: {
                slidesPerView: 3,
            }
        }
    });


});


/* --------------------------------------------------------------
                        [ 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,
            }
        });
    });


    // 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 130%",
                    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 none",
                    },
                    scale: 1,
                    filter: "blur(0px)",
                    opacity: 1,
                    y: 0,
                    duration: 0.8,
                    delay: -0.3,
                    stagger: 0.1,
                    ease: "power4.out"
                }
            );
        });

        ScrollTrigger.refresh();
    });


    gsap.to(".services .service-card", {
        scrollTrigger: {
            trigger: ".services",
            start: "top 40%",
            toggleActions: "play none none reverse"
        },
        marginLeft: 10,
        marginRight: 10,
        rotation: 0,
        // duration: 1.2,
        // stagger: 0.1,
        ease: "power2.out"
    });

    gsap.to(".features .pin-title", {
        scrollTrigger: {
            trigger: ".pin-title",
            start: "top 40%",
            end: "+=1800",
            pin: true,
            pinSpacing: false,
            scrub: 1
        },
        opacity: 0.1,
        filter: "blur(5px)",
        scale: 1.1,
    });

    gsap.from(".features .feat-card", {
        scrollTrigger: {
            trigger: ".features .cards",
            start: "top 25%",
            end: "+=1500",
            pin: true,
            scrub: 1
        },
        opacity: 0,
        filter: "blur(10px)",
        x: -1000,
        // y: 200,
        stagger: 0.5
    });


}

