/*
** autoslideshow.js
** 
** Include: prototype.js
**          effects.js
**
** To initialize a slideshow, include the following
** in the <head> element of the HTML page:
**  var slideshowObject = new slideshow('slideshowObject', 'classNameOfSlides', 'mode');
**
**  where mode = crossfade || fadein
**        classNameOfSlides = class assigned to slide elements in HTML
**
** and initialize with <body onload="slideshowObject.initialize();">
**/
function slideshow(objectName, slideClass, mode) {
	this.slideClass = slideClass;
	this.name = objectName;
	this.mode = mode; // crossfade | fadein
	this.autoDelay = 2000; // ms
	this.timeout;

	this.initialize = function () {
		this.slideArray = $$('.'+this.slideClass);
		this.max = this.slideArray.length-1;
		this.numSlides = this.slideArray.length;

		this.min = 0;
		this.current = this.min;
		this.increment = 1;

		for (var i=0; i<this.numSlides; i++) {
			this.slideArray[i].setStyle({display: "none"});
		}
		this.slideArray[this.min].setStyle({display: ""});

		this.setAutoMode();
	}	

	this.goNext = function () {
		this.setManualMode();
		this.change(this.increment);

		return false;
	}
	this.goPrevious = function () {
		this.setManualMode();
		this.change(-1*this.increment);

		return false;
	}
	this.getNewSlide = function (increment) {
		var newSlide = this.current + increment;
		if (newSlide < this.min) {
			newSlide = this.max;
		} else if (newSlide > this.max) {
			newSlide = this.min;
		}
		return newSlide;
	}
	this.change = function (increment) {
		var prevSlide = this.current;
		this.current = this.getNewSlide(increment);
		
		if (this.mode=='crossfade') {
			this.slideArray[prevSlide].fade({duration:0.25, from:1.0, to:0});
			this.slideArray[this.current].appear({duration: 0.25, from:0, to:1.0});
		} else if (this.mode=='fadein') {
			this.slideArray[prevSlide].hide();
			this.slideArray[this.current].appear({duration: 0.25, from:0, to:1.0});
		}
	}
	this.autoChange = function () {
		this.change(this.increment);
		this.setAutoMode();
	}
	this.setAutoMode = function () {
		this.timeout = setTimeout(this.name+".autoChange("+this.increment+")", this.autoDelay);
	}
	this.setManualMode = function () {
		clearTimeout(this.timeout);
	}
}