
var slideshow = {
	
	/*slideshow modeled from http://www.sohtanaka.com/web-design/automatic-image-slider-w-css-jquery/*/
	
	construct: function(){
		//Show the paging and activate its first link
		$(".slidesPaging").show();
		$(".slidesPaging a:first").addClass("active");

		//Get size of the image, how many images there are, then determin the size of the image reel.
		slideshow.imageWidth = $(".homepageSlideshowWindow").width();
		slideshow.imageSum = $(".slidesContainer img").size();
		slideshow.imageReelWidth = slideshow.imageWidth * slideshow.imageSum;

		//Adjust the image reel to its new size
		$(".slidesContainer").css({'width' : slideshow.imageReelWidth});
	},
	
	loadUI: function(){
		
		window.onblur = function(){
			clearInterval(slideshow.play);
		}
		
		window.onfocus = function(){
			slideshow.rotateSwitch();
		}
		
		//On Hover
		$(".slidesContainer a").hover(function() {
			clearInterval(slideshow.play); //Stop the rotation
		}, function() {
			slideshow.rotateSwitch(); //Resume rotation timer
		});	

		//On Click
		$(".slidesPaging a").click(function() {
			$active = $(this); //Activate the clicked paging
			//Reset Timer
			clearInterval(slideshow.play); //Stop the rotation
			slideshow.rotate(); //Trigger rotation immediately
			slideshow.rotateSwitch(); // Resume rotation timer
			return false; //Prevent browser jump to link anchor
		});
	},
	
	//Paging  and Slider Function
	rotate: function(){
		var triggerID = $active.attr("rel") - 1; //Get number of times to slide
		var image_reelPosition = triggerID * slideshow.imageWidth; //Determines the distance the image reel needs to slide

		$(".slidesPaging a").removeClass('active'); //Remove all active class
		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

		//Slider Animation
		$(".slidesContainer").animate({
			left: -image_reelPosition
		}, 500 );

	},

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

}
