//
// JQuery Image Slider - works in conjunction with CSS and does not rely on IEx DX functions.
// 
//  Note: This code is a modified version on example code posted by Soh Tanaka his web site at
//
//                  http://www.sohtanaka.com/web-design/examples/image-slider/
//
//  Modified by Leon Cate to rotate images in a circular manner instead of sliding left 
//  through all the images to get back to the first one.  Works by putting a duplicate of the
//  last image in the front of the list and checking for the first page.  The additional button
//  click code maintains the ability to slide left or right to a specific image on mouse click.
//
	$(document).ready(function() {

	//Set Default State of each portfolio piece
	$(".paging").show();
	$(".paging a:first").addClass("active");
	
	//Get size of images, how many there are, then determine the size of the image reel.
	var imageWidth = $(".window").width();
	var imageSum = $(".image_reel img").size() + 1;
	var imageReelWidth = imageWidth * imageSum;
    var buttonClicked = 1;		// added to track button clicks and first image display
								// please note that the class definition for image_reel
								// also starts with the left position set to -imageWidth 
	//Adjust the image reel to its new size
	$(".image_reel").css({'width' : imageReelWidth});
		
	//Paging + Slider Function
	rotate = function(){	
		var triggerID = $active.attr("rel");  //Get number of times to slide
		var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

		$(".paging a").removeClass('active'); //Remove all active classes
		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
	
		//Slider Animation
		if (triggerID == 1  && buttonClicked == 0) {  // If we reached the end and we didn't click the button
			$(".image_reel").animate({ left: 0}, 0 ); // quietly and quickly go back to the first image
		}   										  // (a copy of the last) then fall into standard slide
		$(".image_reel").animate({ left: -image_reelPosition }, 500 );
		buttonClicked = 0;
	}; 
	
	//Rotation + Timing Event
	rotateSwitch = function(){		
		play = setInterval(function(){ //Set timer - this will repeat itself every 7 seconds
			$active = $('.paging a.active').next();
			if ( $active.length === 0) { //If paging reaches the end...
				$active = $('.paging a:first'); //go back to first
			}
			rotate(); //Trigger the paging and slider function
		}, 7000); //Timer speed in milliseconds (7 seconds)
	};
	
	rotateSwitch(); //Run function on launch
	
	//On Hover
	$(".image_reel a").hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		rotateSwitch(); //Resume rotation
	});	
	
	//On Click
	$(".paging a").click(function() {	
		$active = $(this); //Activate the clicked paging
		//Reset Timer
		clearInterval(play); //Stop the rotation
		buttonClicked = 1;
		rotate(); //Trigger rotation immediately
		rotateSwitch(); // Resume rotation
		return false; //Prevent browser jump to link anchor
	});	
	
});

