
function getTableOfContent(depth) {
	
	var titles = [ 'H2', 'H3', 'H4', 'H5', 'H6' ];
	var n;
	var toc;
	var t;

	if (isUndefinedOrNull(depth))
		depth = 2;

	toc = [];

	forEach($("content").childNodes,
		function(e) {
		        n = findValue(titles, e.tagName, 0, depth);
			if (n != -1) {
				if(e.title) {
					t = e.title;
				} else {
					t = scrapeText(e);
				}
				toc.push({"depth":n, "title":t, "node":e });
			}
	});
	
	return toc;
}

function createTocNode(toc) {
	var n;
	var a;

	n = [];
	forEach(toc, function(entry) {
		a = getElementsByTagAndClassName('A', null, entry.node)[0];
		if(a) {
			n.push(DIV({"class":"toc-entry-"+entry.depth},
			   A({'href':'#' + a.name}, "- ", entry.title)));
		}
		else {
			n.push(DIV({"class":"toc-entry-"+entry.depth},
				   "- ", entry.title))
		}
	});

	return DIV({"id":"toc-slot"}, n);
}

function addToc() {
	swapDOM($("toc-slot"), createTocNode(getTableOfContent()));
}
addLoadEvent(addToc);

