$(document).ready(function() {

	var outCount = 0;
	var inCount = 1;
	// slideCount populated on page loading this script
	var index = slideCount - 1;
	var colCount = 4;
	var addIndex = 0;
	var playToggle = 1;

	$(".slideNumbers").text("1 - 4 OF " + slideCount);
	
	function playSlideshow() {
		//alert("inCount: " + inCount + " outCount: " + outCount);
		$(".infoLink").eq(outCount).removeClass("current");
		$(".infoLink").eq(inCount).addClass("current");
		$(".infoLink").eq(outCount).next().fadeOut();
		$(".infoLink").eq(inCount).next().fadeIn();
		$(".infoLink").eq(outCount).next().next().fadeOut();
		$(".infoLink").eq(inCount).next().next().fadeIn();
		outCount = (outCount == index) ? 0:outCount + 1;
		inCount = (inCount == index) ? 0:inCount + 1;
		
		if(outCount%colCount == 0) {
			addIndex = (addIndex == (slideCount-colCount)) ? 0 : addIndex+colCount;
			makeCurrentRow(addIndex);
		}
	}

	$(document).everyTime(4000, 'switchslide', playSlideshow);
	
	function makeCurrentRow(addIndex) {
		for(var i=0; i<$(".infoPanel li").length; i++) {
			$(".infoPanel li").eq(i).removeClass("currentRow");
		}
		for(var j=0; j<colCount; j++) {
			$(".infoPanel li").eq(j+addIndex).addClass("currentRow");
		}
		$(".slideNumbers").text((1+addIndex) + " - "+(addIndex+colCount)+" OF "+slideCount);
	}
	
	$(".previousSlideSetButton").click(function() {
		if(!playToggle) {
			$(".pauseSlideshow").removeClass("playSlideshow");
			playToggle = 1;
		}
		
		$(document).stopTime("switchslide");
		$(".infoLink").eq(outCount).next().fadeOut();
		$(".infoLink").eq(outCount).next().next().fadeOut();
		$(".infoLink").eq(outCount).removeClass("current");
		
		addIndex = (addIndex == 0) ? slideCount-colCount : addIndex-colCount;
		inCount = addIndex;
		outCount = inCount - 1;
		
		$(".infoLink").eq(inCount).addClass("current");
		$(".infoLink").eq(inCount).next().fadeIn();
		$(".infoLink").eq(inCount).next().next().fadeIn();
		
		outCount = (outCount == index) ? 0:outCount + 1;
		inCount = (inCount == index) ? 0:inCount + 1;
		makeCurrentRow(addIndex);
		
		$(document).everyTime(4000, 'switchslide', playSlideshow);
	});
	
	$(".pauseSlideshow").click(function() {
		if(playToggle) {
			$(".pauseSlideshow").addClass("playSlideshow");
			$(document).stopTime("switchslide");
			playToggle = 0;
		}else{
			$(".pauseSlideshow").removeClass("playSlideshow");
			$(document).everyTime(4000, 'switchslide', playSlideshow);
			playToggle = 1;
		}
		
	});
	
	$(".nextSlideSetButton").click(function() {
		if(!playToggle) {
			$(".pauseSlideshow").removeClass("playSlideshow");
			playToggle = 1;
		}
		
		$(document).stopTime("switchslide");
		$(".infoLink").eq(outCount).next().fadeOut();
		$(".infoLink").eq(outCount).next().next().fadeOut();
		$(".infoLink").eq(outCount).removeClass("current");
		
		addIndex = (addIndex == (slideCount-colCount)) ? 0 : addIndex+colCount;
		inCount = addIndex;
		outCount = inCount - 1;
		
		$(".infoLink").eq(inCount).addClass("current");
		$(".infoLink").eq(inCount).next().fadeIn();
		$(".infoLink").eq(inCount).next().next().fadeIn();
		
		outCount = (outCount == index) ? 0:outCount + 1;
		inCount = (inCount == index) ? 0:inCount + 1;
		makeCurrentRow(addIndex);
		
		$(document).everyTime(4000, 'switchslide', playSlideshow);
		
	});
	
	// $(".infoLink").mouseenter(function() {
	// 		$(document).stopTime("switchslide");
	// 		if (this.className.indexOf("current") == -1) {
	// 			// $("a.current").next().fadeOut();
	// 			$("a.current").removeClass("current");
	// 			$(this).addClass("current");
	// 			// $(this).next().next().fadeIn();
	// 		}
	// 	});
});


