var Homeslider = new function(){
	
	this.slideSpeed = 2000;
	this.slideTimeout = 5000;
	
	this.slides = [];
	this.numSlides = 0;
	this.timer;
	this.buttons = [];
	
	this.init = function( slides ){
		if( slides.length > 0 ){
			slides.each( function( elm , index ){
				
				var image = ( typeof( elm.image ) == 'string' ) ? elm.image : '' ;
				var text = ( typeof( elm.text ) == 'string' ) ? elm.text : '' ;
				
				this.slides.push( { 'image' : image , 'text' : text } );
				
			}.bind( this ) );
			this.numSlides = this.slides.length;
			
			this.initNavigation();
			
			this.next( 0 );
		}
	};
	
	this.initNavigation = function(){
		if( $( 'imageSlideContainer' ) ){
			var c = $( 'imageSlideContainer' );
			c.setStyle( 'position' , 'relative' );
			
			var btnContainer = new Element( 'div' ).inject( c );
			btnContainer.setStyles( {
				'position':'absolute',
				'left':'690px',
				'bottom':'10px',
				'z-index':'30'
			} );
			
			for( i = 0; i < this.numSlides; i++ ){
				var btn = new Element( 'a' ).addEvent( 'click' , function( e ){
					e.stop();
					Homeslider.next( this.id.replace( 'hdl_' , '' ) );
				} ).inject( btnContainer );
				
				// IE
				btn.id = 'hdl_' + i;
				btn.innerHTML = ( i + 1 );
				btn.className = 'slidehandle';
				
				this.buttons.push( btn );
			}
			
			
		}
	};
	
	this.previousSlide = null;
	
	this.next = function( index ){
		clearTimeout( this.timer );
		
		
		
		if( this.previousSlide != null ){
			this.previousSlide.className = 'previousSlide';
		}
		
		
		
		var next_index = ( typeof( this.slides[ ( index + 1 ) ] ) != 'undefined' ) ? ( index + 1 ) : 0 ;
		var s = this.slides[ index ];
		
		// Setup the slide
		var slide = new Element( 'div' ).inject( $( 'imageSlideContainer' ) ); slide.setStyle( 'opacity' , 0 ); slide.id = 'slide' + index; slide.className = 'currentSlide';
		
		var slidePhoto = new Element( 'div' ).inject( slide ); slidePhoto.className = 'imageSlider';
		var slideInfo = new Element( 'div' ).inject( slide ); slideInfo.className = 'imageInfo';
		
		// Filling ..
	    slidePhoto.setStyle( 'background' , 'url( ' + s.image + ' )' );
	    slideInfo.innerHTML = s.text;
	    
	    this.buttons.each( function( elm , index ){ elm.className = 'slidehandle'; } );
	    if( $( 'hdl_' + index ) ) $( 'hdl_' + index ).className = 'slidehandle active';
	    
	    // Fade in this slide
	    new Fx.Tween( 'slide' + index , {
	        duration: this.slideSpeed,
	        link: 'chain',
	        property: 'opacity',
	        onComplete : function(){
	        
		        // Destroy previous slide after this one has shown
		        if( this.previousSlide != null ){
		        	//console.log( this.previousSlide );
		        	this.previousSlide.destroy();
		        	this.previousSlide = null;
		        }
		        
		        // This will be the next previousSlide
		        this.previousSlide = $( 'slide' + index );
		        
	        	this.timer = setTimeout( function(){ this.next( next_index ) }.bind( this ) , this.slideTimeout );
	       		
	       		return true;
	        }.bind( this )
	    }).start(0, 1);
		
		
		// <div id="imageSlider"></div><div id="imageInfo"></div>
		
	};
	
	/*
	this.initNavigation = function(){
		if( $( 'imageSlideContainer' ) ){
			var c = $( 'imageSlideContainer' );
			c.setStyle( 'position' , 'relative' );
			var btnContainer = new Element( 'div' ).inject( c );
			btnContainer.setStyles( {
				'position':'absolute',
				'left':'690px',
				'bottom':'10px'
			} );
			
			for( i = 0; i < this.numSlides; i++ ){
				var btn = new Element( 'a' ).addEvent( 'click' , function( e ){
					e.stop();
					Homeslider.next( this.id.replace( 'hdl_' , '' ) );
				} ).inject( btnContainer );
				
				// IE
				btn.id = 'hdl_' + i;
				btn.innerHTML = ( i + 1 );
				btn.className = 'slidehandle';
				
				this.buttons.push( btn );
			}
			
		}
	};
	
	this.next = function( index ){
		clearTimeout( this.timer );
		var next_index = ( typeof( this.slides[ ( index + 1 ) ] ) != 'undefined' ) ? ( index + 1 ) : 0 ;
		var s = this.slides[ index ];
		
		if( $( 'imageSlideContainer' ) && $( 'imageSlider' ) && $( 'imageInfo' ) ){
			
			new Fx.Tween( 'imageSlideContainer' , {
			    duration: this.slideSpeed,
			    link: 'chain',
			    property: 'opacity',
			    onComplete : function(){
			    	
			    	this.buttons.each( function( elm , index ){ elm.className = 'slidehandle'; } );
			    
				    $( 'imageSlider' ).setStyle( 'background' , 'url( ' + s.image + ' )' );
				    $( 'imageInfo' ).innerHTML = s.text;
				    
				    if( $( 'hdl_' + index ) ) $( 'hdl_' + index ).className = 'slidehandle active';
				    
					new Fx.Tween( 'imageSlideContainer' , {
					    duration: this.slideSpeed,
					    link: 'chain',
					    property: 'opacity',
					    onComplete : function(){
					    	this.timer = setTimeout( function(){ this.next( next_index ) }.bind( this ) , this.slideTimeout );
					   		return true;
					    }.bind( this )
					}).start(0, 1);
				    
			    }.bind( this )
			}).start(1, 0);
		}
		
	};
	*/
	
}
