/***********************
* A slideshow class for stelladot using 
* scriptoulous and prototype
* Jaime Ruiz July 2009
***************************/
Slideshow = Class.create();
Object.extend(Object.extend(Slideshow.prototype, Abstract.prototype), {
		initialize: function(wrapper){
			this.wrapper  = $(wrapper);
			this.delay = 5000; //in milli
			this.start_frame = 0;
			this.autorun = true;
			
			this.timer = null;
			
			this.lis = this.wrapper.getElementsByTagName('li');
			for( i=0; i < this.lis.length; i++){
				if(i!=0){
					this.lis[i].style.display = 'none';
				}
			}
			this.end_frame = this.lis.length -1;
			this.currentframe = this.start_frame;
			this.start_slideshow(this.start_frame, this.end_frame, this.delay);
		},

		start_slideshow:function(start_frame, end_frame, delay) {
			this.timer = setTimeout(this.fadeInOut(start_frame,start_frame,end_frame, delay).bind(this), delay);
		},


		fadeInOut:function(frame, start_frame, end_frame, delay) {
			return (function() {
				if (this.timer != null) {
					clearTimeout(this.timer);
				}
				this.fading = Effect.Fade(this.lis[frame],{ queue: 'end', duration: 2.5 });
				
				if (frame == end_frame) { frame = start_frame; } else { frame++; }
				lisAppear = this.lis[frame];
				setTimeout("Effect.Appear(lisAppear);", 0);
				//Effect.Appear(lisAppear,{ queue: 'end', duration: 1.0 });
				this.currentframe = frame;
				if(this.autorun){
					this.timer = setTimeout(this.fadeInOut(frame, start_frame, end_frame, delay).bind(this), delay);
				}
			})
	
		},
		
		stop_slideshow:function(){
			if (this.timer != null) {
				clearTimeout(this.timer);
			}
			this.autorun = false;
		},
		
		next:function(){
			this.stop_slideshow();
				
			var frame = this.currentframe;
			
			this.fading = new Effect.Parallel( [ 
					new Effect.Fade( this.lis[ frame ], { sync: true, duration: 2.5 } ),
					new Effect.Appear( this.lis[ ( frame + 1 ) % this.lis.length ], { sync: true, duration: 1.0 } )
				] );
			
			this.currentframe = ( frame + 1 ) % this.lis.length;	
				
		},
		
		prev:function(){
			this.stop_slideshow();
				
			var frame = this.currentframe;

			this.fading = new Effect.Parallel( [ 
					new Effect.Fade( this.lis[ frame ], { sync: true, duration: 2.5 } ),
					new Effect.Appear( this.lis[ ( frame - 1 + this.lis.length ) % this.lis.length ], { sync: true, duration: 1.0 } )
				] );
			
			this.currentframe = ( frame - 1 + this.lis.length ) % this.lis.length;
				
		}
		
});

