/*
 * Slideshow Javascript .. Static File not pluginized
 * created for KCREP.org
 * by Jay Pilgreen
 * copyright River City Studio
 */

var SLIDESHOW = {};

// Additional Globally Scoped variables
var sliderImages = {};
var containerWidth = null;
var currentSliderIndex = 0;
var timer = null;

SLIDESHOW.init = function() {
	sliderImages = $(".sliderImage");
	containerWidth = $("#slideshowContainer").width();
	
	// Ok .. new setup .. we want to start already sliding .. so here we go
	$("#startTimer").addClass("active");
	timer = window.setInterval(function() { SLIDESHOW.next() }, 6000);
	
	
	// Set our default overlay content with the first slider image
	$("#slideshowOverlayContent").html($(sliderImages[currentSliderIndex]).data("overlay"));
	
	// Set up our container
	sliderImages.each( function(index) {
		$(this).css({
			left: (index) * containerWidth
		})
	});
	
	$("#lastSlide").click(function() {
		SLIDESHOW.next("reverse");
	});
	
	$("#nextSlide").click(function() {
		SLIDESHOW.next();
	});
	
	// Timer Functionality
	$("#startTimer").click( function() {
		$(this).toggleClass("active");
		
		if($(this).hasClass("active")) {
			SLIDESHOW.next();
			timer = window.setInterval(function() { SLIDESHOW.next() }, 6000);
		} else {
			window.clearInterval(timer);
		}
	});
	
	// Set the overlay hover effect
	$("#slideshowContainer").hover( 
		function() {
			$("#slideshowOverlay").addClass("out", 0);
		},
		function() {
			$("#slideshowOverlay").removeClass("out", 350);
		}
	);
	
} // End init function


	// Additional Functions

SLIDESHOW.next = function(reverse) {
	currentLeftPosition = $(sliderImages[0]).position().left;
	resetPoint = containerWidth * (sliderImages.length - 1);
	moveToPoint = new String();
	
	if(reverse != "reverse") {
		// Initial Move
		currentSliderIndex = currentSliderIndex + 1;
		moveToPoint = "-=" + containerWidth;
		// Reset if we've hit the end
		if(currentSliderIndex > sliderImages.length - 1) {
			currentSliderIndex = 0;
			moveToPoint = "+=" + resetPoint;
		}
		
	} else {
		// Initial Move Backwards
		currentSliderIndex = currentSliderIndex - 1
		moveToPoint = "+=" + containerWidth;
		// Reset if we've hit the end
		if( currentSliderIndex < 0 ){
			currentSliderIndex = sliderImages.length - 1;
			moveToPoint = "-=" + resetPoint;
		}
	}
	
	sliderImages.animate({
		left: moveToPoint
	}, 550, "easeInOutCirc");
	
	// Set the overlay text
	$("#slideshowOverlay").html($(sliderImages[currentSliderIndex]).data("overlay"));
	
// 	#("#slideshowOverlay").text($(sliderImages[currentSliderIndex]).data("overlay"));
}
