$(function() {

	$('ul.sf-menu').superfish();

	$(".page-navi a:first").addClass("active");

	// Get size of images, how many there are, then determin the size of the image reel.
	var imageWidth		= $(".slide-box").width();

	//Adjust the image reel to its new size
	$(".slide-img").css({'width' : imageWidth * $(".slide-img img").size()});

	//Paging + Slider Function
	rotate = function(){
		//Get number of times to slide
		var slideImgPosition	= ($active.attr("rel") - 1) * imageWidth; //Determines the distance the image reel needs to slide
 
		$(".page-navi a").removeClass('active'); //Remove all active class
		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

		//Slider Animation
		$(".slide-img").animate({ 
			left: -slideImgPosition
		}, 500 );
	};

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

	rotateSwitch(); //Run function on launch

	//On Hover
	$(".slide-img a").hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		rotateSwitch(); //Resume rotation
	});

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

});

