/*#################################################

Slideshow | v1.3
Max Felker | max@bigroomstudios.com
Brian Leighton | brian@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.hover_pause ? this.hover_pause = config.hover_pause : this.hover_pause = false;
		
		}

		// config
		this.container = $(config.container);
		//this.slides = $$('#'+this.container.id+' .slide');
		
		this.items = [];
		this.transitions = [];
		this.distance = [];
		this.transition_duration = [];
		
		for(var i=0; i<config.items.length; i++) {
			this.items[i] = $$('#'+this.container.id+' .'+config.items[i].className);
			this.transitions[i] = config.items[i].transition;
			this.distance[i] = config.items[i].distance;
			this.transition_duration[i] = config.items[i].transition_duration;
		}
		
		if(config.tracker) {
			this.tracking = $$('#'+this.container.id+' .'+config.tracker);
		}
		else {
			this.tracking = null;
		}
		
		//number of slides in each item
		this.items_count = this.items[0].length;
		//set the current item
		//this.reverse ? this.slideshow_counter = this.slides_count-1 : this.slideshow_counter = 0;
		if(this.reverse) {
			this.current_item = this.slides_count-1;
		}
		else {
			this.current_item = 0;
		}
		
		this.playing = false;
		this.completed = false;
		this.next_item = null;
		
		if(this.hover_pause) {

			this.container.observe('mouseenter', function() {
				this.stop();
			}.bind(this));
			
			this.container.observe('mouseleave', function() {
				this.start();
			}.bind(this));
				
		}
		
		for(var i=0; i<this.items.length; i++) {
		
			switch(this.transitions[i]) {
			
				case 'fade':
					this.items[i].invoke('hide');
					
					break;
				case 'slide':
					
			}
			
		}
		
		//show and store the current slide
		//this.current_slide = this.slides[this.slideshow_counter];
		//this.current_slide.show();
		this.items.each(function(item) {
			item[this.current_item].show();
		}.bind(this));
		
		if(this.tracking) {
			this.tracking[this.current_item].addClassName('current');
		}
		
		if(this.autostart) {
			this.start();
			this.show_slide();
		}
		
	},
	
	//hides the current item and shows the next item
	show_slide: function() {
		
		if(this.next_item == null) {
			return;
		}
				
		if(!this.loop && this.current_item == this.items_count-1 && !this.reverse) {
			this.stop();
			return false;

		}
			
		if(!this.loop && this.current_item == 0 && this.reverse) {
			this.stop();
			return false;
			
		}
		
		if(this.current_item != this.next_item) {
		
			for(var i=0; i<this.items.length; i++) {
		
				switch(this.transitions[i]) {
			
					case 'fade':
						//this.items[i][this.current_item].fade(1, {queue: {position: 'end', scope: 'queue_'+this.current_item, limit: 2}});
						new Effect.Fade(this.items[i][this.current_item].id, {duration: 1, queue: {position: 'end', scope: 'queue_'+this.current_item, limit: 2}});
						
						//this.items[i][this.next_item].appear(1, {queue: {position: 'end', scope: 'queue_'+this.next_item, limit: 2}});
						new Effect.Appear(this.items[i][this.next_item].id, {duration: 1, queue: {position: 'end', scope: 'queue_'+this.next_item, limit: 2}});
					
						break;
					
					case 'slide_horizontal':
						//calculate the parameters to the sliding based on the direction of the slide
						if(this.next_item > this.current_item) {
							var next_item_pos = this.distance[i];
							var slide_distance = this.distance[i] * -1;
						}
						else {
							var next_item_pos = (this.distance[i] + 1) * -1;
							var slide_distance = (this.distance[i] + 1);
						}
						
						//position the new item
						this.items[i][this.next_item].setStyle({left:next_item_pos + 'px'});
					
						//slide both items
						new Effect.Move(this.items[i][this.current_item], {x:slide_distance, y:0, duration: this.transition_duration[i]});
						new Effect.Move(this.items[i][this.next_item], {x:slide_distance, y:0, duration: this.transition_duration[i]});
					
						break;
					
					case 'slide_vertical':
						//position the new item
						this.items[i][this.next_item].setStyle({top:(this.distance[i]*-1) + 'px'});
					
						//slide both items
						new Effect.Move(this.items[i][this.current_item], {x:0, y:this.distance[i], duration: this.transition_duration[i]});
						new Effect.Move(this.items[i][this.next_item], {x:0, y:this.distance[i], duration: this.transition_duration[i]});
					
						break;
					
					default:
						//hide the current item
						this.items[i][this.current_item].hide();
			
						//show the new item
						this.items[i][this.next_item].show();
			
				}
				
			}
			
		}
		
		if(this.tracking) {
			this.tracking[this.current_item].removeClassName('current');
			this.tracking[this.next_item].addClassName('current');
		}
		this.current_item = this.next_item;
		
	},
	
	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);
				
			}
			
			this.completed = false;
			
			//this.slides.invoke('hide');
			
		} else {
		
			return false;
		
		}
  
	},
	
	stop: function() {
		if(this.executer) {
			this.executer.stop();
		}
		this.completed = true;
		this.playing = false;

	},
	
	/*
		starts a slideshow if it is not playing
	*/
	restart: function() {
	
		if(!this.playing) {

			this.reset();
			
			this.start();
		} else {
			return false;
		}
	},
	
	/*
		resets the slideshow
	*/
	reset: function() {
		this.stop();
		
		//set the slideshow_position
		if(this.reverse) {
			this.current_item = this.slides_count-1;
			this.next_item = this.slides_count-2;
		}
		else {
			this.current_item = 0;
			this.next_item = 1;
		}
		
		this.show_slide();
	},
	
	next_slide: function() {
		
		this.next_item = (this.current_item + this.items_count + 1) % this.items_count;
		
		this.show_slide();

	},
	
	previous_slide: function() {
		
		this.next_item = (this.current_item + this.items_count - 1) % this.items_count;
		
		this.show_slide();
		
	},
	
	reset_executer: function() {
	
		this.executer.stop();
		
		if(this.reverse) {
		
			this.executer = new PeriodicalExecuter(function() {
				this.previous_slide();
			}.bind(this),this.slide_duration);
		
		} else {
		
			this.executer = new PeriodicalExecuter(function() {
				this.next_slide();
			}.bind(this),this.slide_duration);
				
		}
	}
});
// END CLass

