// Jacob Rask 2009. http://jacobrask.net/lab/toq/

$(document).ready(function(){
	var ToC = $("#toc");
	if (ToC.length > 0)
		genToQ(ToC);
});

function genToQ (ToC) {
	before = "<h2>Contents</h2>";
	tocLink = '';
	$('h2').each(function(i){
		var a = $(this);
		tocId = a.attr("id");
		if (tocId == '')
			tocId = genId(a);
		tocText = a.text();
		tocLink += '<li><a href="#' + tocId + '">' + tocText + '</a></li>';
	});
	tocLink = before + '<ol>' + tocLink + '</ol>';
	ToC.prepend(tocLink);
};

function genId (a) {
	b = a.text().toLowerCase();
	b = b.replace(/^[ \t]+|[^a-zA-Z0-9- _]+|[ \t]+$/g,'');
	b = b.replace(/[\s]+/g,'-');
	b = 'toc_' + b;
	a.attr('id',b);
	return b;
};
