// This file controls the hero slideshow on the homepage.
// No editing needs to be done on this file.

$(document).ready(function() {

	$(".hero-slideshow-paging").show();
	$(".hero-slideshow-paging a:first").addClass("active");
		
	var divWidth = $(".hero-slideshow-viewport").width();
	var divSum = $(".hero-slideshow-slides div").size();
	var divReelWidth = divWidth * divSum;
	
	$(".hero-slideshow-slides").css({'width' : divReelWidth});
	
	//Paging + Slider Function
	rotate = function(){	
		var triggerID = $active.attr("rel") - 1; //Get number of times to slide
		var div_reelPosition = triggerID * divWidth; //Determines the distance the image reel needs to slide
			$(".hero-slideshow-paging a").removeClass('active'); //Remove all active class
		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
		
		//Slider Animation
		$(".hero-slideshow-slides").fadeOut(300, function(){
		    $(".hero-slideshow-slides").css( {left: -div_reelPosition
		    });
		});
		$(".hero-slideshow-slides").fadeIn(300);

		//here is the slide-in alternative to the fade out above
		//$(".slides").animate({
		//    left: -div_reelPosition
		//}, 500 );
		
	
	}; 
	
	//Rotation + Timing Event
	rotateSwitch = function(){		
		play = setInterval(function(){ 
			$active = $('.hero-slideshow-paging a.active').next();
			if ( $active.length === 0) { 
				$active = $('.hero-slideshow-paging a:first'); 
			}
			rotate();
		}, 9000);
	};
	
	rotateSwitch(); 
	
	//On Hover
	$(".hero-slideshow-slides a").hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		rotateSwitch(); //Resume rotation
	});	
	
	//On Click
	$(".hero-slideshow-paging a").click(function() {	
		$active = $(this); //Activate the clicked paging
		//Reset Timer
		clearInterval(play); //Stop the rotation
		rotate(); //Trigger rotation immediately
		rotateSwitch(); // Resume rotation
		return false; //Prevent browser jump to link anchor
	});
	
});
