/* Author: Mihai Bazon, September 2002
 * http://students.infoiasi.ro/~mishoo
 *
 * Table Of Content generator
 * Version: 0.4
 *
 * Feel free to use this script under the terms of the GNU General Public
 * License, as long as you do not remove or alter this notice.
 */
function H_getText(el) {
    var text="";
    for(var i = el.firstChild; i != null; i = i.nextSibling) {
        if(i.nodeType == 3) {
            text += i.data;
        }
        else if(i.firstChild != null) {
            text += H_getText(i);
        }
    }return text;
};
function getHeadlines(el) {
    var l = new Array;
    var rx = /[hH]([1-6])/;
    var rec = function(el) {
        for(var i = el.firstChild; i != null; i = i.nextSibling) {
            if(i.nodeType == 1) {
                if(rx.exec(i.tagName) && !/ignore/.test(i.className)) {
                    l[l.length] = {
                        element : i,
                        text : H_getText(i),
                        level : parseInt(RegExp.$1)
                    };
                }
                rec(i);
            }
        }
    };
    rec(el);
    return l;
};
function generate_TOC(parent) {
    var prefix = "";
    var parent = document.getElementById(parent);
    if(parent.hasChildNodes() && parent.firstChild.nodeType == 8) {
        params = parent.firstChild.data;
        var rx = /base:(.*?)\s/;
        if(rx.exec(params))
            prefix = RegExp.$1;
    }
    var hs = getHeadlines(document.getElementsByTagName("body")[0]);
    for(var i=1; i < hs.length; ++i) {
        var hi = hs[i];
        var el = hi.element;
        if(hi.level <= 5) {
            var div = document.createElement("div");
            div.className = "scrollTop";
            div.innerHTML = "<img src='/static/up.gif' alt='^ TOP ^' />";//"^ TOP ^";
            el.insertBefore(div, el.firstChild);
            div.className += " pointer";
            div.title = "Go to the top of the page";
            div.onclick = function() {
                scrollTo(0,0);
            };
            var oldColor;
            div.onmouseover = function() {
                oldColor = this.style.color;
                this.style.color = "#f84";
            };
            div.onmouseout = function() {
                this.style.color = oldColor;
            };
        }
        var d = document.createElement("div");
        if(el.id == "") el.id = "gen"+i;
        var a = document.createElement("a");
        a.href = prefix+"#"+el.id;
        a.appendChild(document.createTextNode(hi.text));
        d.appendChild(a);
        d.className = "level"+hi.level;
        parent.appendChild(d);
    }
};
