/***
 * Slideshow.js
 * Tom McFarlin / February 2008
 * version 1.0 beta
 */

/*---------------------------------------------------------------------------*/
document.observe('dom:loaded', function() {
	var images = $$('div#slideshow-container img');
	var container = $('slideshow-container');
	if (images.length == 1 || !container) return;
	images.each(function(i) {
		if (images.first() != i) {
			i.hide();
		} // end if
	});
	new Slideshow(images, container);
}); // end observer
/*---------------------------------------------------------------------------*/
var Slideshow = Class.create({
	
	initialize: function(imgs, cntnr) {
		
		var This = this;
		this.images = imgs;
		this.container = cntnr;
		this.FADE = false;
		this.SPEED = 5;
		this.activeImage = null;
		this.nextImage = null;
		
		this.start();
		
	}, // initialize
	
	start: function() {
		
		this._setupImages();
		this._setupContainer();
		var This = this;
		new PeriodicalExecuter(function(pe) {
			This._rotate();
		}, This.SPEED)
		
	}, // start
	
	_setupImages: function() {
		
		var This = this;
		this.images.each(function(i) {
			
			i.setStyle({
				marginBottom: '-' + i.height + 'px',
				float: 'left'
			});
			
			if(This.images.first() == i) {
				This.activeImage = i;
			} else {
				i.hide();
			}
			
		});
		
	}, // setup
	
	_setupContainer: function() {
		
		var maxHeight = -1;
		var maxWidth = -1;
		this.images.each(function(i) {
			if (i.height > maxHeight) {
				maxHeight = i.height;
			}
			if (i.width > maxWidth) {
				maxWidth = i.width;
			}
		});
	
		this.container.setStyle({
			height: maxHeight + 'px',
			width: maxWidth + 'px'
		});
		
		var This = this;
		this.container.classNames().each(function(n) {
			if(n.toLowerCase() == 'fade') {
				This.FADE = true;
			} else {
				This.SPEED = n;
			}
		});
		
	}, // setupContainer
	
	_rotate: function() {

		if(this.activeImage == this.images.last()) {
			this.nextImage = this.images.first();
		} else {
			this.nextImage = this.images[this.images.indexOf(this.activeImage) + 1];
		} 
		
		this._swap(this.activeImage, this.nextImage);

	}, // end _rotate
	
	_swap: function(current, next) {
		
		if(this.FADE) {
			new Effect.Fade(current);
			new Effect.Appear(next);
		} else {
			current.hide();
			next.show();
		}
		
		this.activeImage = this.nextImage;
		
	} // swap
	
}); // Slideshow
