﻿var Slideshow = new Class( {
	initialize: function ( obj, effectTime, showTime ) {
		this.container = $(obj);
		this.container.setStyles( { position: 'relative' } );
		this.effectTime = effectTime;
		this.showTime = showTime;
		this.elements = new Array();
		this.effects = new Array();
		$A(this.container.childNodes).each( function ( obj ) {
			if( obj.nodeType == 1 )
			{
				obj = $(obj);
				obj.setStyles( { position: 'absolute', top: '0px', left: '0px' } );
				obj.setOpacity( 0 );
				this.effects.push( obj.effect( 'opacity', { duration: this.effectTime } ) );
				this.elements.push( obj );
			}
		}, this );
		
		this.actElement = 0;
		this.lastElement = null;
		this.stage = -1;
		
		this.nextStage();
	},
	
	nextElement: function () {
		this.lastElement = this.actElement;
		this.actElement++;
		if( this.actElement == this.elements.length )
			this.actElement = 0;
		this.stage = -1;
		this.nextStage();
	},
	
	nextStage: function () {
		this.stage++;
		if( this.stage > 1 )
			return this.nextElement()
		switch( this.stage )
		{
			case 0:
				this.doEffects();
				break;
			case 1:
				this.doShow();
				break;
		}
	},
	
	doEffects: function () {
		if( this.lastElement != null )
			this.effects[ this.lastElement ].custom( 1, 0 );
		this.effects[ this.actElement ].custom( 0, 1 );
		this.nextStage.delay( this.effectTime, this );
	},
	
	doShow: function () {
		this.nextStage.delay( this.showTime, this );
	}
	
} );
