/*
Enable the "Next" and "Previous" buttons to move through the table of
contents, making entries visible if necessary.
*/

function first_highlighted_navlink() {
    let highlighted_titles = [...K.getv(".section-title.highlight")];
    if (highlighted_titles.length === 0) {
        return null;
    }
    return highlighted_titles[0].parentNode;
}

function make_navlink_visible(navlink) {
    const elts = [...K.getv(".navlink, .caret")];
    let i;
    for (i = elts.indexOf(navlink); i >= 0; i -= 1) {
        let elt = elts[i];
        if (K.has_class(elt, "caret")) {
            if (!K.has_class(elt, "caret-down")) {
                toggle_caret(elt);
            }
            if (K.attr(elt, "data-level") === "0") {
                break;
            }
        }
    }
}

function show_button_status(id, active_p) {
    //const inactive_color = "rgb(60%,60%,60%)";
    //const inactive_color = "rgb(20%,20%,20%)";
    const inactive_color = "transparent";
    //const text_color = "white";
    const text_color = "black";

    const node = K.get(id);
    if (active_p) {
        K.style(node, "color", text_color);
        //K.style(node, "color", "white");
        K.style(node, "text-decoration", "none");
        //K.style(node, "font-style", "normal");
    } else {
        K.style(node, "color", inactive_color);
        K.style(node, "text-decoration", "none");
        //K.style(node, "font-style", "italic");
    }
}

function set_next_last_button_status() {
    const navlinks = [...K.getv(".navlink")];
    const top = headers_in_text()[0][0];
    let status;
    if (!top) {
        status = [true, false];
    }
    else if (top.id === K.attr(navlinks[0], "data-target")) {
        status = [true, false];
    } else if (top.id === K.attr(navlinks[navlinks.length-1], "data-target")) {
        status = [false, true];
    } else {
        status = [true, true];
    }
    show_button_status("#next", status[0]);
    show_button_status("#previous", status[1]);
}

function next_navlink() {
    let navlinks = [...K.getv(".navlink")];
    let index = navlinks.indexOf(first_highlighted_navlink());
    if (index < navlinks.length - 1) {
        let next = navlinks[index + 1];
        next.children[0].children[0].click();
        //make_navlink_visible(next);
    }
    return index + 1;
}

function previous_navlink() {
    let navlinks = [...K.getv(".navlink")];
    let index = navlinks.indexOf(first_highlighted_navlink());
    if (index > 0) {
        let previous = navlinks[index - 1];
        previous.children[0].children[0].click();
        //make_navlink_visible(previous);
    }
    return index - 1;
}

window.addEventListener("load", function (event) {

    K.get("#next").addEventListener("click", function (event) {
        // hide_help();
        const index = next_navlink();
        resize_toc();
    });
    K.get("#previous").addEventListener("click", function (event) {
        // hide_help();
        const index = previous_navlink();
        resize_toc();
    });
    define_keypress("N", "#next");
    define_keypress("P", "#previous");
    set_next_last_button_status();
});
