/* This script file contains code to rotate divs. */
/* Requires Scriptaculous and Prototype */

function DivRotator(params) {
	this.name = params.name;
	this.divs = params.divs;
	this.currentdiv = 0;
	this.nextdiv = 1;
	this.fadetime = params.fadetime ? params.fadetime : 1;
	this.displaytime = params.displaytime ? params.displaytime : 5000;
	this.fader = null;
	
	// hide all divs except first one
	for (var i = 1; i < this.divs.size(); i++)
		this.divs[i].setStyle({ display: "none" });
	
	// gets next div in array. basically loops back to the first item when end of array is reached.
	// this function sets the current div index to the next div and next div index to the next div after that
	this.getNextDiv = function() {
		this.currentdiv = this.nextdiv;
		this.nextdiv++;
		if (this.nextdiv == this.divs.size())
			this.nextdiv = 0;
	
		return this.nextdiv;
	} // end getNextDiv

	// gets previous div in array. basically loops back to the last item when beginning of array is reached.
	// this function sets the next div index to the current div and the current div index to the previous div
	this.getPreviousDiv = function() {
		this.nextdiv = this.currentdiv;
		this.currentdiv--;
		if (this.currentdiv == -1)
			this.currentdiv = this.divs.size() - 1;
	
		return this.currentdiv;
	} // end getPreviousDiv

	// fades out div1, fade in div2
	this.fadeInOut = function(div1, div2) {
		div1.fade({ duration: this.fadetime, queue: { position: "end", scope: this.name } });
		div2.appear({ duration: this.fadetime, queue: { position: "end", scope: this.name } });
	} // end fadeInOut

	// heart of the object. displays objects for displaytime *milliseconds*
	// then fades in and out the next item for fadetime *seconds*
	this.rotate = function() {
		if (this.divs.size() > 0) {
			var dv1, dv2;
			var _self = this;
			dv1 = this.currentdiv;
			dv2 = this.nextdiv;
			this.getNextDiv();
			this.fadeInOut(this.divs[dv1], this.divs[dv2]);
			
			// magic function: sets timeout to call itself. :)
			this.fader = setTimeout(function() { _self.rotate(); }, this.displaytime + (this.fadetime * 1000));
		} // end if
	} // end rotate
	
	// call to start rotation
	this.startRotate = function() {
		var _self = this;
		this.fader = setTimeout(function() { _self.rotate(); }, this.displaytime + (this.fadetime * 1000));
	} // end startRotate
	
	// function to forcibly switch to next div, resetting timer count
	// fade is a boolean that sets whether current div is to be faded or immediately hidden
	this.nextDiv = function(fade) {
		clearTimeout(this.fader);
		
		if (fade)
			this.rotate();
		
		else {
			if (this.divs.size() > 0) {
				this.divs[this.currentdiv].setStyle({ display: "none" });
				this.divs[this.nextdiv].setStyle({ display: "" });
				
				this.getNextDiv();
				
				this.startRotate();
			} // end if
		} // end else
	} // end nextDiv

	// function to forcibly switch to previous div, resetting timer count
	// fade is a boolean that sets whether current div is to be faded or immediately hidden
	this.previousDiv = function(fade) {
		clearTimeout(this.fader);
		
		if (fade) {
			if (this.divs.size() > 0) {
				this.getPreviousDiv();
				this.rotate();
			} // end if
		} // end if
		
		else {
			if (this.divs.size() > 0) {
				this.getPreviousDiv();

				this.divs[this.nextdiv].setStyle({ display: "none" });
				this.divs[this.currentdiv].setStyle({ display: "" });

				this.startRotate();
			} // end if
		} // end else
	} // end previousDiv
} // end DivRotator

