/*

function:	buildTOC(context, div)
author:		Miroslav Wiesner
date:		April 26, 2006

This function loops through a set context in the page, 
finds the first H2 and make it the title of the TOC. Then,
it traverses the rest of the container and finds all of the H3 tags,
creates and uses innerHTML to generate an unodered list of the H3 tags
with corresponding anchors.

*/

function buildTOC(context, div) {
	var i, x	= 0;
	var prefix 	= "header";
	var delim	= " | ";
	var div 	= $(div);
	var context	= $(context);
	var title 	= context.getElementsByTagName("h2")[0].innerHTML;
	var headers	= context.getElementsByTagName("h4");
	var code	= "<ul>\n";
	for (i = 0; i < headers.length; i++) {
		var header = headers[i];
		var n	= headers.length - 1;
		if ( i == n ) {
			delim	= "";
		}
		code 	+= "<li><a href=\"#" + prefix + x + "\">" + header.innerHTML + "</a>" + delim + "</li>\n";
		x++;
	}
	code 		+= "</ul>";
	createAnchors(context, headers, prefix);
	div.innerHTML = code;
}

/*

function:	createAnchors(context, headers, prefix)
author:		Miroslav Wiesner
date:		April 26, 2006

This function loops through a set context in the page, 
finds all of the H3 tags, wraps them in anchor tags and uses innerHTML
to generate the new code.

*/

function createAnchors(context, headers, prefix) {
	var x = 0;
	for (var i = 0; i < headers.length; i++) {
		var header = headers[i];
		header.innerHTML = "<a name=\"" + prefix + x + "\"></a>" + header.innerHTML;
		x++;
	}
}
