/* Nice Slide Show Lite */
var NiceSlideShowLite = new Class( {
	options: {
		showControls: false,
		showDuration: 3000,
		showCaption: false,
		captionContainer: 'niceslides-caption-0',
		captionOpacity: 0.7,
		nextContainer: 'niceslides-next-0',
		previousContainer: 'niceslides-previous-0'
	},
	Implements: [Options,Events],
	initialize: function( container,elements,options ) {
		
		this.setOptions( options );
		this.container = $( container );
		this.elements = $$( elements );
		this.currentIndex = 0;
		this.interval = '';
		if( this.options.showCaption ) this.createCaption(  );
		var capHieght = 0;
	
		if( this.options.showControls ) {
			this.createControls(  );			
		}
		
		this.container.addEvents( {
			mouseenter: function() { this.stop(  ); }.bind( this ),
			mouseleave: function() { this.start(  ); }.bind( this )
		} );

	},
	
	show: function(to) {
		
		this.elements[this.currentIndex].fade( 'out' );
		this.elements[this.currentIndex = ( $defined( to ) ? to : ( this.currentIndex < this.elements.length - 1 ? this.currentIndex+1 : 0 ) )].fade( 'in' );
		
		if( this.options.showCaption ) this.doCaptionFX(  );
		
	},
	
	start: function() {
		if( this.options.showCaption ) this.doCaptionFX(  );
		this.interval = this.show.bind( this ).periodical( this.options.showDuration );
	},
	
	stop: function() {
		$clear( this.interval );
	},

	createControls: function() {
		var next = new Element( 'a',{
			href: '#',
			id: this.options.nextContainer,
			events: {
				click: function( e ) {
					if( e ) e.stop(  );
					this.stop(  ); 
					this.show(  );
				}.bind( this )
			}
		} ).inject( this.container );
		var previous = new Element( 'a',{
			href: '#',
			id: this.options.previousContainer,
			events: {
				click: function( e ) {
					if( e ) e.stop(  );
					this.stop(  ); 
					this.show( this.currentIndex != 0 ? this.currentIndex -1 : this.elements.length-1 );
				}.bind( this )
			}
		} ).inject( this.container );
	},
	
	createCaption: function() {
		var captionElement = new Element( 'div',{
			id: this.options.captionContainer,
			styles: {
				opacity: this.options.captionOpacity
			}
			
		} ).inject( this.container );
		
		var capSize = captionElement.getSize(  );
		capHieght = capSize.y;
		captionElement.setStyle( 'height',0 );
	},

	doCaptionFX: function(element) {

		var title = '';
		var captionText = '';
		var captionbox = this.options.captionContainer;

		if( this.elements[this.currentIndex].get( 'alt' ) ) {
			var cap = this.elements[this.currentIndex].get( 'alt' ).split( '::' );
			title = cap[0];
			captionText = cap[1];
			}

		$( captionbox ).set( 'tween',{
			onComplete: function() {
				$( captionbox ).set( 'tween',{
					onComplete: $empty
				} ).tween( 'height',capHieght );
				$( captionbox ).set( 'html','<h3>' + title + '</h3>' + ( captionText ? '<p>' + captionText + '</p>' : '' ) );
				
			}
		} ).tween( 'height',0 );
	}

} );
