/**
 * Developed by Congos Solutions
 * All rights reserved
 */

var SlideShow = {
	images:			new Array(),
	amount:			0,
	offsets:		[ '14px', '232px', '450px' ],
	current:		new Array(),

	init: function(){
		this.images = $$('#slideshow .container');
		this.amount = this.images.length;

		if (this.amount < 6) {
			return;
		}

		this.initImages();

		this.next.periodical(8000, this);
	},

	initImages: function(){
		var newImg;

		this.images.fade('hide');

		for (i=0; i<3; i++) {
			newImg = this.images.filter(function(img){
				return !(this.current.contains(img));
			}, this).getRandom();

			newImg.style.left = this.offsets[i];
			newImg.fade.delay(i*150, newImg, 'in');

			this.current.push(newImg);
		}
	},
	
	next: function(){
		var newImg, oldImg;

		for (i=0; i<3; i++) {
			newImg = this.images.filter(function(img){
				return !(this.current.contains(img));
			}, this).getRandom();

			newImg.style.left = this.offsets[i];
			newImg.fade.delay(i*150, newImg, 'in');

			oldImg = this.current[i];
			oldImg.tween.delay(i*150, oldImg, [ 'opacity', 0 ]);

			this.current.push(newImg);
		}

		for (i=0; i<3; i++) {
			this.current.shift();
		}
	}
};

window.addEvent('domready', function(){
	SlideShow.init();
});
