/*#######################################################################################################
Program written by Dominik Kressler
Project Name: 	simpleSlide
Version: 		1.0
Date: 			2010-07-19
#########################################################################################################
#########################################################################################################
REQUIREMTENS:
	Libraries:
		Prototype: 		1.6.0.2
		Scriptaculous: 	1.8.1
#########################################################################################################
########################################################################################CONFIGURATION: */

var container = "slideshow";//The parent Object. e.g. an undefined list.
var slideDuration = 2;// Duration of each effect in seconds
var delayTime = 5;// Delay of effect-initializing in seconds
var useTitle = false;// Use the image title attribute to display an overlayed description
var animateTitle = false;
var randomize = false;
/*# CONFIGURATION END ###########*/

//INTERNAL VARIABLES
var p = 0;
var c = 0;
var n = 0;
var imageArray = Array();
var started = false;
var title = String;
var titleHeight = Number;
Event.observe(window, 'load', function() 
{ 
	if(randomize)
	{
		myArray = $(container).childElements();
		myArray.each(function(elm)
		{
			elm.remove();
		});
		shuffle(myArray);
		myArray.each(function(elm, indexOf){
			$(container).insert(elm, { position: content })
		});	
	}
	$(container).childElements().each(function(image) {
		if(p != 0) image.setStyle({display:'none'});
		imageArray[imageArray.length] = image;
		if(useTitle)
		{
			title = image.descendants();
			title = $(title[0]).readAttribute('title')
			image.insert(new Element("span", { id: "title"+parseInt(p+1) }));
			$("title"+parseInt(p+1)).innerHTML = title;
			titleHeight = $("title"+parseInt(p+1)).getHeight();
			if(p > 0)$("title"+parseInt(p+1)).setStyle({top:-titleHeight+"px"});
		}
		p++;
	});
	if(p == imageArray.length) slideshow1 = new slideshow();
});
var slideshow = Class.create({
	initialize:function() 
	{		
		realDelay = (!started) ? delayTime : delayTime + slideDuration ;
		new PeriodicalExecuter(function() {						
			if(c == imageArray.length) c = 0;
			n = (c == imageArray.length - 1) ? 0 : c + 1 ;
			new Effect.Parallel([
				new Effect.Appear(imageArray[n], 
				{ 
					sync: true,
					beforeStart: showTitle(imageArray[n])
				}),
				new Effect.Fade(imageArray[c], 
				{ 
					sync: true,
					beforeStart: hideTitle(imageArray[c])
				})
			], { 
				duration: slideDuration
			});
			started = true;
			c++;
		}, realDelay);
	}
});
var hideTitle=function(current)
{
	if(useTitle && animateTitle)
	{
		cTitle = current.childElements();
		titleHeight = cTitle[1].getHeight();
		cTitle = cTitle[1].readAttribute('id');
		new Effect.Move(cTitle, { y: -titleHeight, mode: 'absolute' });
	}
}
var showTitle=function(current)
{
	if(useTitle && animateTitle)
	{
		cTitle = current.childElements();
		cTitle = cTitle[1].readAttribute('id');
		new Effect.Move(cTitle, { y: 0, mode: 'absolute' });
	}
}
var shuffle=function(o){
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};
