//some checks to make sure we have the proper libraries loaded...
if(Prototype == undefined || parseFloat(Prototype.Version) < 1.6) {
	throw("Prototype is not loaded or Prototype Version is less than 1.6");
}
if(Effect == undefined){
	throw("Scriptaculous is not defined or not loaded.");
}
var SlideShow = Class.create();
SlideShow.prototype = {
	initialize:function(container,options) {
		this.options = Object.extend({
			timeout: 5000,
			slide_text: true,
			speed: 1,
			text_speed: 0.6,
			text_class: 'slideshowText',
			pause: true
		}, options || {});
		this.container = $(container);
		this.OnlyOne = false;
		this.currSlide = 0;
		this.lastSlide = 0;
		this.nextSlide = 0;
		this.paused = false;
		this.timer = null;
		if(this.container != undefined){
			this.items = this.container.childElements();
			this.textOrigionalX = this.container.getHeight();
			this.total = this.items.length;
			this.set_style();
			if(this.total > 1){
				this.buildEvents();
				this.startup();
			}
			else if(this.total == 1){
				this.OnlyOne = true;
				this.justOne();
			}
		} else {
			throw("The container is not defined.");
		}
	},
	set_style: function() {
		var i = 0;
		this.items.each(function(el){
			var textblock = el.select('.' + this.options.text_class)[0];
			if(textblock != undefined){
				textblock.hide();
			}
			el.setStyle({
				position: 'absolute',
				top: '0px',
				left: '0px'
			});
			if(i > 0){
				el.hide();
			}
			i++;
		}.bind(this));
		this.container.setStyle({position: 'relative'});
	},
	buildEvents: function(){
		if(this.options.pause == true){
			this.container.observe('mouseover', this.pause.bind(this));
			this.container.observe('mouseout', this.unpause.bind(this));
		}
	},
	pause: function(e){
		this.paused = true;
		this.clearTimer();
	},
	unpause: function(e){
		this.paused = false;
		this.setTimer();		
	},
	startup: function(){
		if(this.options.slide_text){
			this.textSlide(0);
		} else {
			this.setTimer();
		}
	},
	setTimer: function(){
		if(!this.paused) {
			this.timer = setTimeout(this.nextImage.bind(this), this.options.timeout);
		}
	},
	clearTimer: function(){
		clearTimeout(this.timer);
	},
	textSlide: function(num) {
		var textblock = this.items[num].select('.' + this.options.text_class)[0];
		if(textblock != undefined){
			new Effect.Move(textblock, {
				beforeStart:function(){ textblock.show(); },
				y:( -1 * textblock.getHeight() ),
				mode: 'relative',
				duration: this.options.text_speed,
				afterFinish:function(){ if(!this.OnlyOne){this.setTimer();} }.bind(this)
			});
		}else{
			if(!this.OnlyOne){
				this.setTimer();
			}
		}
	},
	closeText: function(num){
		var textblock = this.items[num].select('.' + this.options.text_class)[0];
		if(textblock != undefined){
			new Effect.Move(textblock, {
				y: this.textOrigionalX,
				mode: 'absolute',
				duration: this.options.text_speed,
				afterFinish:function(){ textblock.hide(); }.bind(this)
			});
		}
	},
	animate: function(){
		this.clearTimer();
		this.items[this.currSlide].fade({
			duration: this.options.speed,
			beforeStart: function(){ this.closeText(this.currSlide); }.bind(this)
		});
		this.items[this.nextSlide].appear({
			duration: this.options.speed,
			afterFinish:function(){
				if(this.options.slide_text){
					this.textSlide(this.nextSlide);
				} else {
					this.setTimer();
				}
			}.bind(this)
		});
	},
	justOne: function(){
		this.textSlide(0);		
	},
	nextImage: function(){
		this.clearTimer();
		this.lastSlide = this.currSlide;
		var roll = (this.nextSlide + 1) == this.total;
		this.nextSlide = roll ? 0 : this.nextSlide+1;
		this.currSlide = roll ? this.total-1 : this.nextSlide-1;
		this.animate();
	}
};
