//Carousel class
function Carousel() {
	//methods
	this.AddObject = mtdAddObject;
	this.AddNav = mtdAddNav;
	this.Scroll = mtdScroll;
	this.Start = mtdStart;
	this.Stop = mtdStop;
	this.CheckUpdate = mtdCheckUpdate;

	//properties
	this.Objects = new Array;
	this.Pages = 1;
	this.CurrentPage = 1;
	this.TotalScroll = 636;
	this.ScrollSpeed = 5;
	this.ScrollStep = 12;
	
	//private
	var blnRunning = false;
	var blnMoving = false;
	
	//Methods
	
	//Adds an object to the stored list
	// id corresponds to an object id in the DOm
	function mtdAddObject(id) {
		var obj;
		obj = document.getElementById(id);
		if (obj) {
			this.Objects[this.Objects.length] = obj;
		}
	}
	
	//Adds the nav objects
	function mtdAddNav(idPrev, idNext, intPages) {
		this.Prev = document.getElementById(idPrev)
		this.Next = document.getElementById(idNext);
		this.Pages = intPages;
		checkPages(this);
	}
	
	//Fades up an object
	// id corresponds to an object id in the DOm that's already been added
	// direction is a boolean, left is true
	function mtdScroll(id, direction) {
		var obj;
		if (!blnMoving) {
			for (var i=0; i<this.Objects.length; i++) {
				obj = this.Objects[i];
				obj.icoScroll = direction;
				if (direction) {
					this.CurrentPage--;
					obj.targetScroll = getScroll(obj) + this.TotalScroll;
				} else {
					this.CurrentPage++;
					obj.targetScroll = getScroll(obj) - this.TotalScroll;
				}
				checkPages(this);
			}
			this.CheckUpdate();
		}
	}
	
	//Initializes everything
	function mtdStart() {
		blnRunning = true;
		pauseObjectMethod(this, this.CheckUpdate, this.FadeSpeed);
	}
	
	//Stops all fading etc
	function mtdStop() {
		blnRunning = false;
	}
	
	//Continues to update the scrolls
	function mtdCheckUpdate() {
		var blnChanged = false;
		if (blnRunning) {
			var o;
			for (var i=0; i<this.Objects.length; i++) {
				obj = this.Objects[i];
				o = getScroll(obj);
				if (obj.icoScroll) {			//Going Left
					if (o<obj.targetScroll) {
						setScroll(obj, o+this.ScrollStep);
						blnChanged = true;
					}
				} else {			//Going Right
					if (o>obj.targetScroll) {
						setScroll(obj, o-this.ScrollStep);
						blnChanged = true;
					}
				}
			}
			if (blnChanged) {
				pauseObjectMethod(this, this.CheckUpdate, this.ScrollSpeed);	//keep calling to continue checking
			}
		}
		blnMoving = blnChanged;
	}
	
	//Private functions
	
	function checkPages(context) {
		if (context.Next)	context.Next.style.visibility = (context.CurrentPage < context.Pages) ? 'visible' : 'hidden';
		if (context.Prev) context.Prev.style.visibility = (context.CurrentPage > 1) ? 'visible' : 'hidden';
	}
		
	//Sets scroll position of an object to the integer value
	function setScroll(obj, value) {
	  obj.style.left = value + 'px';
	}
	
	//Returns the scroll of an object as an integer
	function getScroll(obj) {
		var s = obj.style.left;
		if (isNaN(parseInt(s))) {
			return 0;
		} else {
			return parseInt(s);
		}
	}
		

}