// Slideshow for scriptaculous.
// v0.2

var Slideshow = Class.create();

Slideshow.prototype = {

	// Public: Sole constructor.
	//
	// wrapper      - The String id name of the wrapping div.
	// effect_time  - The Float time of the effect (i.e the transition) in 
	//                seconds
	// delay        - Optional. The Integer number of seconds beetwen two
	//                transitions. Default value is 5 seconds. Only needed if 
	//                you plan to use the start() method.
	//
	// Example:
	//
	//   var my_slide = new Slideshow("slider", 1.0);
	//
	initialize: function(wrapper, effect_time, delay) {
		this.wrapper = wrapper;
		this.frame = 1;
		this.start_frame = 1;
		var elems = $$('#' + this.wrapper + ' .slider');
		this.end_frame = elems.size();
		for(var i = 0; i < elems.length; i++){
			var content = elems[i].innerHTML;
			if(i == 0) {
				elems[i].replace('<div class="slider" id="' + this.wrapper 
										+ (i+1) + '">' + content + '</div>');
			} else {
				elems[i].replace('<div class="slider" id="' + this.wrapper 
					+ (i+1) + '" style="display:none;">' + content + '</div>');
			}
		}
		this.effect_time = effect_time;
		if ( delay === undefined ) {
			this.transition_time = 5;
		} else {
			this.transition_time = delay;
		}
	},
	
	// Public: Start the automated slideshow.
	//
	// Example:
	//
	//   my_slide.start();
	// 
	// Returns nothing.
	start: function() {
		this.executor = new PeriodicalExecuter(function() {
			this.next();
		}.bind(this), this.transition_time); 
	},
	
	// Public: Stop the automated slideshow.
	//
	// Example:
	//
	//   my_slide.stop();
	// 
	// Returns nothing.
	stop: function() {
		this.executor.stop();
	},

	// Public: Show the next element of the slideshow with a transition effect.
	//
	// Example:
	//
	//   my_slide.next();
	// 
	// Returns nothing.
	next: function() {
		this.fade();
		this.nextFrame();
		this.appear();
	},
	
	// Public: Show the previous element of the slideshow with a transition 
	// effect.
	//
	// Example:
	//
	//   my_slide.previous();
	//
	// Returns nothing.
	previous: function() {
		this.fade();
		this.previousFrame();
		this.appear();
	},
	
	// Increment the internal frame counter.
	//
	// Returns nothing.
	nextFrame: function() {
		if (this.frame == this.end_frame) { 
			this.frame = this.start_frame; 
		} else { 
			this.frame++; 
		}
	},
	
	// Decrement the internal frame counter.
	//
	// Returns nothing.
	previousFrame: function() {
		this.frame--;
		if(this.frame < 1) {
			this.frame = this.end_frame;
		}
	},
	
	// Apply a fade effect to the current element.
	//
	// Returns nothing.
	fade: function() {
		Effect.Fade(this.wrapper + this.frame, { duration: this.effect_time });
	},
	
	// Apply an appear effect to the current element.
	//
	// Returns nothing.
	appear: function() {
		Effect.Appear(this.wrapper + this.frame, { duration: this.effect_time });
	}
	
};
