
function first_basename() {
    let first = K.get(".navlink");
    return first ? K.attr(first, "data-page") : null;
}

function scroll_to_target(target_name) {
    var text = K.get("#text");
    if (!text) return;
    var folder_top = text.getBoundingClientRect().top;
    var target = K.get(target_name);
    if (target) {
        var target_top = target.getBoundingClientRect().top;
        var scroll_position = text.scrollTop + target_top - folder_top;
        text.scrollTop = scroll_position;
    }
    highlight_visible_toc_sections();
}

function modify_links() {
    var links = K.getv("#text a");
    links.forEach(function(link) {
        if (link.classList.contains("linkd") || link.querySelector("img")) {
            return;
        }
        var href = link.getAttribute("href");
        if (!href) return;
        if (href.indexOf("http://") != -1 ||
            href.indexOf("https://") != -1 ||
            href.indexOf(".pdf") != -1 ||
            href.indexOf("mailto:") != -1 ||
            href.indexOf("/") == 0 ||
            href.indexOf("#") != -1 ||
            href.indexOf("..") == 0) {
            return;
        }
        var parts = href.split('#');
        var len = parts[0].length;
        var page = parts[0].substring(0, len - 5);
        var target = "#" + parts[1];
        var span = document.createElement("span");
        span.className = "panelink";
        span.setAttribute("data-page", page);
        span.setAttribute("data-target", target);
        span.setAttribute("data-title", link.textContent);
        span.textContent = link.textContent;
        link.parentNode.replaceChild(span, link);
    });
}


function show_text() {
    K.visible("#search_page", "hide");
    K.visible("#help_page", "hide");
    K.visible("#text", "show");
    K.style("#content", "background-color", "white");
    K.text("#help", "Help");
    highlight_visible_toc_sections();
}


function new_page_callback() {
    show_text();
    modify_links();
    resize();
    current_page_callback();
}

var image_timer;
var last_basename;

function current_page_callback() {
    if (target_name == "#") {
        return;
    }
    var selector = '.navlink[data-target="' + (target_name.split("#")[1] || "") + '"]';
    var navlink = K.get(selector);
    if (navlink) {
        update_page_title(navlink);
    }
    resize();
    scroll_to_target(target_name);
}


async function load_page_content(basename, new_page, callback) {
    if (true || basename != current_basename()) {
        var url = "pages/" + basename + ".html";
        await K.load("#text", url);
        callback();
    }
    else {
        callback();
    }
}


function load_page(basename, target, callback, push_state) {
    var init_complete = false;
    var push = typeof push_state !== "undefined";
    var state, state_target;

    if (init_complete) {
        var parts = window.location.hash.toString().split("#");
        state = {"basename" : parts[1], "target" : "#" + parts[2]};
        state_target = "#" + basename + target;
        if (push) {
            history.pushState(state, "", state_target);
        } else {
            history.replaceState(state, "", state_target);
        }
        load_page_content(
            basename, true,
            function() {
                target_name = target;
                initialize = false;
                callback(); } );

    } else {
        basename = first_basename();
        state = {"basename" : basename, "target" : "#"};
        state_target = "#" + basename + "#";

        init_complete = true;

        load_page_content(
            basename, true,
            function() {
                target_name = target;
                initialize = true;
                callback(); } );

        history.replaceState(state, "", state_target);
    }
}
