/**
* SlideShow
*
* @author Terence Hersbach
* @copyright Beeldspraak BV
*/

var slideShow = {
	imageCount : 0,
	images : [],
	links  : new Array(),

	currentShowImage : 0,
	currentPreImage  : 1,

	timeOut : 6000,
	opc		: 1,

	showDiv : 'showDiv',
	preDiv  : 'preDiv',

	addImage : function(src,link) {
		this.images[this.imageCount] = src;
		this.links[this.imageCount] = link;

		this.imageCount++;
	},

	fade : function() {

		if(this.opcaction == 'down') {
			switch(this.opc)
			{
				case this.opc > 0.7 && this.opc < 1 :
				step = 0.9;
				break;
				case this.opc > 0.5 && this.opc < 0.7:
				step = 0.75;
				break;
				default:
				step = 0.6;
				break;
			}
		} else {
			switch(this.opc)
			{
				case this.opc > 0 && this.opc < 0.3 :
				step = 1.9;
				break;
				case this.opc > 0.4 && this.opc < 0.7:
				step = 1.75;
				break;
				default:
				if(this.opc == 0) {
					this.opc = 0.1;
				}
				step = 1.3;
				break;
			}
		}

		this.opc = this.opc * step;

		if(this.opc <= 0 && this.opcaction == 'down') {
			$(this.showDiv).style.opacity = 0;// $(this.showDiv).setOpacity(0);
			this.opcstatus = 'finished';
			this.opc = 0;
		} else if(this.opc >= 1 && this.opcaction == 'up') {
			$(this.showDiv).style.opacity = 1;// $(this.showDiv).setOpacity(1);
			this.opc = 1;
			this.opcstatus = 'finished';
		} else {
			$(this.showDiv).style.opacity = this.opc;// $(this.showDiv).setOpacity(this.opc);
			this.opcstatus = 'busy';

			if(this.opcstatus == 'busy' && this.opc > 0 && this.opc < 1) {

				setTimeout("slideShow.fade();",100);
			}
		}


	},

	opc : 1.0,
	opcstatus : 'sleep',
	opcaction : 'down',

	checkForNextAction : function() {
		if(this.opcaction == 'down') {
			if(this.opc <= 0.03) {

				$(this.showDiv).innerHTML = '<a href="' + this.links[this.currentShowImage] + '"  onclick="this.target=\'blank\'"><img src="' + this.images[this.currentShowImage] + '" alt="" id="SlideShowShowImage' + this.currentShowImage + '"  /></a>';

				this.fixtocenter();

				this.opcstatus = 'busy';
				this.opc = 0.0;
				this.opcaction = 'up';

				//this.fade('up');
			} else {
				setTimeout("slideShow.checkForNextAction()",100);
			}
		}
		//console.info('checkForNextAction()');
	},

	fixtocenter : function() {
		//place the image in the center of the box
		imageheight = $('SlideShowShowImage' + this.currentShowImage).height;//getHeight()
		divheight = $(this.showDiv).height ? $(this.showDiv).height : 100;//getHeight();
		$(this.showDiv).style.padding = Math.round((divheight / 2) - (imageheight / 2)) + 'px 5px 5px 5px';
		$(this.showDiv).style.height = Math.floor(divheight - ((divheight / 2) - (imageheight / 2) ) - 5) + 'px';
	},

	putInShowDiv : function() {

		this.opc = 1.0;
		this.opcaction = 'down';
		this.opcstatus = 'busy';
		this.fade('down');
		this.checkForNextAction();
	},

	putInPreDiv : function(src) {
		$(this.preDiv).innerHTML = '<img src="' + this.images[this.currentPreImage] + '" alt="" id="SlideShowpreImage" />';
	},

	getNextImageId : function(number) {

		if((number + 1) > (this.images.length - 1)) {
			number = -1;

		} else {
			if(typeof this.images[(number + 1)] == 'undefined') {
				number = -1;
			}
		}

		return (number + 1);
	},

	setNextImages : function() {
		this.currentShowImage	= this.getNextImageId(this.currentShowImage);
		this.currentPreImage 	= this.getNextImageId(this.currentPreImage);

		this.putInShowDiv();
		this.putInPreDiv();

	},

	start : function() {
		slideShow.setNextImages();
	},

	init : function () {
		slideShow.fixtocenter();
		slideShow.start.periodical(slideShow.timeOut);
	}
}

window.addEvent('load', function() {
	slideShow.init();
});

