/*#################################################

Slideshow | v1.2
Max Felker | max@bigroomstudios.com

Turns a container (full of slides) into a slideshow

#################################################*/

// START Class
var Slideshow = Class.create({

	initialize: function(config) {
	
		if(config) {
		
			config.duration ? this.slide_duration = config.duration: this.slide_duration = 5.0;
			config.autostart ? this.autostart = config.autostart : this.autostart = false;
			config.loop ? this.loop = config.loop : this.loop = false;
			config.reverse ? this.reverse = config.reverse : this.reverse = false;
		
		}
	
		// config
		this.container = $(config.container);
		this.slides = $$('#'+this.container.id+' .slide');
		this.slides_count = this.slides.length;
		this.slideshow_counter = 0;
		this.playing = false;
		this.completed = false;
		
		this.current_slide = this.slides[0];
		this.slides.invoke('hide');
		this.slides[0].show();
		
		if(this.reverse) {
			this.slides[0].hide();
			this.slides[this.slides_count-1].show();
			this.slideshow_counter = this.slides_count;
		}
		
		if(this.autostart) {
			this.start();
		}
		
		this.pause = false;
		
	},
	
	show_given_slide: function(index) {
		if(this.pause)
			return;
		
		if(this.playing) {
			//reset the executer
			this.executer.stop();
			if(this.reverse) {
			
				this.playing = true;
			
				this.executer = new PeriodicalExecuter(function() {
					this.previous_slide();
				}.bind(this),this.slide_duration);
			
			} else {
			
				this.playing = true;
			
				this.executer = new PeriodicalExecuter(function() {
					this.next_slide();
				}.bind(this),this.slide_duration);
			}
		}
		
		//hide the other slides
		this.slideshow_counter = index;
		this.current_slide = this.slides[this.slideshow_counter];
		this.other_slides = this.current_slide.siblings();
		this.other_slides.each(function(slide) {
			if(slide.hasClassName('slide')) {
				Effect.Fade(slide, { duration: 1.0 }); 
				//slide.hide();
				if(slide.bullet_point) {
					slide.bullet_point.removeClassName('current');
				}
			} 
		});
		
		//show the current slide
		if(this.current_slide.bullet_point) {
			this.current_slide.bullet_point.addClassName('current');
		}
		//this.current_slide.show();
		Effect.Appear(this.current_slide, { duration: 1.0 }); 	
		
		this.pause = true;
		var slideshow = this;
		this.pause_executer = new PeriodicalExecuter(function() {
			slideshow.pause = false;
			this.stop();
		}, 1.0);
	},
	
	show_slide: function() {
	
		if(!this.loop && this.slideshow_counter >= this.slides_count) {
			
			this.slideshow_counter = this.slides_count-1;	
	
			this.stop();
				
			return false;
			
		} else if(this.slideshow_counter>=this.slides_count) {	
		
			this.slideshow_counter = 0;	
			
		}
			
		if(!this.loop && this.slideshow_counter<0) {
		
			this.slideshow_counter = 0;	
	
			this.stop();
				
			return false;
			
		} else if(this.slideshow_counter<0) {
		
			this.slideshow_counter = this.slides_count-1;
			
		}
		
		if(this.slideshow_counter==0) {
			this.current_slide = this.slides[0];
		} else {
			this.current_slide = this.slides[this.slideshow_counter];
		}

		this.other_slides = this.current_slide.siblings();
		
		this.other_slides.each(function(slide) {
		
			if(slide.hasClassName('slide')) {
		
				Effect.Fade(slide, { duration: 1.0 }); 
				//slide.hide();
				if(slide.bullet_point) {
					slide.bullet_point.removeClassName('current');
				}
			
			} 
			
		});
		
		if(this.current_slide.bullet_point) {
			this.current_slide.bullet_point.addClassName('current');
		}
		//this.current_slide.show();
		Effect.Appear(this.current_slide, { duration: 1.0 }); 	
		
		this.pause = true;
		var slideshow = this;
		this.pause_executer = new PeriodicalExecuter(function() {
			slideshow.pause = false;
			this.stop();
		}, 1.0);
	},
	
	start: function() {
	
		if(!this.playing) {
		
			if(this.reverse) {
			
				this.playing = true;
			
				this.executer = new PeriodicalExecuter(function() {
					this.previous_slide();
				}.bind(this),this.slide_duration);
			
			} else {
			
				this.playing = true;
			
				this.executer = new PeriodicalExecuter(function() {
					this.next_slide();
				}.bind(this),this.slide_duration);
				
			}
			
		} else {
		
			return false;
		
		}
  
	},
	
	stop: function() {
	
		this.executer.stop();
		this.completed = true;
		this.playing = false;

	},
	
	restart: function() {
	
		if(!this.playing) {
		
			if(this.reverse) {
			
				this.slideshow_counter = this.slides_count;	
				this.previous_slide();
				
			} else {
			
				this.slideshow_counter = -1;
				this.next_slide();
			
			}
			
			this.start();
		
		} else {
		
			return false;
		
		}
	
	},
	
	next_slide: function() {
		if(this.pause)
			return;
		
		this.slideshow_counter++;
		
		this.show_slide();

	},
	
	next_slide_button: function() {
		if(this.pause)
			return;
			
		//reset the executer
		if(this.playing) {
			this.executer.stop();
			
			if(this.reverse) {
		
				this.playing = true;
		
				this.executer = new PeriodicalExecuter(function() {
					this.previous_slide();
				}.bind(this),this.slide_duration);
		
			} else {
		
				this.playing = true;
		
				this.executer = new PeriodicalExecuter(function() {
					this.next_slide();
				}.bind(this),this.slide_duration);
			
			}
		}
		
		//go to the previous slide
		this.next_slide();
	},
	
	
	previous_slide: function() {
		if(this.pause)
			return;
		
		this.slideshow_counter--;
		
		this.show_slide();
		
	},
	
	previous_slide_button: function() {
		if(this.pause)
			return;

		if(this.playing) {
			//reset the executer
			this.executer.stop();
			if(this.reverse) {
			
				this.playing = true;
			
				this.executer = new PeriodicalExecuter(function() {
					this.previous_slide();
				}.bind(this),this.slide_duration);
			
			} else {
			
				this.playing = true;
			
				this.executer = new PeriodicalExecuter(function() {
					this.next_slide();
				}.bind(this),this.slide_duration);
				
			}
		}
		
		//go to the previous slide
		this.previous_slide();
	}
	

});
// END CLass

