var slider = {
	$active: null
};

slider.init = function(interval){

	slider.interval = (interval) ? interval : 4500;

	//Set Default State of each portfolio piece
	$(".paging").show();
	$(".paging a:first").addClass("active");

	//Get size of images, how many there are, then determin the size of the image reel.
	slider.imageWidth = $(".window").width();
	slider.imageSum = $(".image_reel img").size();
	slider.imageReelWidth = slider.imageWidth * slider.imageSum;
	
	//Adjust the image reel to its new size
	$(".image_reel").css({'width' : this.imageReelWidth});

	slider.rotateSwitch();

	//On Hover
	$(".image_reel").hover(function() {
		clearInterval(slider.play); //Stop the rotation
	}, function() {
		slider.rotateSwitch(); //Resume rotation
	});	
	
	//On Click
	$(".paging a").click(function(e) {	
		slider.$active = $(this); //Activate the clicked paging
		//Reset Timer
		clearInterval(slider.play); //Stop the rotation
		slider.rotate(); //Trigger rotation immediately
		slider.rotateSwitch(); // Resume rotation
		e.preventDefault();
	});	


}


//Paging + Slider Function
slider.rotate = function(){	

	var triggerID = slider.$active.attr("rel") - 1; //Get number of times to slide
	var image_reelPosition = triggerID * slider.imageWidth; //Determines the distance the image reel needs to slide

	$(".paging a").removeClass('active'); //Remove all active class
	slider.$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
		
	//Slider Animation
	/* $(".image_reel").animate({ 
		left: -image_reelPosition
	}, 500 ); */
	$(".image_reel").css("left", -image_reelPosition);
		
}; 
	
//Rotation + Timing Event
slider.rotateSwitch = function(){		
	slider.play = setInterval(function(){ //Set timer - this will repeat itself every 3 seconds
		slider.$active = $('.paging a.active').next();
		if ( slider.$active.length === 0) { //If paging reaches the end...
			slider.$active = $('.paging a:first'); //go back to first
		}
		slider.rotate(); //Trigger the paging and slider function
	}, slider.interval);
};
