/* SlideShow
   Copyright (C) 2008 Best Foot Forward

   Draws a slideshow of the images in the div.
   Requires scriptaculous.js
*/
function SlideShow (div, secs)
{
	var slideshow_div = $(div);

	var srclist = slideshow_div.getElementsByClassName ("slide");
	/* Because we fade all the images (becasue IE 6 is rubbish) we must initially fade out
       all the slides */
	for (var i = 0; i < srclist.length; ++i)
		srclist [i].setStyle ({ opacity : 0 });

	/* Initially make the div invisible */
	slideshow_div.setStyle ({ opacity : 0 });

	FadeTo = function (index) {

		/* Fire an update signal; this allows use to e.g. update a caption elsewhere */
		$(div).fire ("slideshow:focus-change", { 'index' : index });

		var visible_slide = srclist [index];
			visible_slide.setStyle ({ opacity : 0 });

		for (var i = 0; i < srclist.length; ++i)
			srclist [i].style.visibility = i == index ? 'visible' : 'hidden';

		/* Fade in: IE 6 requires we fade the image and the div */
		new Effect.Opacity (slideshow_div, { duration: 1.0, from: 0.0, to: 1.0 });
		new Effect.Opacity (visible_slide, { duration: 1.0, from: 0.0, to: 1.0 });

		/* Queue fading out again */
		setTimeout (function () {
			new Effect.Opacity (slideshow_div, { duration: 1.0, from: 1.0, to: 0.0 });
			new Effect.Opacity (visible_slide, { duration: 1.0, from: 1.0, to: 0.0 });
		}, (secs + 1) * 1000);

		/* Queue the next fade in */
		setTimeout (function () {
			FadeTo ((index + 1) % srclist.length);
		}, (secs + 2) * 1000);
	}

	/* Delay initial slide to avoid flicker */
	setTimeout (function () { FadeTo(0); }, 50);
}

Event.observe(window, 'load', function() {
	var slideshows = $(document).getElementsByClassName ('slideshow');

	for (i = 0; i < slideshows.length; i++) {
		var slideshow = slideshows [i];
		SlideShow (slideshow, 4);
	}
});

