// site-2.js

function activateCurrentPageNavLink() {
    // Activate nav-link that matches current path
    const currentPath = window.location.pathname.replace(/\/+$/, "") || "/";

    $(".navbar-nav .nav-link").each(function () {
        const href = $(this).attr("href");
        if (!href) return;

        const linkPath = new URL(href, window.location.origin).pathname.replace(/\/+$/, "") || "/";
        const isActive = linkPath === currentPath;

        $(this).toggleClass("active", isActive);
        if (isActive) {
            $(this).attr("aria-current", "page");
        } else {
            $(this).removeAttr("aria-current");
        }
    });
}

function loadLastAccordionState() {
    const pageId = window.location.pathname.replace('/', '');
    if (!pageId) { return; }

    const lastState = localStorage.getItem(`lastAccordionState-${pageId}`);
    if (lastState) {
        const accordion = document.getElementById("accordionExample");
        const collapseElement = document.getElementById(lastState);
        if (accordion && collapseElement) {
            const bsCollapse = new bootstrap.Collapse(collapseElement, { toggle: true});
        }
    }
}

$(async () => {
    $("#copyright-year").text(new Date().getFullYear());
    activateCurrentPageNavLink();
});
