var pages, listLIs;

function showMenu(index,first) {
	var int = (first) ? 0 : 500 ;
	//hide all
	pages.hide();
	//show selected
	pages.eq(index).fadeIn(int);
	//modify toc
	listLIs.removeClass('active');
	listLIs.eq(index).addClass('active');
}

$(document).ready(function () {
	//create toc
	var list = document.createElement('ul');
	list.id = 'menu-toc';
	$('.page h1').each(function(index) {
		var li = document.createElement('li');
		li.appendChild(document.createTextNode($(this).text()))
		li.onclick = function() {
			showMenu(index);
		};
		list.appendChild(li);
	});
	//add list bottom
	var li = document.createElement('li');
	li.className = 'bottom';
	list.appendChild(li);
	//insert list
	$('#menus').before(list);
	//setup variables & page
	pages = $('.page');
	listLIs = $('#menu-toc li');
	//show first
	showMenu(0,true);
});
