$(document).ready(function() {

	var slideshow = true;
	var	slideShowTimer = 7000; //Timer speed in milliseconds

	//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.
	var imageWidth = $(".box_contents").width() + 0; //20px del padding
	var imageSum = $(".contents_reel .box_item").size();
	var imageReelWidth = imageWidth * imageSum;
	
	
	//Adjust the image reel to its new size
	$(".contents_reel").css({'width' : imageReelWidth});
	
	//Paging + Slider Function
	rotate = function(it){	
		var triggerID = $active.attr("rel") - 1; //Get number of times to slide
		var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

		$(it).parent().parent().find(".paging a").removeClass('active'); //Remove all active class
		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
		
		//Slider Animation				
		$(it).parent().parent().find(".contents_reel").animate({ 
			left: -image_reelPosition
		}, 1000 );
		
	}; 
	
	//Rotation + Timing Event
	rotateSwitch = function(){		
		play = setInterval(function(){ //Set timer - this will repeat itself every x seconds
			$active = $('.paging a.active').next();
			if ( $active.length === 0) { //If paging reaches the end...
				$active = $('.paging a:first'); //go back to first
			}
			rotate(".contents_reel"); //Trigger the paging and slider function
		}, slideShowTimer); 
	};
	
	if(slideshow == true)
	rotateSwitch(); //Run function on launch
	
	//On Hover
	$(".contents_reel").hover(function() {
		
		if(slideshow == true)
		clearInterval(play); //Stop the rotation 
		
	}, function() {
		
		if(slideshow == true)
		rotateSwitch(); //Resume rotation
	});	
	
	//On Click
	$(".paging a").click(function() {	
		$active = $(this); //Activate the clicked paging
		
		if(slideshow == true)
		//Reset Timer
		clearInterval(play); //Stop the rotation
		
		rotate(this); //Trigger rotation immediately
		
		if(slideshow == true)
		rotateSwitch(); // Resume rotation		
		
		return false; //Prevent browser jump to link anchor
	});	
	
	//Next button
	$("#slider_next_bn").click(function() {
		$active = $('.paging a.active').next();
		if ($active.length === 0) { //If paging reaches the end…
		$active = $('.paging a:first'); //go back to first
		}
		
		if(slideshow == true)
		clearInterval(play); //Stop the rotation
		
		rotate(); //Trigger rotation immediately
		
		if(slideshow == true)
		rotateSwitch(); // Resume rotation
		
		return false; //Prevent browser jump to link anchor
	});

	//Prev button
	$("#slider_prev_bn").click(function() {
		$active = $('.paging a.active').prev();
		if ($active.length === 0) { //If paging reaches the end…
		$active = $('.paging a:last'); //go back to first
		}
		
		if(slideshow == true)
		clearInterval(play); //Stop the rotation
		
		rotate(); //Trigger rotation immediately
		
		if(slideshow == true)
		rotateSwitch(); // Resume rotation
		
		return false; //Prevent browser jump to link anchor
	});
	
	
});

