$(document).ready(function() {
    var slideshowPlay = 0;	
	var slideshowTimer;
	//Set Default State of each portfolio piece
	$("#slideShow .pagger").show();
	$("#slideShow .pagger a:first").addClass("active");
	$("#slideShow .info .text").html($('#infoSource div:first').html());
	//Get size of images, how many there are, then determin the size of the image reel.
	var imageWidth = $("#slideShow .banner").width();
	var imageSum = $("#slideShow .images img").size();
	var imageReelWidth = imageWidth * imageSum;
	
	//Adjust the image reel to its new size
	$("#slideShow .images").css({'width' : imageReelWidth});
	
	//Paging + Slider Function
	rotate = function(){
		
		var triggerID = $active.parent().index(); //Get number of times to slide
		var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

		$("#slideShow .pagger a").removeClass('active'); //Remove all active class
		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
		$('#slideShow .info .text').slideUp(250,function(){
			$(this).html($('#infoSource div:eq('+triggerID+')').html());
			$(this).slideDown(250);
		});
		//Slider Animation
		$("#slideShow .images").stop(true, true);
		$("#slideShow .images").animate({ 
			left: -image_reelPosition
		}, 500 );
		
	}; 
	rotateNext = function(){
		$active = $('#slideShow .pagger li a.active').parent().next().find('a'); //Move to the next nav item
		if ( $active.length === 0) { //If paging reaches the end...
			$active = $('#slideShow .pagger a:first'); //go back to first
		}
		rotate(); //Trigger the paging and slider function
	};
	stopPlay = function() {
      slideshowPlay = 0;
      clearInterval(slideshowTimer); //Stop the rotation
	}
	startPlay = function() {
	   slideshowPlay = 1;
	   rotateSwitch();
	}
	//Rotation + Timing Event
	rotateSwitch = function(){	
	   $('#play').addClass('active');
	   $('#pause').removeClass('active');
	   slideshowTimer = setInterval(rotateNext, 3000); //Timer speed in milliseconds (3 seconds)
	};
	
	$("#slideShow .pagger a").mouseover(function() {
		$active = $(this);
		//Reset Timer
	 // $('#pause').addClass('active');
   	//$('#play').removeClass('active');
		//stopPlay();
		clearInterval(slideshowTimer); 
		rotate(); //Trigger rotation immediately
		return false; //Prevent browser jump to link anchor
	});
	
	$("#slideShow .pagger a").mouseout(function(){if (slideshowPlay==1) startPlay()});	
	
	$('#pause').click(function(){
 	  stopPlay();
	  $('#pause').addClass('active');
   	$('#play').removeClass('active');
		return false;
	});
	
	$('#play').click(function(){  if (slideshowPlay==1) return false; rotateNext(); startPlay(); return false;});
	startPlay();
	//rotateSwitch(); //Run function on launch
});
