$(document).ready(function() {
	// Configure slideshow
	var SLIDE_DURATION = 11000;
	var FADE_DURATION = 400; 
	
	// Initialize slideshow
	var interaction = false;
	
	$("#descriptions li:not(:first-child)").css( "opacity",0);
	
	$("#booklets li a").click(function () {
		interaction = true;
		clearTimeout(slideTimer);
		targetSlideNum = $(this).attr("name");
		advance();
		
		_gaq.push(['_trackPageview','/home/booklet'+$(this).attr("name")+'/']);

		return false;
	});

	var totalSlidesNum =  $("#booklets").children().length;
	var targetSlideNum = 1;
	var slideTimer = false;
	
	function advance() {
		$("#descriptions li").css("opacity",0);
		$("#descriptions li").removeClass("active");
		$("#booklets li").removeClass("active");
		
		$("#booklets li:nth-child("+targetSlideNum+")").addClass("active");
		$("#descriptions li:nth-child("+targetSlideNum+")").addClass("active");
		$("#descriptions li:nth-child("+targetSlideNum+")").fadeTo(FADE_DURATION,1);
		
		targetSlideNum++;
		
		if(targetSlideNum > totalSlidesNum) {
			targetSlideNum = 1;
		}
		
		if(interaction == false) {
			slideTimer = setTimeout(advance, SLIDE_DURATION)
		}
	}
	
	advance();
});
