/*
    Automatically generates the outline for the document.
*/

$(function() {
    // Only generate for articles.
    if ($("body").hasClass("wider")) {
        var headings = []
        $("div#content h2").each(function () {
            var html = $(this).html().replace(/<\/?[^>]+>/gi, "");
            var id = "section_" + html.replace(/ /g, "_").replace(/[^a-zA-Z_]/g, "").toLowerCase();
            $(this).attr("id", id);
            headings.push([id, html]);
        });
        // Dont bother for one or less headings.
        if (headings.length > 1) {
            var module = $("<div/>").addClass("module");
            module.append("<h2>Outline</h2>");
            var list = $("<ul/>");
            for (var n = 0; n < headings.length; n++) {
                var heading = headings[n];
                list.append($("<li/>").append($("<a/>").attr("href", "#" + heading[0]).html(heading[1])));
            }
            module.append(list);
            // Insert into the document.
            $("div#content-tools").prepend(module);
        }
    }
});