$(document).ready(function () {
    // AOS Optimization
    function initAOS() {
        if (!window.aosLoaded) {
            window.aosLoaded = true;
            if (typeof AOS !== "undefined") {
                AOS.init({
                    disable: "mobile",
                    once: true
                });
            }
        }
    }

    // Scrollbar and Scroll triggers
    $(window).on("load", function () {
        $(".content").mCustomScrollbar({
            autoHideScrollbar: true
        });
    });

    $(window).on("scroll", initAOS);
    setTimeout(initAOS, 2000);

    // Custom Modal Popup Handler (Manual jQuery)
    $(".various").click(function (e) {
        e.preventDefault();
        e.stopPropagation();

        var pkgName = $(this).attr("name") || "";
        var pkgTitle = $(this).attr("title") || "";

        $(".popupform .leadprice").val(pkgName);
        $(".popupform .pp-title").html(pkgTitle);
        $(".serviii").val(pkgTitle);

        $(".pp-title").each(function (index, element) {
            var $el = $(element);
            var words = $el.text().split(/\s+/);
            if (words.length > 1) {
                var lastWord = words.pop();
                var firstPart = words.join(" ");
                $el.html(firstPart + ' <span class="last-word">' + lastWord + "</span>");
            }
        });

        // Open the custom modal
        $("#customModalOverlay").fadeIn(400).css("display", "flex");
        $("body").css("overflow", "hidden"); // Prevent scrolling when modal is open
    });

    // Close Custom Modal
    $("#customModalClose").click(function () {
        $("#customModalOverlay").fadeOut(400);
        $("body").css("overflow", "auto");
    });

    // Close on overlay click
    $("#customModalOverlay").click(function (e) {
        if (e.target === this) {
            $(this).fadeOut(400);
            $("body").css("overflow", "auto");
        }
    });

    // Slick Slider
    $(".slider").slick({
        slidesToShow: 1,
        infinite: false,
        slidesToScroll: 1,
        arrows: false,
        asNavFor: ".slider-nav-thumbnails"
    });

    $(".slider-nav-thumbnails").slick({
        slidesToShow: 6,
        vertical: true,
        verticalSwiping: true,
        infinite: false,
        slidesToScroll: 1,
        asNavFor: ".slider",
        dots: false,
        focusOnSelect: true
    });

    $(".slider").on("beforeChange", function (event, slick, currentSlide, nextSlide) {
        $(".slider-nav-thumbnails .slick-slide").removeClass("slick-active").eq(nextSlide).addClass("slick-active");
    });

    // Parallax
    var scene = document.getElementById("scene");
    if (scene && typeof Parallax !== "undefined") {
        new Parallax(scene);
    }

    // Menu Toggle
    $(".menu-bottom").on("click", function () {
        $("html").toggleClass("menu-open");
        $(this).toggleClass("click");
    });

    // Carousels
    $(".owl-testimonial").owlCarousel({
        loop: true,
        margin: 20,
        nav: true,
        dots: false,
        autoplay: true,
        autoplayTimeout: 13000,
        navText: ['<img src="assets/images/owl-prev.webp" alt="">', '<img src="assets/images/owl-next.webp" alt="">'],
        responsive: {
            0: { items: 1 },
            1000: { items: 1 }
        }
    });

    $(".owl-about").owlCarousel({
        loop: true,
        margin: 10,
        nav: false,
        dots: true,
        autoplay: true,
        responsive: {
            0: { items: 2 },
            600: { items: 3 },
            1000: { items: 4 }
        }
    });

    // Fancybox initialization (remains for other potential uses)
    if ($.fn.fancybox) {
        $("[data-fancybox]").fancybox({
            hash: false
        });
    }

    // Validation
    $(".validate-popupform, .contact_form, .validate-popupform-quote").validate();

    // Isotope
    var $grid = $(".grid").isotope({
        itemSelector: ".element-item",
        layoutMode: "fitRows"
    });

    $("#filters").on("click", "button", function () {
        var filterValue = $(this).attr("data-filter");
        $grid.isotope({ filter: filterValue });
        $(this).addClass("is-checked").siblings().removeClass("is-checked");
    });

    var itemsToShow = 8;
    function updateIsotope(count) {
        $grid.find(".hidden").removeClass("hidden");
        var isotopeInstance = $grid.data("isotope");
        if (isotopeInstance) {
            var itemsToHide = isotopeInstance.filteredItems.slice(count, isotopeInstance.filteredItems.length).map(item => item.element);
            $(itemsToHide).addClass("hidden");
            $grid.isotope("layout");
            (itemsToHide.length === 0) ? $("#showMore").hide() : $("#showMore").show();
        }
    }

    updateIsotope(8);
    $("#showMore").click(function () {
        itemsToShow += 8;
        updateIsotope(itemsToShow);
    });

    // Lazy load
    if ($.fn.lazy) {
        $(".lazy").lazy();
    }
});