/**
 * Slideshow is a class which can be used to show a slideshow of pictures.
 * @constructor
 * @param {String} targetPicture The ID of the tag in which the slideshow should take place.
 * @param {String} targetCaption The ID of the tag in which the captions should be placed.
 * @param {Boolean} rand Whether The next picture should be picked randomly or the one next in line.
 */
var Slideshow	= function(targetPicture, targetCaption, rand) {
	this.targetPicture	= targetPicture;
	this.targetCaption	= (targetCaption ? targetCaption : targetPicture);
	this.rand			= (rand ? rand : false);
	
	this.speed			= 4000;
	this.duration		= 400;
	
	this.pictures		= new Array();
	this.captions		= new Array();
	
	this.pictures2		= new Array();
	this.index			= 0;
	this.total			= 0;
	
	this.interval;
}

Slideshow.prototype = {
	/**
	 * Add a picture and caption to the Array of slidewhow pictures.
	 * @param {String} picture Source of the image.
	 * @param {String} caption Caption that belongs to the image.
	 */
	add: function(picture, caption) {
		this.pictures[this.total] = (picture ? picture : '');
		this.captions[this.total] = (caption ? caption : '');
		
		this.total++;
	},
	
	/**
	 * Preload all the images added to the Array of images.
	 */
	preload: function() {
		for(i = 0; i < this.total; i++) {
			this.pictures2[i]		= new Image();
			this.pictures2[i].src	= this.pictures[i];
		}
	},
	
	/**
	 * Change the backgroun-image and caption of the target.
	 */
	change: function() {
		var targetPicture	= document.getElementById(this.targetPicture);
		var targetCaption	= document.getElementById(this.targetCaption);
		var index;
		
		if(this.rand) {
			index	= Math.floor(Math.random() * this.pictures2.length);
		}
		else {
			index	= this.index;
		}
		
		if(targetPicture) {
			if(document.all) {
				targetPicture.style.filter	= "blendTrans(duration=2)";
				targetPicture.style.filter	= "blendTrans(duration=this.duration)";
				targetPicture.filters.blendTrans.Apply();
				targetPicture.filters.blendTrans.Play();
			}
			targetPicture.style.backgroundImage		= 'url("' + this.pictures2[index].src + '")';
			targetPicture.style.backgroundRepeat	= 'no-repeat';
		}
		
		if(targetCaption) {
			targetCaption.innerHTML				= this.captions[index];
		}
		
		this.index++;
		
		if(this.index >= this.total) {
			this.index	= 0;
		}
	},
	
	/**
	 * Start the slideshow
	 */
	start: function() {
		this.preload();
		
		var targetPicture	= document.getElementById(this.targetPicture);
		var targetCaption	= document.getElementById(this.targetCaption);
		
		if(targetPicture) {
			targetPicture.style.backgroundImage		= 'url("' + this.pictures2[0].src + '")';
			targetPicture.style.backgroundRepeat	= 'no-repeat';
		}
		
		if(targetCaption) {
			targetCaption.innerHTML				= this.captions[0];
		}
		
		this.index		= 1;
		var self		= this;
		this.interval	= setInterval(function() {
			self.change()
		}, this.speed);
	},
	
	/**
	 * Stop the slideshow.
	 */
	stop: function() {
		clearInterval(this.interval);
	}
}
