/* ─── Global Pricing Toggle ──────────────────────────── */
window.switchPricing = function (type) {
    if (type === 'bulk') {
        $('#toggleBulk').addClass('active');
        $('#toggleStandard').removeClass('active');
        $('#panel-bulk').addClass('active');
        $('#panel-standard').removeClass('active');
    } else {
        $('#toggleStandard').addClass('active');
        $('#toggleBulk').removeClass('active');
        $('#panel-standard').addClass('active');
        $('#panel-bulk').removeClass('active');
    }
};

$(function () {

    /* ─── Lenis Smooth Scroll ─────────────────────────────── */
    let lenis;
    if (typeof Lenis !== 'undefined') {
        lenis = new Lenis({
            duration: 1.2,
            easing: t => Math.min(1, 1.001 - Math.pow(2, -10 * t)),
            direction: 'vertical',
            smooth: true,
        });
        function raf(time) { lenis.raf(time); requestAnimationFrame(raf); }
        requestAnimationFrame(raf);
    }

    /* ─── Anchor links via Lenis ──────────────────────────── */
    $('a[href^="#"]').on('click', function (e) {
        const target = $(this).attr('href');
        if (target === '#') return;
        const $t = $(target);
        if ($t.length) {
            e.preventDefault();
            if (lenis) {
                lenis.scrollTo($t[0], { offset: -72, duration: 1.4 });
            } else {
                $('html, body').animate({ scrollTop: $t.offset().top - 72 }, 800);
            }
            // Close mobile drawer if open
            $('#mobileDrawer').removeClass('open');
            $('#menuToggle').removeClass('active');
        }
    });

    /* ─── Preloader ───────────────────────────────────────── */
    $(window).on('load', function () {
        setTimeout(function () {
            $('#preloader').addClass('hidden');
        }, 1800);
    });

    /* ─── Navbar scroll behavior ──────────────────────────── */
    $(window).on('scroll', function () {
        if ($(this).scrollTop() > 60) {
            $('#mainNav').addClass('scrolled');
        } else {
            $('#mainNav').removeClass('scrolled');
        }
    });

    /* ─── Mobile Menu Toggle ──────────────────────────────── */
    $('#menuToggle').on('click', function () {
        $(this).toggleClass('active');
        $('#mobileDrawer').toggleClass('open');
        $('body').toggleClass('overflow-hidden');
    });
    $('.mob-nav-links a').on('click', function () {
        $('#mobileDrawer').removeClass('open');
        $('#menuToggle').removeClass('active');
        $('body').removeClass('overflow-hidden');
    });

    /* ─── Go to Top Button ────────────────────────────────── */
    if (lenis) {
        lenis.on('scroll', function ({ scroll }) {
            if (scroll > 400) {
                $('#go-top').addClass('visible');
            } else {
                $('#go-top').removeClass('visible');
            }
        });
        $('#go-top').on('click', function () {
            lenis.scrollTo(0, { duration: 1.6 });
        });
    } else {
        $(window).on('scroll', function () {
            if ($(this).scrollTop() > 400) {
                $('#go-top').addClass('visible');
            } else {
                $('#go-top').removeClass('visible');
            }
        });
        $('#go-top').on('click', function () {
            $('html, body').animate({ scrollTop: 0 }, 800);
        });
    }

    /* ─── Reveal on Scroll (Intersection Observer) ────────── */
    const revealObserver = new IntersectionObserver(function (entries) {
        entries.forEach(function (entry) {
            if (entry.isIntersecting) {
                entry.target.classList.add('visible');
                revealObserver.unobserve(entry.target);
            }
        });
    }, { threshold: 0.12, rootMargin: '0px 0px -40px 0px' });

    document.querySelectorAll('.reveal').forEach(function (el) {
        revealObserver.observe(el);
    });


    /* ─── Navbar active link highlight ───────────────────── */
    const $navLinks = $('.nav-links a[href^="#"]');
    const sectionIds = $navLinks
        .map(function () { return $(this).attr('href').replace('#', ''); })
        .get()
        .filter(Boolean);

    function updateActiveLink(scrollY) {
        const firstId = sectionIds[0];
        const firstEl = firstId ? document.getElementById(firstId) : null;
        let current = '';

        if (firstEl && scrollY >= firstEl.offsetTop - 120) {
            current = firstId;
            sectionIds.forEach(function (id) {
                const el = document.getElementById(id);
                if (el && el.offsetTop - 120 <= scrollY) current = id;
            });
        }

        $navLinks.each(function () {
            $(this).toggleClass('active-link', $(this).attr('href') === '#' + current);
        });
    }

    if (lenis) {
        lenis.on('scroll', function ({ scroll }) {
            updateActiveLink(scroll);
        });
        updateActiveLink(lenis.scroll || window.scrollY || 0);
    } else {
        $(window).on('scroll', function () {
            updateActiveLink($(window).scrollTop());
        });
        updateActiveLink($(window).scrollTop());
    }

    /* ─── Table of contents scrollspy (pirmas-optionas) ───── */
    const $tocLinks = $('.table-contents ol a[href^="#"]');
    if ($tocLinks.length) {
        const tocIds = $tocLinks
            .map(function () { return $(this).attr('href').replace('#', ''); })
            .get()
            .filter(Boolean);

        function updateTocActive(scrollY) {
            if (!tocIds.length) return;
            let current = tocIds[0];
            tocIds.forEach(function (id) {
                const el = document.getElementById(id);
                if (el && el.offsetTop - 140 <= scrollY) current = id;
            });
            $tocLinks.each(function () {
                $(this).toggleClass('active-li', $(this).attr('href') === '#' + current);
            });
        }

        if (lenis) {
            lenis.on('scroll', function ({ scroll }) {
                updateTocActive(scroll);
            });
            updateTocActive(lenis.scroll || window.scrollY || 0);
        } else {
            $(window).on('scroll', function () {
                updateTocActive($(window).scrollTop());
            });
            updateTocActive($(window).scrollTop());
        }
    }


    // marquee js
    $('.marquee').marquee({
        duration: 20000,
        duplicated: true,
        pauseOnHover: true,
        gap: 0,
        startVisible: true,
        delayBeforeStart: 0,
    });


    $(document).ready(function () {
        $('.custom-accordion .acc-header').on('click', function () {
            let parent = $(this).closest('.custom-accordion');
            let item = $(this).parent();
            let body = $(this).next();

            if (item.hasClass('active')) {
                item.removeClass('active');
                body.stop().slideUp();
            } else {
                parent.find('.acc-item').removeClass('active');
                parent.find('.acc-body').stop().slideUp();

                item.addClass('active');
                body.stop().slideDown();
            }
        });

        // Show default active
        $('.custom-accordion .acc-item.active .acc-body').show();
    });


    $(function () {

        const $section = $('#hiw');
        const $steps = $('.hiw-step');
        const $mockups = $('.mockup');
        let current = 0;
        let scrollTimer = null;

        /* ── Activate a step by index ────────────────── */
        function goTo(idx) {
            if (idx === current) return;

            // Mark leaving mockup so it exits left
            $mockups.eq(current).removeClass('active').addClass('leave');

            // Clean leave class after transition
            const leaving = current;
            setTimeout(function () {
                $mockups.eq(leaving).removeClass('leave');
            }, 560);

            current = idx;

            $steps.removeClass('active');
            $steps.eq(idx).addClass('active');

            // Slight delay so leave animation starts first
            setTimeout(function () {
                $mockups.eq(idx).addClass('active');
            }, 60);
        }

        /* ── Click handler ─────────────────────────── */
        $steps.on('click', function () {
            const idx = parseInt($(this).data('step'));

            goTo(idx); // always activate step

            // Only scroll on desktop
            if ($(window).width() >= 992) {
                const sTop = $section.offset().top;
                const sH = $section.outerHeight();
                const wH = $(window).height();
                const range = sH - wH;
                const target = sTop + range * (idx / 3);

                $('html, body').animate({ scrollTop: target }, 600, 'swing');
            }
        });

        /* ── Scroll handler (desktop only) ────────── */
        $(window).on('scroll.hiw', function () {
            if ($(window).width() < 992) return; // mobile: ignore scroll

            if (scrollTimer) clearTimeout(scrollTimer);
            scrollTimer = setTimeout(function () {
                const sTop = $section.offset().top;
                const sH = $section.outerHeight();
                const wH = $(window).height();
                const scrollY = $(window).scrollTop();
                const progress = (scrollY - sTop) / (sH - wH);

                let idx;
                if (progress <= 0) idx = 0;
                else if (progress >= 1) idx = 3;
                else idx = Math.min(3, Math.floor(progress * 4));

                goTo(idx);
            }, 10);
        });

        /* ── Init ────────────────────────────────────── */
        $(window).trigger('scroll.hiw');

        /* ── Optional: Re-trigger on resize ─────────── */
        $(window).on('resize', function () {
            if ($(window).width() >= 992) {
                $(window).trigger('scroll.hiw');
            }
        });

    });


    $(document).ready(function () {

        const $fillterBtn = $('.fillter-btn');
        const $fillteredText = $('.filltered-text');
        const $filterBtn = $('.filter-btns button');
        const $filterControls = $('.filter-controls');

        // Toggle dropdown
        $fillterBtn.on('click', function () {
            $filterControls.toggleClass('active');
        });

        // Handle button click
        $filterBtn.on('click', function () {
            $fillteredText.text($(this).text());
            $filterControls.removeClass('active');
        });

    });


    // updateScrollProgress
    function updateScrollProgress() {
        let scrollTop = $(window).scrollTop();
        let docHeight = $(document).height() - $(window).height();
        let scrollPercent = (scrollTop / docHeight) * 100;

        $('.scroll-progress').css('width', scrollPercent + '%');
    }

    $(window).on('scroll', updateScrollProgress);


    // AOS
    AOS.init({
        duration: 1000,        // animation time (ms)
        easing: 'ease-out-cubic', // smoother easing
        once: true,            // animate only once (prevents jitter)
        mirror: false,         // no reverse animation on scroll up
        offset: 120,           // trigger a bit earlier
        delay: 100,            // slight delay for smoothness
    });

}); // end document ready
