$(function(){
    const MARGIN = 100;
    const get_jq = (elem, index) => $($(elem).get(index));
    const do_jump = (fwd = true) => {
        if(! found || total < 1){
            return;
        }
        if(fwd){
            if(cindex < 0 || cindex >= total - 1){
                cindex = 0;
            }else{
                cindex = cindex + 1;
            }
        }else{
            if(cindex < 0){
                cindex = 0;
            }else if(cindex == 0){
                cindex = total - 1;
            }else{
                cindex = cindex - 1;
            }
        }
        s_current.text(cindex + 1);
        const curr = get_jq(found, cindex);
        if(pindex >= 0){
            get_jq(found, pindex).removeClass("found-current");
        }
        curr.addClass("found-current");
        const pos = curr.offset();
        if(pos){
            window.scrollTo(0, pos.top > MARGIN ? pos.top - MARGIN : 0);
        }
        pindex = cindex;
    }; 
    const go_next = () => do_jump(true);
    const go_prev = () => do_jump(false);
    const post_mark = () => {
        found = $(".found");
        if(found){
            total = found.length;
            if(total >= 1){
                s_total.text(total);
                $("span#close").click(() => {
                    target.unmark();
                    navbox.hide();
                });
                if(total > 1){
                    $("span#next").click(go_next);
                    $("span#prev").click(go_prev);
                }else{
                    $("span#next").addClass("disabled");
                    $("span#prev").addClass("disabled");
                }
                navbox.show();
                go_next();
            }
        }
    };
    const do_mark = (jq, query, case_sensitive, callback) => {
        try{
            jq.mark(query, {
                separateWordSearch: false,
                acrossElements: true,
                caseSensitive: case_sensitive,
                className: "found",
                exclude: [".nosearch", ".nosearch *", ".find-nav-box", ".find-nav-box *"],
                done: callback
            });
        }catch(e){
            console.error(e);
        }
    };
    const params = new URLSearchParams(window.location.search);
    const search = params.get("search") === "true" ? true : false;
    const query = (() => {
        let q = sessionStorage.getItem("query");
        if(typeof q !== "string") return null;
        q = q.trim();
        if(q.length === 0) return null;
        if(q.length > 100) return null;
        return q;
    })();
    const case_sensitive = sessionStorage.getItem("casesensitive") === "true" ? true : false;
    const s_current = $("span#current");
    const s_total = $("span#total");
    const target = $("body");
    const navbox = $("div.find-nav-box");
    let found;
    let pindex = -1;
    let cindex = -1;
    let total = 0;
    if(search && query){
        do_mark(target, query, case_sensitive, post_mark);
    }
});