$(function(){
	if($('.notoc').length < 1) {
		// adds 'go to top' link to each section
		$('#main_content').append('<div class="gototop"><a  href="#top">[Go to top]<\/a><\/div>');
		$('#main_content h2').filter(':not(:first)').each(function() {
			$(this).before('<div class="gototop"><a  href="#top">[Go to top]<\/a><\/div>');
		});
		
		// adds bookmark to each section and creates a table of contents;
		// last bit needs to be altered, as it is cheating a bit
		$(":header").not('h1').each(function(i) {
			return $(this).before('<a id="h_' + i +'"><\/a>');
		}).clone().appendTo("div#toc").wrapAll('<ul><\/ul>').
		each(function(i) {
			var toc_item = '';
			if($(this).is('h2')) {
				toc_item = $(this).replaceWith('<li class="toclevel-1"><a href="#h_' + i + '">' + this.innerHTML + '<\/a><\/li>');
			}
			else {
				toc_item = $(this).replaceWith('<ul><li class="toclevel-2"><a href="#h_' + i + '">' + this.innerHTML + '<\/a><\/li><\/ul>');
			}
			return toc_item;
		});
	}
	else {
		$('#toc').hide();
	}
});
