function sort_table (){
    /* https://stackoverflow.com/questions/3160277/jquery-table-sort */
    const getCellValue = (tr, idx) => tr.children[idx].innerText || tr.children[idx].textContent;

    const comparer = (idx, asc) => (a, b) => ((v1, v2) => 
        v1 !== '' && v2 !== '' && !isNaN(v1) && !isNaN(v2) ? v1 - v2 : v1.toString().localeCompare(v2, undefined, {numeric:true})
    )(getCellValue(asc ? a : b, idx), getCellValue(asc ? b : a, idx));

    // do the work...
    window.addEventListener('load', function() {
        document.querySelectorAll('th').forEach(th => th.addEventListener('click', (() => {
            const table = th.closest('table');
            Array.from(table.querySelectorAll('tr:nth-child(n+2)'))
                .sort(comparer(Array.from(th.parentNode.children).indexOf(th), this.asc = !this.asc))
                .forEach(tr => table.appendChild(tr) );
        })));
    }, false);
}

function toggle_hidden(id) {
    var x = document.getElementById(id);
    if (x.className.indexOf("is-hidden") == -1) {
        /* It is showing, hide it */
        x.className = x.className.replace("is-visible", "is-hidden");
        /* Special case for the sidebar */
        if (id == "sidebar") {
            document.getElementById("main").style.marginLeft = "0px";
        }
    } else {
        /* It's hidden, show it */
        x.className = x.className.replace("is-hidden", "is-visible");
        if (id == "sidebar") {
            document.getElementById("main").style.marginLeft = "200px";
        }
    }
}

/* https://github.com/jloh/bulma-prefers-dark/issues/22#issuecomment-663790065 */
function toggle_darkness() {
    let darkLink = document.getElementById('dark-theme');
    if(darkLink){
        darkLink.remove();
    }else{
        darkLink = document.createElement('link');
        darkLink.crossOrigin = "anonymous";
        darkLink.href = "https://cdnjs.cloudflare.com/ajax/libs/bulma-prefers-dark/0.1.0-beta.1/bulma-prefers-dark.min.css";
        darkLink.id = "dark-theme";
        darkLink.integrity = "sha512-8L9NjgWBr9opkijcN9ZZCzzl7T3hVqji0baeKdTvfq1VN119XV4RNCGGI6vAF8ygQkSK0Qew84toTqqpzmbxUw==";
        darkLink.referrerPolicy="no-referrer";
        darkLink.rel="stylesheet";

        document.head.appendChild(darkLink);
    }
}
