(function ($) {
    "use strict";

    $(function () {
        const $window = $(window);
        const $document = $(document);
        const $siteHeader = $(".site_header");
        const $progressWrap = $(".progress-wrap");

        // Back-to-top progress ring setup (if present on page)
        const progressPath = document.querySelector(".progress-wrap path");
        let pathLength = 0;
        if (progressPath) {
            pathLength = progressPath.getTotalLength();
            progressPath.style.transition = progressPath.style.WebkitTransition = "none";
            progressPath.style.strokeDasharray = pathLength + " " + pathLength;
            progressPath.style.strokeDashoffset = pathLength;
            progressPath.getBoundingClientRect();
            progressPath.style.transition = progressPath.style.WebkitTransition = "stroke-dashoffset 10ms linear";
        }

        const progressOffset = 50;
        const progressDuration = 550;
        let scrollFrameRequested = false;

        function updateScrollStates() {
            const scrollTop = $window.scrollTop();
            const docHeight = $document.height() - $window.height();

            if (progressPath && docHeight > 0) {
                const progress = pathLength - (scrollTop * pathLength / docHeight);
                progressPath.style.strokeDashoffset = progress;
            }

            if ($siteHeader.length) {
                $siteHeader.toggleClass("sticky", scrollTop >= 100);
            }

            if ($progressWrap.length) {
                $progressWrap.toggleClass("active-progress", scrollTop > progressOffset);
            }
        }

        function onScroll() {
            if (scrollFrameRequested) return;
            scrollFrameRequested = true;
            requestAnimationFrame(function () {
                updateScrollStates();
                scrollFrameRequested = false;
            });
        }

        updateScrollStates();
        $window.on("scroll", onScroll);

        $progressWrap.on("click", function (event) {
            event.preventDefault();
            $("html, body").animate({ scrollTop: 0 }, progressDuration);
            return false;
        });

        // menu_toggle
        $(".menu_toggle").on("click", function (event) {
            event.preventDefault();
            $("body").toggleClass("menu_active");
        });

        // language-select
        $(".language-select").on("click", function () {
            $(".language-select").removeClass("active");
            $(this).addClass("active");
        });

        // megamenu1-totgle-btn
        $(".megamenu1-totgle-btn").on("click", function () {
            $(".mega-menu-one").toggleClass("show");
        });

        // menutwo-dropdown-btn
        $(".menutwo-toggle").on("click", function () {
            $(".mega-menu-two").toggleClass("show");
        });

        // select_btn
        $("button.select_btn").on("click", function () {
            $(this).toggleClass("active");
        });

        // heroSlider
        if (document.querySelector(".heroSlider")) {
            new Swiper(".heroSlider", {
                loop: true,
                slidesPerView: 1,
                effect: "fade",
                fadeEffect: {
                    crossFade: true,
                },
                autoplay: {
                    delay: 3000,
                    disableOnInteraction: false,
                },
                pagination: {
                    el: ".swiper-pagination",
                    clickable: true,
                },
            });
        }

        // auto-slider
        if (document.querySelector(".auto-slider")) {
            new Swiper(".auto-slider", {
                loop: false,
                slidesPerView: "auto",
                
                autoplay: {
                    delay: 3000,
                    disableOnInteraction: false,
                },

                spaceBetween: 20,
                pagination: {
                    el: ".swiper-pagination",
                    clickable: true,
                },
            });
        }

        // as-fotorelacja-slider
        if (document.querySelector(".as-fotorelacja-slider")) {
            new Swiper(".as-fotorelacja-slider", {
                loop: true,
                spaceBetween: 20,
                autoplay: {
                    delay: 3000,
                    disableOnInteraction: false,
                },
                navigation: {
                    nextEl: ".minione-button-next",
                    prevEl: ".minione-button-prev",
                },
                breakpoints: {
                    575: {
                        slidesPerView: 2,
                        spaceBetween: 10
                    },
                    767: {
                        slidesPerView: 3,
                        spaceBetween: 15
                    },
                    991: {
                        slidesPerView: 4,
                        spaceBetween: 15
                    },
                }
            });
        }

        // as-fotorelacja-slider2
        if (document.querySelector(".as-fotorelacja-slider2")) {
            new Swiper(".as-fotorelacja-slider2", {
                slidesPerView: 2,
                loop: true,
                spaceBetween: 20,
                autoplay: {
                    delay: 3000,
                    disableOnInteraction: false,
                },
                navigation: {
                    nextEl: ".as-history-button-next",
                    prevEl: ".as-history-button-prev",
                },
                breakpoints: {
                    320: {
                        slidesPerView: 1,
                        spaceBetween: 10
                    },
                    767: {
                        slidesPerView: 1,
                        spaceBetween: 10
                    },
                    768: {
                        slidesPerView: 2,
                        spaceBetween: 10
                    },
                }
            });
        }

        // camera-video
        $(".camera-video").each(function () {
            const video = this;

            video.play().catch(function () { });

            video.addEventListener("ended", function () {
                video.currentTime = 0;
                video.play().catch(function () { });
            });

            video.addEventListener("pause", function () {
                if (!video.ended) {
                    video.play().catch(function () { });
                }
            });
        });

        // sort-trigger
        $document.on("click", ".sort-trigger", function (e) {
            e.stopPropagation();
            const box = $(this).closest(".sort-select");
            $(".sort-select").not(box).removeClass("active").find(".sort-menu").slideUp(150);
            box.toggleClass("active").find(".sort-menu").slideToggle(150);
        });

        $document.on("click", ".sort-option", function () {
            const option = $(this);
            const box = option.closest(".sort-select");
            option.addClass("active").siblings().removeClass("active");
            box.find(".sort-label").text(option.text().toUpperCase());
            $(box.data("target")).text("Selected: " + option.data("value"));
            box.removeClass("active").find(".sort-menu").slideUp(150);
            box.trigger("sortChange", [option.data("value"), option.text()]);
        });

        $document.on("click", function () {
            $(".sort-select").removeClass("active").find(".sort-menu").slideUp(150);
        });

        $(".sort-select").on("sortChange", function (e, value, label) {
            console.log("Changed:", value, label);
        });
    });
})(jQuery);
