// JavaScript Document
var slideshow = {
	photos: Array(),
	photo_index: 0,
	delay: 5,
	div: 'image',
	uniqueID: 'photo',
	REQUIRED_PROTOTYPE: '1.5.2',
	check: function(){
		function convertVersionString(versionString){
			var r = versionString.split('.');
			return parseInt(r[0])*100000 + parseInt(r[1])*1000 + parseInt(r[2]);
		}
		if((typeof Scriptaculous=='undefined') || 
	       (typeof Element == 'undefined') || 
	       (typeof Element.Methods=='undefined') ||
	       (convertVersionString(Scriptaculous.Version) < convertVersionString(slideshow.REQUIRED_PROTOTYPE)))
	       throw("Slideshow maakt gebruik van het Scriptaculous JavaScript Framework >= " + slideshow.REQUIRED_PROTOTYPE);
	},
	startup: function(div){
		slideshow.check();
		slideshow.div = div;
		var d = new Date();
		slideshow.uniqueID = d.getTime().toString;
		var imagediv = document.getElementById(slideshow.div);
		var images = imagediv.getElementsByTagName('img');
		var height = 0;
		for(var i=0; i<images.length; i++){
			if(images[i].attributes['src']){
				images[i].id = slideshow.uniqueID + '_' + i;
				if(height < images[i].offsetHeight) height = images[i].offsetHeight;
				if(i>0)images[i].style.display = 'none';
				slideshow.photos.push({0:images[i].attributes['src'].value, 1:images[i].id});
			}
			if(height>0) document.getElementById(slideshow.div).style.height = height + 20 + 'px';
		}
		if(images.length>1){
			slideshow.update_photoindex();
			new PeriodicalExecuter(slideshow.cycle3, slideshow.delay);
		}else if(slideshow.photos > 0){
			new PeriodicalExecuter(slideshow.cycle1, slideshow.delay);
		}
	},
	update_photoindex: function(){
		if(slideshow.photo_index < slideshow.photos.length-1)
			slideshow.photo_index++;
		else
			slideshow.photo_index = 0;
	},
	previous_photoindex: function(x){
		if(x <= 0)
			return slideshow.photos.length-1;
		else
			return x-1;
	},
	cycle1: function(){
		new Effect.Fade(slideshow.div, {duration: 1, fps: 50, 
				afterFinish: function(){
					new Ajax.Updater(slideshow.div,slideshow.photos[slideshow.photo_index], {asynchronous: true,
							onSuccess: function(){slideshow.update_photoindex();new Effect.Appear(slideshow.div, {duration: 1, fps: 50, queue:'end'});}});
				}
	    });
	},
	cycle2: function(){
		document.getElementById(slideshow.photos[slideshow.previous_photoindex(slideshow.photo_index)][1]).style.display='none';
		document.getElementById(slideshow.photos[slideshow.photo_index][1]).style.display='';
		slideshow.update_photoindex();
	},
	cycle3: function(){
		new Effect.Fade(slideshow.div, {duration: 1, fps: 50, 
				afterFinish: function(){
					document.getElementById(slideshow.photos[slideshow.previous_photoindex(slideshow.photo_index)][1]).style.display='none';
					document.getElementById(slideshow.photos[slideshow.photo_index][1]).style.display='';
					slideshow.update_photoindex();
					new Effect.Appear(slideshow.div, {duration: 1, fps: 50, queue:'end'});
				}
	    });
	}
} 
