
(function($) {
    "use strict";
   
    // Sticky Header
    // Sticky Header
        let $header = $(".header-area");
        $(window).on("scroll", function() {
            $(window).scrollTop() > 500 ? $header.addClass("sticky") : $header.removeClass("sticky");
        });
       

    
    let screenWidth = window.screen.width,
        $overlaySwitcherClose = $(".overlay-switcher-close");

    // Preloader
    $(document).ready(function() {
        $("#container").addClass("loaded");
        if ($("#container").hasClass("loaded")) {
            $("#preloader").delay(1000).queue(function() {
                $(this).remove();
            });
        }
    });

    // ScrollSmoother
    gsap.registerPlugin(ScrollTrigger);
    if ($(".image-popup").length && $.fn.magnificPopup) {
        $(".image-popup").magnificPopup({ type: "image", gallery: { enabled: true } });
    }
    if ($(".video-popup").length && $.fn.magnificPopup) {
        $(".video-popup").magnificPopup({ type: "iframe" });
    }
    if (screenWidth > 767 && $("#has_smooth").hasClass("has-smooth")) {
        ScrollSmoother.create({ smooth: 0.5, effects: screenWidth >= 1025, smoothTouch: 0.5, normalizeScroll: false, ignoreMobileResize: true });
    }

    // Counter
    if (window.counterUp) {
        let counterUp = window.counterUp.default,
            observer = new IntersectionObserver(entries => {
                entries.forEach(entry => {
                    let target = entry.target;
                    if (entry.isIntersecting && !target.classList.contains("is-visible")) {
                        counterUp(target, { duration: 1500, delay: 16 });
                        target.classList.add("is-visible");
                    }
                });
            }, { threshold: 1 });

        $(".wc-counter").each(function() {
            observer.observe(this);
        });
    }

    // Scroll to top
    let $scrollTop = $("#scroll_top");
    if ($scrollTop.length) {
        $(window).on("scroll", function() {
            $(document).scrollTop() > 50 ? $scrollTop.addClass("showed") : $scrollTop.removeClass("showed");
        });
        $scrollTop.on("click", function() {
            $("html, body").animate({ scrollTop: 0 }, "slow");
        });
    }

    // MeanMenu
    $(".offcanvas__menu").meanmenu({ meanScreenWidth: "5000", meanMenuContainer: ".offcanvas__menu-wrapper", meanMenuCloseSize: "28px" });
    $(".main-menu").meanmenu({ meanScreenWidth: "1199", meanMenuContainer: ".offcanvas__menu-wrapper", meanMenuCloseSize: "28px" });

    // Button hover effect
    $(".btn-move").each(function(index) {
        $(this).on("mousemove", function(e) {
            let $btnItem = $(".btn-item").eq(index),
                offset = $(this).offset(),
                x = (e.pageX - offset.left - $(this).width() / 2) / $(this).width() * 80,
                y = (e.pageY - offset.top - $(this).height() / 2) / $(this).height() * 80;
            gsap.to($btnItem, 0.3, { x: x, y: y, scale: 1.2, ease: "Power2.easeOut" });
        }).on("mouseleave", function() {
            let $btnItem = $(".btn-item").eq(index);
            gsap.to($btnItem, 0.3, { x: 0, y: 0, scale: 1, ease: "Power2.easeOut" });
        });
    });

    // Button background hover effect
    $(".btn-hover-bgchange").each(function() {
        $(this).append("<span></span>");
        $(this).on("mouseenter mouseout", function(e) {
            let offset = $(this).offset(),
                x = e.pageX - offset.left,
                y = e.pageY - offset.top;
            $(this).find("span").css({ top: y, left: x });
        });
    });

    // Pinning elements with ScrollTrigger
    if ($(".pin__element").length && screenWidth > 991) {
        gsap.to(".pin__element", { scrollTrigger: { trigger: ".pin__area", pin: ".pin__element", start: "top top", end: "bottom bottom", pinSpacing: false } });
    }
    if ($(".pin__elem").length && screenWidth > 991) {
        gsap.utils.toArray(".pin__elem").forEach(function(elem, index, arr) {
            if (index !== arr.length - 1) {
                ScrollTrigger.create({ trigger: elem, start: "top top", pin: true, pinSpacing: false });
            }
        });
    }

    // Cursor animation
    $(document).on("mousemove", function(e) {
        gsap.timeline({ defaults: { x: e.clientX, y: e.clientY } })
            .to(".cursor1", { ease: "power2.out" })
            .to(".cursor2", { ease: "power2.out" }, "-=0.4");
    });

    // Image animation reveal
    $(".img_anim_reveal").each(function() {
        let img = $(this).find("img");
        gsap.timeline({
            scrollTrigger: {
                trigger: this,
                start: "top 50%",
            }
        })
        .set(this, { autoAlpha: 1 })
        .from(this, 1.5, { xPercent: -100, ease: "Power2.out" })
        .from(img, 1.5, { xPercent: 100, scale: 1.3, delay: -1.5, ease: "Power2.out" });
    });

    // Offcanvas toggle
    $("#open_offcanvas").on("click", function() {
        $(".offcanvas-area").css({ opacity: "1", visibility: "visible" });
    });
    $("#close_offcanvas").on("click", function() {
        $(".offcanvas-area").css({ opacity: "0", visibility: "hidden" });
    });

    // Switcher open/close
    $("#switcher_open").on("click", function() {
        $(this).hide();
        $("#switcher_close").show();
        $(".switcher__icon").css("right", "280px");
        $(".switcher__items").css({ right: "0" });
        $overlaySwitcherClose.addClass("show-overlay");
    });
    $("#switcher_close").on("click", function() {
        $(this).hide();
        $("#switcher_open").show();
        $(".switcher__icon").css("right", "0");
        $(".switcher__items").css({ right: "-280px" });
        $overlaySwitcherClose.removeClass("show-overlay");
    });
    $(".overlay-switcher-close").on("click", function() {
        $("#switcher_open").show();
        $("#switcher_close").hide();
        $(".switcher__icon").css("right", "0");
        $(".switcher__items").css({ right: "-280px" });
        $overlaySwitcherClose.removeClass("show-overlay");
    });

    // Mode toggle
    $(".mode-type button").on("click", function() {
        $(this).addClass("active").siblings().removeClass("active");
        $("body").toggleClass("dark", $(this).data("mode") === "dark");
    });

    // Layout type toggle
    $(".layout-type button").on("click", function() {
        $(this).addClass("active").siblings().removeClass("active");
        $("#smooth-content, header").toggleClass("box-layout", $(this).data("mode") === "box-layout");
    });

    // Language direction toggle
    $(".lang_dir button").on("click", function() {
        $(this).addClass("active").siblings().removeClass("active");
        $("body").toggleClass("dir-rtl", $(this).data("mode") === "rtl");
    });

    // Cursor style toggle
    $("#cursor_style").on("change", function() {
        let showCursor = $(this).val() !== "1";
        $(".cursor1, .cursor2").toggle(showCursor);
    });

    // Scroll to top link
    $("a[href='#top']").on("click", function() {
        $("html, body").animate({ scrollTop: 0 }, "slow");
        return false;
    });

    // Toggle switcher pane
    $(".wcf__toggle_switcher input").on("change", function() {
        $(".toggle-pane").toggleClass("show");
        $(".before_label, .after_label").toggleClass("active");
    });
})(jQuery);
