
/*
 * Returns the index of val in the given array, or -1 if val is not in the aray.
 */
function index_of(val, array)
{
	for (var i = 0; i < array.length; ++i)
	{
		if (array[i] == val)
			return i;
	}
	return -1;
}


/*
 * Inserts a <a id="node.textvalue"> node before node.
 */
function insert_anker(node)
{
	// insert anker
	var anker = document.createElement('a');
	var id = node.firstChild.data;
	anker.id = id;
	node.parentNode.insertBefore(anker, node);
}


/*
 * Creates an <li><a href=#node.textvalue>node.textvalue</li> node out of the given node.
 */
function create_link_to_anker(node)
{
// insert link to the anker
	var li_node = document.createElement('li');
	var a_node = document.createElement('a');
	var id = node.firstChild.data;
	a_node.href= "#" + String(id);
	a_node.appendChild(document.createTextNode(id));
	li_node.appendChild(a_node);
	return li_node;
}

function build_content_links(rootId, maxlevel)
{
	var root = document.getElementById(rootId);

	var headers = new Array(maxlevel);
	for (var i = 0; i < headers.length; ++i)
		headers[i] = "h" + String(i+1);

	var currentLevel = 0;
	var root_node = document.createElement('div');
	var insert_point = document.createElement('ul');
	root_node.appendChild(insert_point);

	for (var i = 0; i < root.childNodes.length; ++i)
	{
		n = root.childNodes[i];

		var idx = index_of(n.nodeName.toLowerCase(), headers);
		if (idx != -1)
		{
			insert_anker(n);
			++i; // for the anker we just inserted
			var li_node = create_link_to_anker(n);

			if (idx < currentLevel)
			{
				// go up in the tree to find the right <ul>
				while(idx < currentLevel--)
					insert_point = insert_point.parentNode.parentNode;
			}
			else if (idx > currentLevel)
			{
				// create new nested <il><ul>.. - structures until we reach the right level
				while (idx > currentLevel++)
				{
					var tmp = document.createElement('li');
					var	ul_node = document.createElement('ul');
					tmp.appendChild(ul_node);
					insert_point.appendChild(tmp);
					insert_point = ul_node;
				}
			}
			insert_point.appendChild(li_node);
			currentLevel = idx;
		}
	}

	return root_node;
}

function insert_content_links(rootId, maxlevel)
{
	var top_anker = document.createElement('a');
	top_anker.id = "top";
	document.body.insertBefore(top_anker, document.body.firstChild);

	var	top_link = document.createElement('a');
	top_link.href = "#top";
	top_link.appendChild(document.createTextNode(" ... back to the top"))
	
	var link_list = build_content_links(rootId, maxlevel);
	link_list.insertBefore(top_link, link_list.firstChild);

	var content = document.getElementById(rootId);
	link_list.id = "toc";
	content.parentNode.insertBefore(link_list, content);
}

/*
 * When we create the TOC, the whole document must already have been loaded.
 * Hence, we use the body.onload eventhandler.
 * Also, we set up that event handler only after some time (to make sure
 * the <body>-tag actually exists when we set up the handler).
 */
window.setTimeout("insert_content_links('content', 3)", 1000);
