
$(document).ready(function() {
	var numberOfItems = $("#slideList li").length;
	var showLength = 7;
	var topItem = 0;
	var browserMsg = (topItem + 1) + " - " + Math.min(numberOfItems, topItem + showLength) + " of " + numberOfItems;
	$('#slideList').css({ overflow: "hidden"});
	if (numberOfItems > showLength) {
		$('#slideList').after("<ul id='pageNav'><li><a class='navPrev' href='#' rel='prev'>Prev</a></li><li><a class='navNext' href='#' rel='next'>Next</a></li><li id='navStat'>" + browserMsg + "</li></ul>");
	}
	
	$('#pageNav .navPrev').click(function(e) {
		e.preventDefault();
		$('#pageNav .navPrev').blur();
		if (topItem > 0) {
			topItem = Math.max(topItem - showLength, 0);
			$('#slideList').scrollTo( 'li:eq(' + topItem +')', 1000, {axis:'y'} );
			browserMsg = (topItem + 1) + " - " + Math.min(numberOfItems, topItem + showLength) + " of " + numberOfItems;
			$('#pageNav #navStat').html(browserMsg);
		}
	});
	
	$('#pageNav .navNext').click(function(e) {
		e.preventDefault();
		$('#pageNav .navNext').blur();
		if (topItem < numberOfItems) {
			topItem = Math.min(topItem + showLength, numberOfItems - showLength);
			$('#slideList').scrollTo( 'li:eq(' + topItem + ')', 1000, {axis:'y'} );
			browserMsg = (topItem + 1) + " - " + Math.min(numberOfItems, topItem + showLength) + " of " + numberOfItems;
			$('#pageNav #navStat').html(browserMsg);
		}
	});
	
	storyNumber = $('.storyTitle').attr("id");

	//highlight and scroll to Selected Item
	var count = 0;
	var scrollToX = 0;
	$('#slideList li a').each(function(i) {
		count++;
		if ($(this).attr("rel") == storyNumber) {
			scrollToX = Math.floor((count-1)/8) * 8;
			$(this).addClass("activeLink");
		}
	});
	if (storyNumber==null) $('#slideList li:eq(0) a').addClass("activeLink");
	if (scrollToX > 0) {
		$('#slideList').scrollTo( 'li:eq(' + scrollToX + ')', 1000, {axis:'y'} );
		topItem = scrollToX;
	}
	browserMsg = (topItem + 1) + " - " + Math.min(numberOfItems, topItem + showLength) + " of " + numberOfItems;
	$('#pageNav #navStat').html(browserMsg);
	
});