(function($) {
    "use strict";

    // Pace Options
    Pace.Options = {
        ajax: false,
        document: false,
        eventLag: false
    };

    // Pace completion event handler
    Pace.on("done", function() {
        $("#preloader").addClass("isdone");
        $(".loading").addClass("isdone");
    });

    // Mouse cursor animation
    function mousecursor() {
        const e = document.querySelector(".cursor-inner");
        const t = document.querySelector(".cursor-outer");

        if (!e || !t) return;  // Check if the elements exist

        let n, i = 0, o = false;

        window.onmousemove = function(s) {
            if (!o) {
                t.style.transform = `translate(${s.clientX}px, ${s.clientY}px)`;
            }
            e.style.transform = `translate(${s.clientX}px, ${s.clientY}px)`;
            n = s.clientY;
            i = s.clientX;
        };

        $("body").on("mouseenter", "a, .cursor-pointer", function() {
            e.classList.add("cursor-hover");
            t.classList.add("cursor-hover");
        });

        $("body").on("mouseleave", "a, .cursor-pointer", function() {
            if ($(this).is("a") && $(this).closest(".cursor-pointer").length) return;
            e.classList.remove("cursor-hover");
            t.classList.remove("cursor-hover");
        });

        e.style.visibility = "visible";
        t.style.visibility = "visible";
    }

    $(function() {
        mousecursor();
    });

    // MeanMenu for mobile navigation
    $(".header-area nav").meanmenu();

    // Fixed menu on scroll
    var fixed_top = $(".header-area");
    $(window).on("scroll", function() {
        if ($(this).scrollTop() > 300) {
            fixed_top.addClass("menu-fixed animated fadeInDown");
        } else {
            fixed_top.removeClass("menu-fixed fadeInDown");
        }
    });

    // Highlight active section in navigation
    $(window).scroll(function() {
        var scrollPos = $(document).scrollTop();
        $("section").each(function() {
            var offsetTop = $(this).offset().top;
            var height = $(this).height();
            var id = $(this).attr("id");

            if (scrollPos >= offsetTop && scrollPos < offsetTop + height) {
                $('nav a[href="#' + id + '"]').addClass("primary-color");
            } else {
                $('nav a[href="#' + id + '"]').removeClass("primary-color");
            }
        });
    });

    // Set theme color
    function setThemeColor(color) {
        const root = document.documentElement;
        root.setAttribute("data-theme", color);
    }

    // Search functionality
    var $searchWrap = $(".search-wrap");
    var $navSearch = $(".nav-search");
    var $searchClose = $("#search-close");

    $(".search-trigger").on("click", function(e) {
        e.preventDefault();
        $searchWrap.animate({ opacity: "toggle" }, 500);
        $navSearch.add($searchClose).addClass("open");
    });

    $(".search-close").on("click", function(e) {
        e.preventDefault();
        $searchWrap.animate({ opacity: "toggle" }, 500);
        $navSearch.add($searchClose).removeClass("open");
    });

    function closeSearch() {
        $searchWrap.fadeOut(200);
        $navSearch.add($searchClose).removeClass("open");
    }

    $(document.body).on("click", function(e) {
        closeSearch();
    });

    $(".search-trigger, .main-search-input").on("click", function(e) {
        e.stopPropagation();
    });

    // Swiper Slider Initialization
    var sliderInit1 = new Swiper(".banner__slider", {
        loop: true,
        slidesPerView: 1,
        effect: "fade",
        speed: 3000,
        autoplay: {
            delay: 7000,
            disableOnInteraction: false,
        },
        pagination: {
            el: ".banner__dot",
            clickable: true,
        },
    });

    function animated_swiper(selector, init) {
        var animated = function() {
            $(selector + " [data-animation]").each(function() {
                var anim = $(this).data("animation");
                var delay = $(this).data("delay");
                var duration = $(this).data("duration");

                $(this).removeClass("anim" + anim).addClass(anim + " animated")
                    .css({
                        webkitAnimationDelay: delay,
                        animationDelay: delay,
                        webkitAnimationDuration: duration,
                        animationDuration: duration,
                    })
                    .one("animationend", function() {
                        $(this).removeClass(anim + " animated");
                    });
            });
        };

        animated();
        init.on("slideChange", function() {
            $(selector + " [data-animation]").removeClass("animated");
        });
        init.on("slideChange", animated);
    }

    animated_swiper(".banner__slider", sliderInit1);

    // Additional sliders for case, testimonial, service, etc.
    var swiperSettings = [
        { selector: ".case__slider", settings: { loop: true, spaceBetween: 24, speed: 800, autoplay: { delay: 4000, disableOnInteraction: false }, pagination: { el: ".case__dot", clickable: true } } },
        { selector: ".brand__slider", settings: { loop: true, spaceBetween: 30, speed: 300, autoplay: { delay: 3000, disableOnInteraction: false }, breakpoints: { 1200: { slidesPerView: 5 }, 992: { slidesPerView: 4 }, 575: { slidesPerView: 3 }, 320: { slidesPerView: 2 } } } },
        // Add more sliders as needed
    ];

    swiperSettings.forEach(function(slider) {
        new Swiper(slider.selector, slider.settings);
    });

    // Sidebar toggle
    $(document).on("click", "#openButton", function() {
        $("#targetElement").removeClass("sidebar__hide");
    });

    $(document).on("click", "#closeButton", function() {
        $("#targetElement").addClass("sidebar__hide");
    });

    // Service item hover effect
    $(".service__item").hover(function() {
        $(".service__item").removeClass("active");
        $(this).addClass("active");
    });

    // Set background image from data-background attribute
    $("[data-background]").each(function() {
        $(this).css("background-image", "url(" + $(this).attr("data-background") + ")");
    });

    // Video popup
    $(".video-popup").magnificPopup({
        type: "iframe",
        iframe: {
            markup: '<div class="mfp-iframe-scaler">' +
                '<div class="mfp-close"></div>' +
                '<iframe class="mfp-iframe" frameborder="0" allowfullscreen></iframe>' +
                "</div>",
            patterns: {
                youtube: { index: "youtube.com/", id: "v=", src: "https://www.youtube.com/embed/%id%?autoplay=1" },
                vimeo: { index: "vimeo.com/", id: "/", src: "//player.vimeo.com/video/%id%?autoplay=1" },
                gmaps: { index: "//maps.google.", src: "%id%&output=embed" }
            },
            srcAction: "iframe_src",
        },
    });

    // Counter up
    $(".count").counterUp({ delay: 30, time: 3000 });
    $(".progress-count").counterUp({ delay: 30, time: 1000 });

    // Nice select
    $(document).ready(function() {
        $("select").niceSelect();
    });

    // Footer popup
    $(".footer-popup").magnificPopup({
        type: "image",
        gallery: {
            enabled: true
        },
    });

    // Scroll to top button
    var offset = 50;
    var duration = 950;

    $(window).on("scroll", function() {
        if ($(this).scrollTop() > offset) {
            $(".scroll-up").addClass("active-scroll");
        } else {
            $(".scroll-up").removeClass("active-scroll");
        }
    });

    $(".scroll-up").on("click", function(event) {
        event.preventDefault();
        $("html, body").animate({ scrollTop: 0 }, duration);
        return false;
    });
})(jQuery);
