function change_path(){
    const select_lang = document.getElementById("language");
    const lang = select_lang.value;
    const select_version = document.getElementById("version");
    const version = select_version.value;
    const fileName = location.href.split("/").slice(-1);
    let link =`../../${lang}/${version}/${fileName}`
    window.open(link,"_self");
}


function change_page(){
    const select_lang = document.getElementById("language");
    const lang = select_lang.value;
    const select_page = document.getElementById("select-page-mobile");
    const page = select_page.value;
    const select_version = document.getElementById("version");
    const version = select_version.value;
    let link =`../../${lang}/${version}/${page}`
    window.open(link,"_self");
}


$(document).ready(function() {
    const interne = location.href.split('/')[3];

    let current_lang = "";
    let current_version = "";
    let current_page = (["wapt-create-packages-setuphelpers.html", "wapt-changelog.html", "quickstart-wapt-install-server.html", "index.html"].includes(String(location.href.split("/").slice(-1))) ? String(location.href.split("/").slice(-1)) : "index.html");

    let select_page = document.getElementById("select-page-mobile");
    let select_lang = document.getElementById("language");
    let select_version = document.getElementById("version");

    select_page.value = current_page;

    if (interne == "wapt"){ //interne doc
        current_lang = location.href.split("/wapt/")[1].split('/')[0];
        current_version = location.href.split('/')[5];
        select_lang.value = current_lang;
        select_version.value = (current_version == "doc" ? select_version.options[0].value : current_version); //get value of the first element of the select box (conf.py, ex : line 81, <option value="doc-2.6" selected>2.6</option>)
    }

    else {
        select_lang.value = interne; //www.wapt.fr
        current_version = location.href.split('/')[4];
        select_version.value = (current_version == "doc" ? select_version.options[0].value : current_version);
    }
});

// Empeche la liste de gauche de remonter au plus haut lors de la navigation de page
document.addEventListener('DOMContentLoaded', function() {
    // Find the current/active page in the sidebar
    const activeLink = document.querySelector('.sidebar-tree .current-page');
    
    if (activeLink) {
        // Get the sidebar scroll container
        const sidebar = document.querySelector('.sidebar-scroll');
        
        if (sidebar) {
            // Temporarily disable smooth scrolling
            const originalBehavior = sidebar.style.scrollBehavior;
            sidebar.style.scrollBehavior = 'auto';
            
            // Calculate cumulative offset for nested elements
            let linkOffsetTop = 0;
            let element = activeLink;
            
            while (element && element !== sidebar) {
                linkOffsetTop += element.offsetTop;
                element = element.offsetParent;
            }
            
            const sidebarHeight = sidebar.clientHeight;
            const linkHeight = activeLink.clientHeight;
            
            // Center the active link in the sidebar
            const scrollTo = linkOffsetTop - (sidebarHeight / 2) + (linkHeight / 2);
            
            // Set scroll position directly
            sidebar.scrollTop = scrollTo;
            
            // Restore original scroll behavior
            sidebar.style.scrollBehavior = originalBehavior;
        }
    }
});