function generate_toc() {
	// Get the headings
	headings =  $$('#content h2');
	if (headings.length < 2) return false;

	// Setup the div#toc	
	$$('#content .primary dl')[0].insert({after: toc = new Element('div', { id: "toc" })})
	
	// Setup the div#toc h3 & div#toc ul
	toc.insert(new Element('h3').update("Table of Contents"))
	toc.insert(toc_ul = new Element('ul'))
		
	headings.each(function(heading) {
		// Get the existing heading id or setup a new one based on the heading text
		var heading_id = heading.id || heading.innerHTML.stripTags().toLowerCase().replace(/\W/g,'')
		// Set the heading id to heading_id variable
		heading.id = heading_id
		// Add a li element with anchor link to the heading
		toc_ul.insert(new Element('li', { className: heading.nodeName }).update("<a href='#"+heading_id+"'>"+heading.innerHTML.stripTags()+"</a>"))
	});
}

Event.observe(window, "load", generate_toc);
