Cycler = function(window_id, pager_id){
	this.pager = $('#' + pager_id);
	this.reel = $('#' + window_id + ' .image_reel');
	
	//Set Default State of each portfolio piece
	this.pager.show();
	this.pager.find('a:first').addClass("active");
	
	//Get size of images, how many there are, then determin the size of the image reel.
	this.imageWidth = $('#' + window_id).width();
	this.play = null;
	this.active = null;
	
	var imageSum = this.reel.find('img').size();
	var imageReelWidth = this.imageWidth * imageSum;

	//Adjust the image reel to its new size
	this.reel.css({'width' : imageReelWidth});
	this.rotateSwitch();

	var cycler = this;
	//On Hover
	this.reel.find('a').hover(function() {
		clearInterval(cycler.play); //Stop the rotation
	}, function() {
		cycler.rotateSwitch(); //Resume rotation
	});	

	//On Click
	this.pager.find('a').click(function() {	
		cycler.active = $(this); //Activate the clicked paging
		//Reset Timer
		clearInterval(cycler.play); //Stop the rotation
		cycler.rotate(); //Trigger rotation immediately
		cycler.rotateSwitch(); // Resume rotation
		return false; //Prevent browser jump to link anchor
	});
}

Cycler.prototype = {
	//Paging + Slider Function
	rotate : function(){	
		var triggerID = this.active.attr("rel") - 1; //Get number of times to slide
		var image_reelPosition = triggerID * this.imageWidth; //Determines the distance the image reel needs to slide

		this.pager.find('a').removeClass('active'); //Remove all active class
		this.active.addClass('active'); //Add active class (the active is declared in the rotateSwitch function)

		//Slider Animation
		this.reel.animate({ 
			left: -image_reelPosition
		}, 500 );
	},

	//Rotation + Timing Event
	rotateSwitch : function(){		
		var cycler = this;
		this.play = setInterval(function(){ //Set timer - this will repeat itself every 3 seconds
			cycler.active = cycler.pager.find('a.active').first().next();		
			if (cycler.active.length === 0) { //If paging reaches the end...
				cycler.active = cycler.pager.find('a:first').first(); //go back to first
			}
			cycler.rotate(); //Trigger the paging and slider function
		}, 14000); //Timer speed in milliseconds (3 seconds)
	}
}

