$(document).ready(function() {
	slideShow.init();
});

function SlideShow() {
	this.$activepic = 5;
	this.imageWidth;
	var me = this;

	this.rotateSwitch = function() {
		play = setInterval(function(){
			slideShow.rotate("next");
		}, 6000);
	};

	this.rotate = function (direction) {
		var triggerID;
		var image_reelPosition;

		if (direction == "next") {
			this.$activepic = $('.ImageReel a.activepic').next();
			if ( this.$activepic.length === 0) { //If paging reaches the end...
				this.$activepic = $('.ImageReel a:first'); //go back to first
			}
		} else if (direction == "previous") {
			this.$activepic = $('.ImageReel a.activepic').prev();
			if ( this.$activepic.length === 0) { //If paging reaches the beginning...
				this.$activepic = $('.ImageReel a:last'); //go to the final picture
			}
		}

		triggerID = this.$activepic.attr("rel") - 1; //Get number of times to slide
		image_reelPosition = triggerID * this.imageWidth; //Determines the distance the image reel needs to slide

		$(".ImageReel a").removeClass('activepic');
		this.$activepic.addClass('activepic');

		$(".ImageReel").animate({
			left: -image_reelPosition
		},600);
	};

	this.init = function() {

		$(".ImageReel a:first").addClass("activepic");

		//Get size of images, how many there are, then determin the size of the image reel.
		this.imageWidth = $(".ViewSpace").width();
		var imageSum = $(".ImageReel img").size();
		var imageReelWidth = slideShow.imageWidth * imageSum;

		//Adjust the image reel to its new size
		$(".ImageReel").css({'width' : imageReelWidth});

		me.rotateSwitch(); //Run function on launch

		// onclick functions:

		$("#pause_btn").click(function() {
			clearInterval(play);
			$(this).hide();
			$("#resume_btn").show();
		});
		$("#resume_btn").click(function() {
			me.rotateSwitch();
			$(this).hide();
			$("#pause_btn").show();
		});
		$("#next_btn").click(function() {
			clearInterval(play);
			$("#pause_btn").hide();
			$("#resume_btn").show();
			me.rotate("next");
		});
		$("#prev_btn").click(function() {
			clearInterval(play);
			$("#pause_btn").hide();
			$("#resume_btn").show();
			me.rotate("previous");
		});
	}
}

var slideShow = new SlideShow();

