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

    const menuToggle =
        document.getElementById("cbxMenuToggle");

    const navMenu =
        document.getElementById("cbxNavMenu");


    if (!menuToggle || !navMenu) {
        return;
    }


    menuToggle.addEventListener("click", function () {

        const isOpen =
            navMenu.classList.toggle("show");

        menuToggle.classList.toggle(
            "active",
            isOpen
        );

        menuToggle.setAttribute(
            "aria-expanded",
            isOpen ? "true" : "false"
        );

    });


    /* Close menu after clicking a link */

    const navLinks =
        navMenu.querySelectorAll("a");


    navLinks.forEach(function (link) {

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

            navMenu.classList.remove("show");

            menuToggle.classList.remove("active");

            menuToggle.setAttribute(
                "aria-expanded",
                "false"
            );

        });

    });

});


/* =========================================================
   COMPLETE BLISS HERO SLIDER
========================================================= */

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

        const slides =
            document.querySelectorAll(
                ".cbx-bg-slide"
            );

        const dots =
            document.querySelectorAll(
                ".cbx-slider-dot"
            );

        const previousButton =
            document.querySelector(
                ".cbx-slider-prev"
            );

        const nextButton =
            document.querySelector(
                ".cbx-slider-next"
            );


        if (!slides.length) {
            return;
        }


        let currentSlide = 0;

        let sliderTimer;


        function showSlide(index) {

            slides.forEach(
                function (slide) {
                    slide.classList.remove(
                        "active"
                    );
                }
            );


            dots.forEach(
                function (dot) {
                    dot.classList.remove(
                        "active"
                    );
                }
            );


            currentSlide =
                (index + slides.length)
                % slides.length;


            slides[
                currentSlide
            ].classList.add(
                "active"
            );


            if (
                dots[currentSlide]
            ) {

                dots[
                    currentSlide
                ].classList.add(
                    "active"
                );

            }

        }


        function nextSlide() {

            showSlide(
                currentSlide + 1
            );

        }


        function previousSlide() {

            showSlide(
                currentSlide - 1
            );

        }


        function startSlider() {

            clearInterval(
                sliderTimer
            );


            sliderTimer =
                setInterval(
                    nextSlide,
                    4000
                );

        }


        if (nextButton) {

            nextButton.addEventListener(
                "click",
                function () {

                    nextSlide();

                    startSlider();

                }
            );

        }


        if (previousButton) {

            previousButton.addEventListener(
                "click",
                function () {

                    previousSlide();

                    startSlider();

                }
            );

        }


        dots.forEach(
            function (dot) {

                dot.addEventListener(
                    "click",
                    function () {

                        const slideIndex =
                            Number(
                                this.dataset.slide
                            );


                        showSlide(
                            slideIndex
                        );


                        startSlider();

                    }
                );

            }
        );


        showSlide(0);

        startSlider();

    }
);

/* =========================================================
   COMPLETE BLISS HOMEPAGE EVIDENCE LIGHTBOX
========================================================= */

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

        const evidenceCards =
            document.querySelectorAll(
                ".cbx-evidence-card"
            );

        const lightbox =
            document.getElementById(
                "cbxEvidenceLightbox"
            );

        const lightboxImage =
            document.getElementById(
                "cbxEvidenceLightboxImage"
            );

        const lightboxTitle =
            document.getElementById(
                "cbxEvidenceLightboxTitle"
            );

        const closeButton =
            document.getElementById(
                "cbxEvidenceClose"
            );


        if (
            !evidenceCards.length ||
            !lightbox ||
            !lightboxImage
        ) {
            return;
        }


        evidenceCards.forEach(
            function (card) {

                card.addEventListener(
                    "click",
                    function () {

                        const image =
                            this.dataset.evidenceImage;

                        const title =
                            this.dataset.evidenceTitle
                            || "Complete Bliss Moment";


                        if (!image) {
                            return;
                        }


                        lightboxImage.src = image;

                        lightboxImage.alt = title;


                        if (lightboxTitle) {

                            lightboxTitle.textContent =
                                title;

                        }


                        lightbox.classList.add(
                            "show"
                        );


                        lightbox.setAttribute(
                            "aria-hidden",
                            "false"
                        );


                        document.body.style.overflow =
                            "hidden";

                    }
                );

            }
        );


        function closeEvidenceLightbox() {

            lightbox.classList.remove(
                "show"
            );


            lightbox.setAttribute(
                "aria-hidden",
                "true"
            );


            document.body.style.overflow =
                "";


            lightboxImage.src = "";

        }


        if (closeButton) {

            closeButton.addEventListener(
                "click",
                closeEvidenceLightbox
            );

        }


        lightbox.addEventListener(
            "click",
            function (event) {

                if (event.target === lightbox) {

                    closeEvidenceLightbox();

                }

            }
        );


        document.addEventListener(
            "keydown",
            function (event) {

                if (
                    event.key === "Escape" &&
                    lightbox.classList.contains(
                        "show"
                    )
                ) {

                    closeEvidenceLightbox();

                }

            }
        );

    }
);

/* =========================================================
   COMPLETE BLISS - FULL GALLERY VIEWER
========================================================= */

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

        const galleryPhotos =
            window.cbxCompleteBlissGallery || [];


        if (!galleryPhotos.length) {
            return;
        }


        const items =
            document.querySelectorAll(
                ".cbx-full-gallery-item"
            );


        const viewer =
            document.getElementById(
                "cbxGalleryViewer"
            );


        const viewerImage =
            document.getElementById(
                "cbxGalleryViewerImage"
            );


        const currentCounter =
            document.getElementById(
                "cbxGalleryCurrent"
            );


        const downloadButton =
            document.getElementById(
                "cbxGalleryDownload"
            );


        const closeButton =
            document.getElementById(
                "cbxGalleryViewerClose"
            );


        const previousButton =
            document.getElementById(
                "cbxGalleryViewerPrev"
            );


        const nextButton =
            document.getElementById(
                "cbxGalleryViewerNext"
            );


        let currentIndex = 0;


        function showPhoto(index) {

            currentIndex =
                (index + galleryPhotos.length)
                % galleryPhotos.length;


            const photo =
                galleryPhotos[currentIndex];


            viewerImage.src =
                photo.path;


            viewerImage.alt =
                "Complete Bliss gallery photo";


            currentCounter.textContent =
                currentIndex + 1;


            downloadButton.href =
                photo.path;


            downloadButton.setAttribute(
                "download",
                photo.filename
            );

        }


        function openViewer(index) {

            showPhoto(index);


            viewer.classList.add(
                "show"
            );


            viewer.setAttribute(
                "aria-hidden",
                "false"
            );


            document.body.style.overflow =
                "hidden";

        }


        function closeViewer() {

            viewer.classList.remove(
                "show"
            );


            viewer.setAttribute(
                "aria-hidden",
                "true"
            );


            document.body.style.overflow =
                "";

        }


        function nextPhoto() {

            showPhoto(
                currentIndex + 1
            );

        }


        function previousPhoto() {

            showPhoto(
                currentIndex - 1
            );

        }


        items.forEach(
            function (item) {

                item.addEventListener(
                    "click",
                    function () {

                        const index =
                            Number(
                                this.dataset.galleryIndex
                            );


                        openViewer(index);

                    }
                );

            }
        );


        closeButton.addEventListener(
            "click",
            closeViewer
        );


        previousButton.addEventListener(
            "click",
            previousPhoto
        );


        nextButton.addEventListener(
            "click",
            nextPhoto
        );


        viewer.addEventListener(
            "click",
            function (event) {

                if (
                    event.target === viewer
                ) {

                    closeViewer();

                }

            }
        );


        document.addEventListener(
            "keydown",
            function (event) {

                if (
                    !viewer.classList.contains(
                        "show"
                    )
                ) {
                    return;
                }


                if (event.key === "Escape") {

                    closeViewer();

                }


                if (event.key === "ArrowRight") {

                    nextPhoto();

                }


                if (event.key === "ArrowLeft") {

                    previousPhoto();

                }

            }
        );

    }
);


/* =========================================================
   COMPLETE BLISS FAQ
========================================================= */

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

        const faqItems =
            document.querySelectorAll(
                ".cbx-faq-item"
            );


        faqItems.forEach(
            function (item) {

                const question =
                    item.querySelector(
                        ".cbx-faq-question"
                    );


                if (!question) {
                    return;
                }


                question.addEventListener(
                    "click",
                    function () {

                        const isOpen =
                            item.classList.contains(
                                "active"
                            );


                        /* CLOSE ALL */

                        faqItems.forEach(
                            function (faq) {

                                faq.classList.remove(
                                    "active"
                                );


                                const button =
                                    faq.querySelector(
                                        ".cbx-faq-question"
                                    );


                                if (button) {

                                    button.setAttribute(
                                        "aria-expanded",
                                        "false"
                                    );

                                }

                            }
                        );


                        /* OPEN SELECTED */

                        if (!isOpen) {

                            item.classList.add(
                                "active"
                            );


                            question.setAttribute(
                                "aria-expanded",
                                "true"
                            );

                        }

                    }
                );

            }
        );

    }
);