(function ($) {
    "use strict";

    //Scroll back to top
    $(document).ready(function () {

        var progressPath = document.querySelector('.progress-wrap path');
        var 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';
        var updateProgress = function () {
            var scroll = $(window).scrollTop();
            var height = $(document).height() - $(window).height();
            var progress = pathLength - (scroll * pathLength / height);
            progressPath.style.strokeDashoffset = progress;
        }
        updateProgress();
        $(window).scroll(updateProgress);
        var offset = 50;
        var duration = 550;
        jQuery(window).on('scroll', function () {
            if (jQuery(this).scrollTop() > offset) {
                jQuery('.progress-wrap').addClass('active-progress');
            } else {
                jQuery('.progress-wrap').removeClass('active-progress');
            }
        });
        jQuery('.progress-wrap').on('click', function (event) {
            event.preventDefault();
            jQuery('html, body').animate({ scrollTop: 0 }, duration);
            return false;
        })

    });

    // Document Ready
    $(document).ready(function () {

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

        // menu_link
        $('.menu_link').on('click', function (event) {
            $("body").removeClass('menu_active');
        });

        // accordion
        // $(function () {
        //     // Keep the first item open initially in each accordion
        //     $(".accordion2").each(function () {
        //         let $firstItem = $(this).find(".accordion_item").first();
        //         $firstItem.addClass("active");
        //         $firstItem.find(".accordion_body").show();
        //     });

        //     $(".accordion_btn").on("click", function () {
        //         let $item = $(this).closest(".accordion_item");
        //         let $accordion = $item.closest(".accordion2"); // scope to current accordion
        //         let $body = $item.find(".accordion_body");

        //         if ($item.hasClass("active")) {
        //             // collapse this one
        //             $body.stop(true, true).slideUp(300);
        //             $item.removeClass("active");
        //         } else {
        //             // close only items inside this accordion
        //             $accordion
        //                 .find(".accordion_item.active")
        //                 .removeClass("active")
        //                 .find(".accordion_body")
        //                 .stop(true, true)
        //                 .slideUp(300);

        //             // expand clicked one
        //             $item.addClass("active");
        //             $body.stop(true, true).slideDown(300);
        //         }
        //     });
        // });

        $(function () {
            // Keep the first item open initially in each accordion
            $(".accordion2 .accordion_item").on("click", function () {
                let $body = $(this).find(".accordion_body");
                $body.toggleSlide();
            });
        });

        // accordion
        $(function () {
            // Keep the first item open initially in each accordion
            $(".accordion").each(function () {
                let $firstItem = $(this).find(".accordion_item").first();
                $firstItem.addClass("active");
                $firstItem.find(".accordion_body").show();
            });

            $(".accordion_btn").on("click", function () {
                let $item = $(this).closest(".accordion_item");
                let $accordion = $item.closest(".accordion"); // scope to current accordion
                let $body = $item.find(".accordion_body");

                if ($item.hasClass("active")) {
                    // collapse this one
                    $body.stop(true, true).slideUp(300);
                    $item.removeClass("active");
                } else {
                    // close only items inside this accordion
                    $accordion
                        .find(".accordion_item.active")
                        .removeClass("active")
                        .find(".accordion_body")
                        .stop(true, true)
                        .slideUp(300);

                    // expand clicked one
                    $item.addClass("active");
                    $body.stop(true, true).slideDown(300);
                }
            });
        });

        // more_less_btn
        $(function () {
            $(".more_less_btn").on("click", function () {
                let $btn = $(this);
                let $box = $btn.closest(".duration_wrap_more_box");
                let $items = $box.find(".duration_wrap_more");

                // Toggle visibility
                $items.toggle();

                // Toggle button state
                $btn.toggleClass("active_less");

                // Update text and icon direction
                if ($btn.hasClass("active_less")) {
                    $btn.find(".less_text").show();
                    $btn.find(".more_text").hide();
                } else {
                    $btn.find(".less_text").hide();
                    $btn.find(".more_text").show();
                }
            });

            // Initial state setup
            $(".less_text").show();
            $(".more_text").hide();
        });

        // site_header
        $(document).ready(function () {
            function toggleSticky() {
                if ($(window).scrollTop() >= 100) {
                    $("body").addClass("sticky_menu");
                } else {
                    $("body").removeClass("sticky_menu");
                }
            }

            // Run once on page load (handles refresh case)
            toggleSticky();

            // Run on scroll
            $(window).on("scroll", function () {
                toggleSticky();
            });
        });

        // menu_link
        $(document).ready(function () {
            let forceActive = false; // Flag to force clicked menu link as active

            // Smooth scroll on menu click
            $(".menu_link").on("click", function (e) {
                e.preventDefault();
                var target = $(this).attr("href");
                var offsetTop = $(target).offset().top;
                var height = $('.site_header').height();

                // Extra offset for specific sections
                if (target === "#Itinerary") offsetTop -= height + 20;
                if (target === "#price_inclusion") offsetTop -= height + 35;
                if (target === "#booking_steps") offsetTop -= height + 35;
                if (target === "#whyiris") offsetTop -= height + 35;
                if (target === "#testimonial") offsetTop -= height + 35;
                if (target === "#faq") offsetTop -= height + 35;


                // Immediately activate clicked menu link
                $(".menu_link").removeClass("active");
                $(this).addClass("active");

                // Set forceActive to prevent scroll from overriding
                forceActive = true;

                $("html, body").animate(
                    { scrollTop: offsetTop },
                    800, // scroll speed
                    function () {
                        // Animation complete, allow normal scroll updates again
                        forceActive = false;
                        updateActiveMenu(); // Ensure final section gets active if needed
                    }
                );

                $("body").removeClass('menu_active');
            });

            function updateActiveMenu() {
                if (forceActive) return; // Do nothing if forced active by click

                var scrollPos = $(window).scrollTop();
                $(".section").each(function () {
                    var top = $(this).offset().top - 100;
                    var bottom = top + $(this).outerHeight();
                    var id = $(this).attr("id");

                    if (scrollPos >= top && scrollPos < bottom) {
                        $(".menu_link").removeClass("active");
                        $('.menu_link[href="#' + id + '"]').addClass("active");
                    }
                });
            }

            // Run on scroll
            $(window).on("scroll", updateActiveMenu);

            // Run once on page load
            updateActiveMenu();
        });

    });

})(jQuery);
