$.fn.textNodes = function() {
    var ret = [];
    this.contents().each( function() {
        var fn = arguments.callee;
        if ( this.nodeType == 3) 
            ret.push( this );
        else $(this).contents().each(fn);
    });
    return $(ret);
}

function toggleDT(dt) {
    var dd = dt.next("dd").first();
    dt.toggleClass("selected");
    dd.toggle('fast');
}


function rolloutIfDL(el) {
    el.closest("dt").first().each(function(i,p) {
        if(p.nodeName=="DT") {
            toggleDT($(p));
        }
        else {
            toggleDT($(p).prev("dt").first());
        }
    });
};
$(function() {
    $(".nav-item ul").hide();
    $(".nav-item").hover(function() {
        $(this).find("ul").show();
    },function() {
        $(this).find("ul").hide();
    });
    $("dt").click(function(){
        toggleDT($(this));
    });
    // highlight searched words
    if (document.referrer) {
        var m = document.referrer.match(/search\?query=(.+)/);
        if (m) {
            var words = new RegExp(decodeURI(m[1]).replace(/\W+/," ").replace(/\s+/,"|"),"ig");
            var firstMatch = true;
            $("#content").textNodes().each(function (i,node) {
                var wm;
                var lastIndex = 0;
                var replaceWith = document.createDocumentFragment();
                while ((wm = words.exec(node.textContent)) != null) {
                    var word = wm[0];
                    if (lastIndex < wm.index) {
                        replaceWith.appendChild(
                            document.createTextNode(
                                node.textContent.substring(lastIndex,wm.index)));
                    }
                    var sp = document.createElement("span");
                    sp.setAttribute("class","searched-word");
                    sp.appendChild(
                        document.createTextNode(
                            word));
                    replaceWith.appendChild(sp);
                    lastIndex = wm.index + word.length;
                }
                if (lastIndex != 0) {
                    if (lastIndex < node.textContent.length) {
                        replaceWith.appendChild(
                            document.createTextNode(
                                node.textContent.substring(lastIndex)));
                    }
                    var p = node.parentNode;
                    p.replaceChild(
                        replaceWith,node);
                    rolloutIfDL($(p));
                    if (firstMatch) {
                        if (p.scrollIntoView) p.scrollIntoView(true);
                        firstMatch = false;
                    }
                }
            });
        }
    }
    
});

function update_show_or_hide_sections() {
    $(".hide .show-or-hide-section").hide();
    $(".show .show-or-hide-section").show();
    $(".hide button.toggle .show-text").show();
    $(".hide button.toggle .hide-text").hide();
    $(".hide button.toggle, .show button.toggle").show();
}

$(document).ready(function() {

    setTimeout(update_show_or_hide_sections,100);
    $("button.toggle").click(function() {
        if ($(this.parentNode).hasClass("show")) {
            $(this.parentNode).removeClass("show");
            $(this.parentNode).addClass("hide");
        }
        else {
            $(this.parentNode).removeClass("hide");
            $(this.parentNode).addClass("show");
        }
        update_show_or_hide_sections();
        return false;
    });
});
