/**
 * This file handles the SlideShow.
 * 
 * @version 13-01-2010
 * @author <a href="mailto:r.tennapel@griponservice.nl?SUBJECT=Amasus SlideShow script.js">R. ten Napel, ing.</a>
 **/

/**
 * A slide used in the slideshow.
 * 
 * @param source		The image source.
 * @param caption		The caption that tells about the image.
 * @param url			The URL that is opened when clicking the image.
 **/
function Slide(source, caption, url) {
	this.source = source;
	this.caption = caption;

}

/**
 * Show the slide.
 * 
 * @param divImg		The div-holder for the slides.
 * @param divCaption	The div-holder for the captions.
 **/
Slide.prototype.show = function(divImg, divCaption) {
	if (divImg == null || divCaption == null) {
//		alert("No slideshow holders!");
		
		return;
	}
	
	divImg.src = this.source;
	
	

	divCaption.innerHTML = this.caption;
}

/**
 * A slideshow which updates images.
 * 
 * @param divImg		The div-holder for the slides.
 * @param divCaption	The div-holder for the captions.
 * @param time			The time in ms to elapse for the next image.
 **/
function SlideShow(divImg, divCaption, time) {
	this.slides = new Array();
	this.current = -1;
	this.interval = null;
	
	// Set the used div-blocks where the slides and captions should be placed:
	this.divImg = divImg;
	this.divCaption = divCaption;
	
	// Set default time to 5000 ms:
	this.time = (time == null) ? 5000 : time;
}

/**
 * Add a slide to the slideshow.
 * 
 * @param source		The image source.
 * @param caption		The caption that tells about the image.
 * @param url			The URL that is opened when clicking the image.
 **/
SlideShow.prototype.addSlide = function(source, caption, url) {
	this.slides[this.slides.length] = new Slide(source, caption, url);
	
	// Set the current image index to the first image and show it:
	if (this.current == -1) {
		this.current = 0;
		this.update();
	}
}

/**
 * Update the slideshow (show the slide in the div-holder).
 **/
SlideShow.prototype.update = function() {
	var divImg = this.divImg;
	var divCaption = this.divCaption;
	
	// Retrieve the slide:
	var slide = this.slides[this.current];
	
	// Show the slide:
	slide.show(divImg, divCaption);
}

/**
 * Show the previous slide.
 **/
SlideShow.prototype.previous = function() {
	this.current--;
	
	// Check if current index is out of range:
	if (this.current < 0) {
		this.current = this.slides.length - 1;
	}
	
	this.update();
}


/**
 * Show next slide after slide with the given time in ms as the interval. When no time is given, the internal time will be used.
 * 
 * @param time			The slide interval in ms.
 **/
SlideShow.prototype.play = function(time) {
	// If no time is set, use the internal one:
	time = (time == null) ? this.time : time;
	
	// Set the off pause image:
	//el = (document.getElementById("slideshow_onpause") == null) ? document.getElementById("slideshow_pause") : document.getElementById("slideshow_onpause");
	//if (!el) return false;
	//el.id = "slideshow_pause";
	
	// Set the on play image:
	//el = (document.getElementById("slideshow_onplay") == null) ? document.getElementById("slideshow_play") : document.getElementById("slideshow_onplay");
	//el.id = "slideshow_onplay";
	
	this.interval = setInterval("SlideShow.instance.next();", time);
}

/**
 * Pause the slideshow (clear the interval).
 **/
SlideShow.prototype.pause = function() {
	clearInterval(this.interval);
	
	// Set the off play image:
	el = (document.getElementById("slideshow_onplay") == null) ? document.getElementById("slideshow_play") : document.getElementById("slideshow_onplay");
	el.id = "slideshow_play";
	
	// Set the on pause image:
	el = (document.getElementById("slideshow_onpause") == null) ? document.getElementById("slideshow_pause") : document.getElementById("slideshow_onpause");
	el.id = "slideshow_onpause";
}

/**
 * Show the next slide.
 **/
SlideShow.prototype.next = function() {
	this.current++;
	
	// Check if current index is out of range:
	if (this.current >= this.slides.length) {
		this.current = 0;
	}
	
	this.update();
}


