document.addEventListener('DOMContentLoaded', function() {

    function calculateSize(size) {
        var sufixes = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'EiB'];
        var q = 0;

        while (size / 1024 > 1) {
            size = size / 1024;
            q++;
        }

        return (Math.round(size * 100) / 100) + ' ' + sufixes[q];
    }

    if (window.location.pathname == '/') {
        document.querySelector('.directory.go-up').style.display = 'none';
    }

    var path = window.location.pathname.split('/');
    var nav = document.querySelector("nav#breadcrumbs ul");
    var pathSoFar = '';

    for (var i = 1; i < path.length - 1; i++) {
        pathSoFar += '/' + decodeURI(path[i]);
        nav.innerHTML += '<li><a href="' + encodeURI(pathSoFar) + '">' + decodeURI(path[i]) + '</a></li>';
    }

    var mtimes = document.querySelectorAll("table#contents td.mtime a");

    for (var i = 0; i < mtimes.length; i++) {
        var mtime = mtimes[i].textContent;
        if (mtime) {
            var d = new Date(mtime);
            mtimes[i].textContent = d.toLocaleString(navigator.language);
        }
    }

    var sizes = document.querySelectorAll("table#contents td.size a");

    for (var i = 1; i < sizes.length; i++) {
        var size = sizes[i].textContent.slice(0, -2);
        if (size) {
            sizes[i].textContent = calculateSize(parseInt(size));
        }
    }

}, false);

