$(document).ready(function() {

	//Set Default State of each portfolio piece
	$(".paging_wrapper").show();
	$(".paging a:first").addClass("active");
		
	//Get size of features, how many there are, then determine the size of the feature window.
	var featureWidth = $("#window").width();
	var featureCount = $(".window_features .each_feature").size();
	var featureReelWidth = featureWidth * featureCount;
	
	//Adjust the image reel to its new size
	$(".window_features").css({'width' : featureReelWidth});
	
	//Paging + Slider Function
	rotate = function(){	
		var triggerID = $active.attr("rel") - 1; //Get number of times to slide
		var window_featuresPosition = triggerID * featureWidth; //Determines the distance the image reel needs to slide

		$(".paging a").removeClass('active'); //Remove all active class
		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
		
		//Slider Animation
		$(".window_features").animate({ 
			left: -window_featuresPosition
		}, 1000 );
		
	}; 
	
	//Rotation + Timing Event
	rotateSwitch = function(){		
		play = setInterval(function(){ //Set timer - this will repeat itself every n 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
		}, 6000); //Timer speed in milliseconds
	};
	
	rotateSwitch(); //Run function on launch
	
	//On Hover
	$(".window_features a").hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		rotateSwitch(); //Resume rotation
	});	
	
	//On Click
	$(".paging a").click(function() {
		document.getElementById('featuresPaused').value=true;
		document.getElementById('playPauseLabel').innerHTML='play';
		playPauseFeatures(true);
		$active = $(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
	});
	
	// next button
	$(".next_feature").click(function() {
		document.getElementById('featuresPaused').value=true;
		document.getElementById('playPauseLabel').innerHTML='play';
		playPauseFeatures(true);
		$active = $('.paging a.active').next();
		if ( $active.length === 0) { //If paging reaches the end...
			$active = $('.paging a:first'); //go back to first
		}
		clearInterval(play); // pause until user clicks resume button
		rotate(); //Trigger rotation immediately
		return false; //Prevent browser jump to link anchor
	});
	
	// previous button
	$(".previous_feature").click(function() {
		document.getElementById('featuresPaused').value=true;
		document.getElementById('playPauseLabel').innerHTML='play';
		playPauseFeatures(true);
		$active = $('.paging a.active').prev();
		if ( $active.length === 0) { //If paging reaches the end...
			$active = $('.paging a:last'); //go back to first
		}
		clearInterval(play); // pause until user clicks resume button
		rotate(); //Trigger rotation immediately
		return false; //Prevent browser jump to link anchor
	});
	
});

function playPauseFeatures(pause_override) {
	var featuresPaused=document.getElementById('featuresPaused');
	var playPauseLabel=document.getElementById('playPauseLabel');
	if (featuresPaused.value=='true' && !pause_override) { // if paused, resume
		rotateSwitch(); // PLAY
		playPauseLabel.innerHTML='pause';
		featuresPaused.value='false';
		var div=document.getElementById('play_pause_feature');
		div.getElementsByTagName('a')[0].style.backgroundPosition='-74px 0px';
	} else { // otherwise, pause
		clearInterval(play); // PAUSE
		featuresPaused.value=true;
		playPauseLabel.innerHTML='play';
		featuresPaused.value='true';
		var div=document.getElementById('play_pause_feature');
		div.getElementsByTagName('a')[0].style.backgroundPosition='-37px 0px';
	}
}
