// parameters for the slideshow object: id, name (= variable name), delay between slides in milliseconds, start slideshow onload (true/false), and fade transition speed (we recommend that this number not be edited b/c slower increments can affect cpu performance)

function Slideshow(id,name,delay,autoplay,increment,transition) {

	this.id = getObject(id);
	this.name = name;
	
	// decrease increment to go slower (cannot be less than 1)
	// slower slideshows have a greater impact on CPU usage
	this.increment = 5;
	if (increment) {
		this.increment = increment;
	}
	
	// default transition fades slides in/out
	this.transition = 'fade';
	
	// if we're not using Opera, get an alternative transition if provided
	if (!window.opera) { 
		if (transition) {
			this.transition = transition;
		}
	}
	else {
		this.transition = 'flip';
	}
	
	this.currentSlide = 0;
	this.nextSlide = 0;
	
	this.slides = getChildren(id);
	this.slideCount = this.slides.length;
	this.slideObj = new Array(this.slideCount);	
	
	// convert the slides into effect objects
	for (var i = 0; i < this.slideCount; i++) {
		this.slideObj[i] = new Effect(this.slides[i].id, { increment:this.increment });
	}
	
	// show the first slide
	this.slideObj[0].show();
	this.slideObj[0].fadeIn(); // not supported by Opera, but doesn't throw errors
	
	this.ssGoToSlide = ssFindSlide;
	
	this.delay = delay;
	this.defaultDelay = delay;
	this.timeout = false;
	
	if (autoplay) {
		this.timeout = setTimeout(this.name + '.ssPlay()', this.delay);
	}	
}

// ----- INDIVIDUAL SLIDE METHODS	
// because the slides are also Effect objects, we can leverage Effect methods like fadeIn, fadeOut, etc.
// the methods set here are specific to the slideshow:

Effect.prototype.ssOnStart = function() {
	if (hasAttr(this.id,'action')) {
		var doAction = hasAttr(this.id,'action');		
		var doActionDelay = 0;	
		if (hasAttr(this.id,'actDelay')) {
			doActionDelay = hasAttr(this.id,'actDelay') * 1000;
		}
		setTimeout(doAction, doActionDelay);
	}
}	

Effect.prototype.ssSetDelay = function() {
	if (hasAttr(this.id,'delay')) {
		return hasAttr(this.id,'delay') * 1000;			
	}
	else {
		return null;
	}
}

// END INDIVIDUAL SLIDE METHODS

// ----- SLIDESHOW METHODS	

Slideshow.prototype.ssGoToNext = function() {
	this.ssPause();
	this.ssPlay();
}

Slideshow.prototype.ssGoToPrevious = function() {
	this.ssPause();
	if (this.currentSlide > 0){
		this.nextSlide = this.currentSlide - 1;
	}
	else {
		this.nextSlide = this.slideCount - 1;
	}		
	this.ssTransition();
}

Slideshow.prototype.ssGoToFirst = function() {
	this.ssPause();
	this.nextSlide = 0;
	this.ssTransition();
}

Slideshow.prototype.ssGoToLast = function() {
	this.ssPause();
	this.nextSlide = this.slideCount - 1;		
	this.ssTransition();
}

function ssFindSlide(number) {
	this.ssPause();
	if (number) {
		this.nextSlide = number - 1;
	}
	this.ssTransition();
}

Slideshow.prototype.ssPause = function() {
	if (this.timeout) {
		clearTimeout(this.timeout);
		this.timeout = false;
	}
}

Slideshow.prototype.ssPlay = function() {
	// find next slide by testing for end of array
	if (this.currentSlide + 1 < this.slideCount){
		this.nextSlide = this.currentSlide + 1;
	}
	else {
		this.nextSlide = 0;
	}		
	this.ssTransition();
}

Slideshow.prototype.ssTransition = function() {		
	// reorder the slides so that the next slide is on top
	this.slides[this.currentSlide].style.zIndex = 0;
	this.slides[this.nextSlide].style.zIndex = 99;
	
	this.slideObj[this.nextSlide].show();
			
	switch (this.transition) {		
		case 'fade':	
			this.slideObj[this.nextSlide].fadeIn();	
			this.slideObj[this.currentSlide].fadeOut();
			break;	
						
		case 'flip':
			this.slideObj[this.currentSlide].hide();
			break;		
	}
	
	this.slideObj[this.nextSlide].ssOnStart();
	
	if (this.slideObj[this.nextSlide].ssSetDelay() != null) {
		this.delay = this.slideObj[this.nextSlide].ssSetDelay();
	}
	else {
		this.delay = this.defaultDelay;
	}
	
	// Optional - highlight control links
	// this.ssHighlightControl();
	
	this.currentSlide = this.nextSlide;		
	this.timeout = setTimeout(this.name + '.ssPlay()', this.delay);		
}

Slideshow.prototype.ssHighlightControl = function() {
	this.controlPanel = getObject(this.id.id + 'Controls');
	this.ctrls = getTags(this.controlPanel,'a');
	
	for (var i = 0; i < this.ctrls.length; i++) {
		removeClass(this.ctrls[i],'ctrlOn');
		
		if (i == this.nextSlide) {
			addClass(this.ctrls[i],'ctrlOn');
		}
	}
}