// Scriptaculous slideshow code written by Rufo Sanchez - rufo@rufosanchez.com

var delayBetweenDisplay = 500 // controls the delay between each of the three images blinding down
var intervalBetweenSlides = 6000; // controls how long the script will wait after displaying the images
var loadImageDelay = 50 // controls how long before starting the animation the script will replace the image - 50ms seems to work
var imagePathPrefix = ""; //if you need a prefix before every image loaded, you could set that here (or leave it blank and do it yourself) - make sure there's a slash at the end
var backgroundImage = imagePathPrefix + "background.jpg" //the image we display at the beginning and end - this doesn't neccessarily have to use imagePathPrefix
var currentInterval = 0; //this is our counter for scheduling when each animation will happen - theoretically, if you wanted everything to wait, you could increment this - but I haven't tried it

// the image arrays are in the index.html file - they're what you'll output in PHP

//some utility functions for repetitive things - cleans up the code better
//assignImage schedules a time at which an image will be assigned to a div - we need to do this just before an animation occurs
function assignImage(idToAssign, imgToAssign, assignWhen) {
	srcToAssign = "<img src='" + imgToAssign + "'/>";
	codeToRun = "$('" + idToAssign + "').innerHTML = \"" + srcToAssign + "\";";
	setTimeout(codeToRun, assignWhen);
}

//bumpInterval increments currentInterval by the current amount - it's in a function because I put it in one
function bumpInterval(howMuch) {
	currentInterval += howMuch;
}

//scheduleAnimation takes care of scheduling the animation. when to assign the image, all that good stuff
function scheduleAnimation(idToAnimate, imageToAnimate, whatEffect, whenToAnimate) {
	animationCode = whatEffect + "(\'" + idToAnimate + "\');";
	
	assignImage(idToAnimate, imageToAnimate, whenToAnimate - loadImageDelay);
	setTimeout(animationCode, whenToAnimate);
}

//this is an attempt to preload the images
var img_list = new Array();
([left, u_right, l_right, whole_field]).flatten().each(function (image) {
	img = img_list.push(new Image());
	img.src = "" + image;
});

// OK, here we go - bring in the background image
assignImage("whole_field", backgroundImage, 0);
Effect.Grow('whole_field');
bumpInterval(intervalBetweenSlides);

(left.length).times(function(index) {
	// puff the whole_field image - wait for delayBetweenDisplay
	setTimeout("Effect.Puff(\'whole_field\');", currentInterval);
	bumpInterval(delayBetweenDisplay);
	
	//bring down each of the three areas, waiting between each animation for delayBetweenDisplay
	scheduleAnimation("left_box", imagePathPrefix + left[index], "Effect.BlindDown", currentInterval);
	bumpInterval(delayBetweenDisplay);
	scheduleAnimation("upper_right", imagePathPrefix + u_right[index], "Effect.BlindDown", currentInterval);
	bumpInterval(delayBetweenDisplay);
	scheduleAnimation("lower_right", imagePathPrefix + l_right[index], "Effect.BlindDown", currentInterval);
	
	//wait for intervalBetweenSlides
	bumpInterval(intervalBetweenSlides);
	
	//bring up the images, and wait for delayBetweenDisplay
	setTimeout("Effect.BlindUp(\'left_box\');", currentInterval);
	bumpInterval(delayBetweenDisplay);
	setTimeout("Effect.BlindUp(\'upper_right\');", currentInterval);
	bumpInterval(delayBetweenDisplay);
	setTimeout("Effect.BlindUp(\'lower_right\');", currentInterval);
	bumpInterval(delayBetweenDisplay);

	//bring up a new background image, and display it for intervalBetweenSlides
	scheduleAnimation("whole_field", imagePathPrefix + whole_field[index], "Effect.Grow", currentInterval);
	bumpInterval(intervalBetweenSlides);
});

//OK, get rid of the last background image, wait until that animation is over (default duration of scriptaculous is 1 second, so we wait for 1.2 - otherwise you get weird stuff going on with both animations running on the same area at the same time)
setTimeout("Effect.Puff(\'whole_field\');", currentInterval);
bumpInterval(1200);
scheduleAnimation("whole_field", backgroundImage, "Effect.Grow", currentInterval);


//and, scene

