//On page load start this function.

$(document).ready(function() {
						   
	var isActive = true;
	 function checkActive (){
		 
		 window.onfocus = function () {    
			isActive = true;
		 };
		 
		 window.onblur = function () {    
			isActive = false;
		 };  
	 }
	 
	$(".paging").css('visibility','visible');
	$(".paging").show();
	$(".paging a:first").addClass("active");
	var imageWidth = 946;
	var imageSum = 3784;
	var imageReelWidth = imageWidth * imageSum;
	$("#slideshow").css({'width' : imageReelWidth});
	rotate = function(){
		checkActive();
	
		if(isActive == true){
			
			var triggerID = $active.attr("rel") - 1;
			var image_reelPosition = triggerID * imageWidth;
			$(".paging a").removeClass('active');
			$active.addClass('active');
			$("#slideshow").animate({ 
				left: -image_reelPosition
			}, 500 );
		}
		else if(isActive == false){
			clearInterval(play);
		}
	};
	rotateSwitch = function(){		
		play = setInterval(function(){			
			$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
		}, 4000); //Timer speed in milliseconds (3 seconds)
	};
	rotateSwitch(); //Run function on launch
	$("#slideshow").hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		rotateSwitch(); //Resume rotation
	});	
	$(".paging a").click(function() {	
		$active = $(this);
		clearInterval(play); //Stop the rotation
		rotate(); //Trigger rotation immediately
		rotateSwitch(); // Resume rotation
		return false; //Prevent browser jump to link anchor
	});
	
});
