function change_path() {
    const select_lang = document.getElementById("language");
    const lang = select_lang.value;
    const fileName = location.href.split("/").slice(-1);
    let link = "";

    if (location.protocol === "file:") {
        // Cas local
        // Exemple : file:///C:/tranquilit/doc-samba4/build/en/doc/index.html
        // → file:///C:/tranquilit/doc-samba4/build/fr/doc/index.html
        link = location.href.replace(/\/(en|fr)\/doc\//, `/${lang}/doc/`);
    } 
    else if (location.hostname === "doc.ad.tranquil.it") {
        // Cas documentation interne
        // Exemple : https://doc.ad.tranquil.it/samba/en/doc/index.html
        // → https://doc.ad.tranquil.it/samba/fr/doc/index.html
        link = location.href.replace(/\/(en|fr)\/doc\//, `/${lang}/doc/`);
    } 
    else if (location.hostname === "samba.tranquil.it") {
        // Cas documentation publique
        // Exemple : https://samba.tranquil.it/doc/en/index.html
        // → https://samba.tranquil.it/doc/fr/index.html
        link = location.href.replace(/\/(en|fr)\//, `/${lang}/`);
    }

    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;
    let link = "";

    if (location.protocol === "file:") {
        link = location.href
            .replace(/\/(en|fr)\/doc\//, `/${lang}/doc/`)
            .replace(/\/[^\/]+$/, `/${page}`);
    } 
    else if (location.hostname === "doc.ad.tranquil.it") {
        link = location.href.replace(/\/(en|fr)\/doc\/[^\/]+$/, `/${lang}/doc/${page}`);
    } 
    else if (location.hostname === "samba.tranquil.it") {
        link = location.href.replace(/\/(en|fr)\/[^\/]+$/, `/${lang}/${page}`);
    }

    window.open(link, "_self");
}


$(document).ready(function() {
    const hostname = location.hostname;
    const pathParts = location.href.split('/');
    let current_lang = "";
    let current_page = location.href.split("/").slice(-1)[0] || "index.html";

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

    if (select_page) select_page.value = current_page;

    if (location.protocol === "file:") {
        // Cas local : file:///C:/tranquilit/doc-samba4/build/en/doc/index.html
        current_lang = pathParts.includes("fr") ? "fr" : "en";
    } 
    else if (hostname === "doc.ad.tranquil.it") {
        // Cas interne : https://doc.ad.tranquil.it/samba/en/doc/index.html
        current_lang = pathParts.includes("fr") ? "fr" : "en";
    } 
    else if (hostname === "samba.tranquil.it") {
        // Cas internet : https://samba.tranquil.it/doc/en/index.html
        current_lang = pathParts.includes("fr") ? "fr" : "en";
    }

    // Sélectionne la langue actuelle dans le combobox
    if (select_lang) select_lang.value = current_lang;
});
