var $j = jQuery.noConflict();

$j(document).ready(function() {

	//Set Default State of each portfolio piece
	$j('#pager').show();
	$j('#pager a:first').addClass('active');
		
	//Get size of entry divs, how many there are, then determin the size of the div reel.
	var divWidth = $j('#entries').width();
	var divSum = $j('#entry-scroll div.entry-block').size();
	var divReelWidth = divWidth * divSum;
	
	//Adjust the entry reel to its new size
	$j('#entry-scroll').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 div reel needs to slide

		$j('#pager a').removeClass('active'); //Remove all active class
		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
		
		//Slider Animation
		$j('#entry-scroll').animate({ 
			left: -div_reelPosition
		}, 500 );
		
	}; 
	
	//Rotation + Timing Event
	rotateSwitch = function(){		
		play = setInterval(function(){ //Set timer
			$active = $j('#pager a.active').next();
			if ( $active.length === 0) { //If paging reaches the end...
				$active = $j('#pager a:first'); //go back to first
			}
			rotate(); //Trigger the paging and slider function
		}, 7000 ); //Timer speed
	};
	
	rotateSwitch(); //Run function on launch
	
	//On Hover
	$j('#entry-scroll div.entry-block').hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		rotateSwitch(); //Resume rotation
	});	
	
	//On Click
	$j('#pager a').click(function() {	
		$active = $j(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
	});	
	
});
