// intialization plugins
$(document).ready(function () {

    //animation icon toggler of navbar
    $(`.navbar-toggler`).click(function() {
        $(`.navbar-toggler`).toggleClass(`active`);
    });

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

    //fancybox
    $("[data-fancybox]").fancybox({
        selector: '[data-fancybox="images"]',
        // loop: true
    });

    //  loader 
    // $(window).on("load", function() {
    //     $("#preloader").addClass("isdone");
    // });

});

// Swiper
$(document).ready(function () {

    //////////////////// partners_swiper  ////////////////////
    var swiper = new Swiper('.partners_sec .partners_swiper', {
        // loop: true,
        speed: 900,
        slidesPerView: 1,
        spaceBetween: 15,
        // autoplay: {
        //     delay: 2500,
        // },
        navigation: {
            nextEl: '.partners_sec .swiper-button-next',
            prevEl: '.partners_sec .swiper-button-prev',
        },
        pagination: {
            el: '.partners_sec .swiper-pagination',
            clickable: true
        },
        breakpoints: {
            640: {
                slidesPerView: 2,
                spaceBetween: 20,
            },
            768: {
                slidesPerView: 3,
                spaceBetween: 20,
            },
            1024: {
                slidesPerView: 5,
                spaceBetween: 20,
            },
        }
    });

    //////////////////// Swiper  ////////////////////
    // var swiper = new Swiper(' .swiper-container', {
    //     // loop: true,
    //     speed: 900,
    //     slidesPerView: 1,
    //     spaceBetween: 15,
    //     // autoplay: {
    //     //     delay: 2500,
    //     // },
    //     //In Tabs
    //     // observer: true,
    //     // observeParents: true,
    //     navigation: {
    //         nextEl: '.swiper-button-next',
    //         prevEl: '.swiper-button-prev',
    //     },
    //     pagination: {
    //         el: '.swiper-pagination',
    //         clickable: true
    //     },
    //     breakpoints: {
    //         640: {
    //             slidesPerView: 2,
    //             spaceBetween: 20,
    //         },
    //         768: {
    //             slidesPerView: 3,
    //             spaceBetween: 20,
    //         },
    //         1024: {
    //             slidesPerView: 5,
    //             spaceBetween: 20,
    //             // Prevent swiper in lg screens
    //             // allowTouchMove: false,
    //             // preventClicks: false
    //         },
    //     }
    // });

});


// GSAP 
// GSAP
$(document).ready(function () {

    gsap.registerPlugin(
        ScrollTrigger,
        ScrollSmoother,
        SplitText
    );

    const smoother = ScrollSmoother.create({
        wrapper: "#smooth-wrapper",
        content: "#smooth-content",
        smooth: 1.2,
        effects: true,
        normalizeScroll: true
    });

    ScrollTrigger.config({
        ignoreMobileResize: true
    });

    window.addEventListener("load", () => {
        ScrollTrigger.refresh();
    });

    // =============================
    // GLOBAL HEADER ANIMATIONS
    // =============================

    // Fade Up
    gsap.utils.toArray(".gsap-fade-up").forEach(el => {

        gsap.fromTo(el,
            {
                y: 60,
                opacity: 0
            },
            {
                y: 0,
                opacity: 1,
                duration: 1,
                ease: "power3.out",
                scrollTrigger: {
                    trigger: el,
                    start: "top 85%",
                    toggleActions: "play reverse play reverse"
                }
            }
        );

    });

    // Fade Left
    gsap.utils.toArray(".gsap-fade-left").forEach(el => {

        gsap.fromTo(el,
            {
                x: -80,
                opacity: 0
            },
            {
                x: 0,
                opacity: 1,
                duration: 1,
                ease: "power3.out",
                scrollTrigger: {
                    trigger: el,
                    start: "top 85%",
                    toggleActions: "play reverse play reverse"
                }
            }
        );

    });

    // Fade Right
    gsap.utils.toArray(".gsap-fade-right").forEach(el => {

        gsap.fromTo(el,
            {
                x: 80,
                opacity: 0
            },
            {
                x: 0,
                opacity: 1,
                duration: 1,
                ease: "power3.out",
                scrollTrigger: {
                    trigger: el,
                    start: "top 85%",
                    toggleActions: "play reverse play reverse"
                }
            }
        );

    });

    gsap.utils.toArray(".gsap-text-reveal").forEach(text => {

        const split = new SplitText(text, {
            type: "lines,words"
        });

        gsap.from(split.words, {
            y: 120,
            opacity: 0,
            stagger: 0.06,
            duration: 1.2,
            ease: "power4.out",
            scrollTrigger: {
                trigger: text,
                start: "top 85%",
                toggleActions: "play reverse play reverse"
            }
        });

    });

    gsap.utils.toArray(".gsap-scale-in").forEach(el => {

        gsap.fromTo(el,
            {
                scale: 0.85,
                opacity: 0
            },
            {
                scale: 1,
                opacity: 1,
                duration: 0.8,
                ease: "back.out(1.7)",
                scrollTrigger: {
                    trigger: el,
                    start: "top 90%",
                    toggleActions: "play reverse play reverse"
                }
            }
        );

    });

    gsap.utils.toArray(".gsap-parallax-bg").forEach(bg => {

        gsap.to(bg, {
            yPercent: 15,
            ease: "none",
            scrollTrigger: {
                trigger: bg,
                start: "top bottom",
                end: "bottom top",
                scrub: true
            }
        });

    });

    gsap.from(".gsap-stagger-item", {
        y: 80,
        opacity: 0,
        duration: 1,
        stagger: 0.15,
        ease: "power3.out",
        scrollTrigger: {
            trigger: ".gsap-stagger-item",
            start: "top 85%",
            toggleActions: "play reverse play reverse"
        }
    });
        
    gsap.utils.toArray(".gsap-mask-reveal").forEach(el => {

        gsap.fromTo(el,
            {
                clipPath: "inset(0 100% 0 0 round 33px)"
            },
            {
                clipPath: "inset(0 0% 0 0 round 33px)",
                duration: 1.4,
                ease: "power4.out",
                scrollTrigger: {
                    trigger: el,
                    start: "top 80%",
                    toggleActions: "play reverse play reverse"
                }
            }
        );

    });

    gsap.utils.toArray(".gsap-image-reveal").forEach(box => {

        const img = box.querySelector(".secImg");

        gsap.fromTo(img,
            {
                scale: 1.3,
                y: 120,
                opacity: 0
            },
            {
                scale: 1,
                y: 0,
                opacity: 1,
                duration: 1.6,
                ease: "power3.out",
                scrollTrigger: {
                    trigger: box,
                    start: "top 85%",
                    toggleActions: "play reverse play reverse"
                }
            }
        );

    });

    gsap.utils.toArray(".gsap-float-icon").forEach(icon => {

        gsap.to(icon, {
            y: -15,
            duration: 2.5,
            ease: "sine.inOut",
            repeat: -1,
            yoyo: true
        });

    });

    gsap.utils.toArray(".gsap-reading-flow").forEach(box => {

        const items = box.children;

        gsap.from(items, {
            y: 40,
            opacity: 0,
            duration: 0.8,
            stagger: 0.18,
            ease: "power2.out",
            scrollTrigger: {
                trigger: box,
                start: "top 85%",
                toggleActions: "play reverse play reverse"
            }
        });

    });

    gsap.utils.toArray(".gsap-card-lift").forEach(card => {

        gsap.fromTo(card,
            {
                y: 120,
                rotateX: 8,
                opacity: 0
            },
            {
                y: 0,
                rotateX: 0,
                opacity: 1,
                duration: 1.2,
                ease: "back.out(1.4)",
                scrollTrigger: {
                    trigger: card,
                    start: "top 85%",
                    toggleActions: "play reverse play reverse"
                }
            }
        );

    });

    gsap.to(".partners_sec .inner", {
        backgroundPosition: "60% 40%",
        ease: "none",
        scrollTrigger: {
            trigger: ".partners_sec",
            start: "top bottom",
            end: "bottom top",
            scrub: true
        }
    });


    gsap.utils.toArray(".gsap-sec-head").forEach(head => {

        const items = head.children;

        gsap.fromTo(items,
            {
                y: 40,
                opacity: 0
            },
            {
                y: 0,
                opacity: 1,
                duration: 0.9,
                stagger: 0.12,
                ease: "power2.out",
                scrollTrigger: {
                    trigger: head,
                    start: "top 85%",
                    toggleActions: "play reverse play reverse"
                }
            }
        );

    });

    gsap.utils.toArray(".gsap-logo-reveal").forEach(logo => {

        gsap.fromTo(logo,
            {
                opacity: 0,
                scale: 0.9,
                filter: "grayscale(100%)"
            },
            {
                opacity: 1,
                scale: 1,
                filter: "grayscale(0%)",
                duration: 0.8,
                ease: "power2.out",
                scrollTrigger: {
                    trigger: logo,
                    start: "top 90%",
                    toggleActions: "play reverse play reverse"
                }
            }
        );

    });

    gsap.utils.toArray(".gsap-stage").forEach(stage=>{

        gsap.from(stage,{
            opacity:0,
            y:80,
            duration:1.1,
            ease:"power3.out",
            scrollTrigger:{
                trigger:stage,
                start:"top 80%",
                toggleActions:"play reverse play reverse"
            }
        });

    });

    gsap.utils.toArray(".gsap-card").forEach((card,i)=>{

        gsap.from(card,{
            opacity:0,
            scale:.94,
            duration:1,
            delay:i*0.06,
            ease:"power3.out",
            scrollTrigger:{
                trigger:card,
                start:"top 90%",
                toggleActions:"play reverse play reverse"
            }
        });

    });

    gsap.utils.toArray(".gsap-media-depth").forEach(media => {

        gsap.fromTo(media,
            {
                scale: 1.08,
                opacity: 0,
                filter: "blur(8px)"
            },
            {
                scale: 1,
                opacity: 1,
                filter: "blur(0px)",
                duration: 1.2,
                ease: "power3.out",
                scrollTrigger:{
                    trigger: media,
                    start: "top 85%",
                    toggleActions:"play reverse play reverse"
                }
            }
        );

    });
    
    gsap.utils.toArray(".gsap-panel").forEach(panel=>{

        gsap.from(panel,{
            opacity:0,
            duration:.7,
            ease:"power2.out",
            scrollTrigger:{
                trigger:panel,
                start:"top 95%",
                toggleActions:"play reverse play reverse"
            }
        });

    });

    gsap.utils.toArray(".gsap-article").forEach((article,i)=>{

        gsap.from(article,{
            x:60,
            opacity:0,
            duration:.9,
            delay:i*0.12,
            ease:"power2.out",
            scrollTrigger:{
                trigger:article,
                start:"top 92%",
                toggleActions:"play reverse play reverse"
            }
        });

    });

    gsap.utils.toArray(".gsap-reveal-soft").forEach(el=>{

        gsap.fromTo(el,
            {
                opacity:0,
                y:60,
                scale:.97
            },
            {
                opacity:1,
                y:0,
                scale:1,
                duration:1.2,
                ease:"power3.out",
                scrollTrigger:{
                    trigger:el,
                    start:"top 85%",
                    toggleActions:"play reverse play reverse"
                }
            }
        );

    });

    gsap.utils.toArray(".gsap-float-soft").forEach(icon=>{

        gsap.to(icon,{
            y:-8,
            duration:3,
            ease:"sine.inOut",
            repeat:-1,
            yoyo:true
        });

    });

    gsap.utils.toArray(".gsap-list-row").forEach((row,i)=>{

        gsap.from(row,{
            opacity:0,
            x:-40,
            duration:.7,
            delay:i*0.08,
            ease:"power2.out",
            scrollTrigger:{
                trigger:row,
                start:"top 92%",
                toggleActions:"play reverse play reverse"
            }
        });

    });

    gsap.from(".working_sec .pattern",{
        opacity:0,
        rotate:8,
        duration:1.5,
        ease:"power2.out",
        scrollTrigger:{
            trigger:".working_sec",
            start:"top 80%",
            toggleActions:"play reverse play reverse"
        }
    });

    gsap.utils.toArray(".gsap-form").forEach(form=>{

        gsap.from(form,{
            opacity:0,
            y:60,
            scale:.98,
            duration:1,
            ease:"power3.out",
            scrollTrigger:{
                trigger:form,
                start:"top 85%",
                toggleActions:"play reverse play reverse"
            }
        });

    });

    gsap.utils.toArray(".gsap-field").forEach((field,i)=>{

        gsap.from(field,{
            opacity:0,
            y:30,
            duration:.6,
            delay:i*0.06,
            ease:"power2.out",
            scrollTrigger:{
                trigger:field,
                start:"top 95%",
                toggleActions:"play reverse play reverse"
            }
        });

    });

    gsap.from(".gsap-button-focus",{
        opacity:0,
        scale:.85,
        duration:.7,
        ease:"back.out(1.6)",
        scrollTrigger:{
            trigger:".gsap-button-focus",
            start:"top 90%",
            toggleActions:"play reverse play reverse"
        }
    });

    gsap.from(".contact_sec .bg",{
        opacity:0,
        duration:1.5,
        ease:"power1.out",
        scrollTrigger:{
            trigger:".contact_sec",
            start:"top 80%",
            toggleActions:"play reverse play reverse"
        }
    });

    gsap.utils.toArray(".gsap-footer-col").forEach((col,i)=>{

        gsap.from(col,{
            opacity:0,
            y:40,
            duration:.8,
            delay:i*0.1,
            ease:"power2.out",
            scrollTrigger:{
                trigger:col,
                start:"top 95%",
                toggleActions:"play reverse play reverse"
            }
        });

    });

    gsap.from(".footer .starting_box .item",{
        opacity:0,
        x:30,
        stagger:.12,
        duration:.7,
        ease:"power2.out",
        scrollTrigger:{
            trigger:".footer",
            start:"top 85%",
            toggleActions:"play reverse play reverse"
        }
    });


});

document.addEventListener("DOMContentLoaded", function () {

    function enableMobileDropdown() {

        if (window.innerWidth <= 991) {

            document.querySelectorAll(".has_dropdown > .nav-link").forEach(function (link) {

                link.addEventListener("click", function (e) {

                    e.preventDefault();

                    let parent = this.parentElement;

                    // يقفل أي dropdown تاني
                    document.querySelectorAll(".has_dropdown").forEach(function (item) {
                        if (item !== parent) {
                            item.classList.remove("active");
                        }
                    });

                    parent.classList.toggle("active");

                });

            });

        }

    }

    enableMobileDropdown();

});