//  _____________________________________________________________________________________________________________
// |_| ImageRotator |____________________________________________________________________________________________|
//
//	atrauzzi@gmail.com
//	(c) 2007 Alexander Trauzzi
//	Any use, modification or derivative works from this script are strictly prohibited unless authorized in
//	writing by the script author.
//
//	This JavaScript class rotates through a sequential collection of images in a specified directory.
//	It does not require a server back-end, and thus the images must be consecutive (1.jpg, 2.jpg...) in
//	order to work.
//	Images are preloaded during the wait between fades to keep things looking sharp.  If there is no image
//	loaded, it is pre-loaded before being displayed.
//
//	ImageRotator has been tested and confirmed to work on the following platforms:
//		Windows			-	Microsoft Internet Explorer 6 32bit
//						Microsoft Internet Explorer 7 32bit
//						Mozilla Firefox 2.0.0.6 32bit
//						Opera 9.22
//						Apple Safari
//		Linux			-	Mozilla Firefox 2.0.0.6 x86
//						Mozilla Firefox 2.0.0.6 x86_64
//						Opera 9.22
//
//	ImageRotator depends on:
//		XHTML
//		JavaScript 1.6 with DOM
//		Prototype 1.5.1
//		Scriptaculous 1.7.0
//
//	private pickNewImage()		-	Picks a new image and preloads it.
//
//	public ImageRotator()		-	Constructor.
//		domTarget		-	The DOM node under which to place images.
//		path			-	An address pointing to a browseable directory (HTTP,FTP,file).
//		count			-	The number of images within {path}.
//
//	public rotate()			-	Triggers one rotation cycle.
//
//	public start()			-	Starts image rotation.
//
//	public stop()			-	Stops rotating images.
//  _____________________________________________________________________________________________________________
// |_____________________________________________________________________________________________________________|


var ImageRotator = Class.create();
ImageRotator.prototype.initialize = function (domTarget, path, count) {

  // The PeriodicalExecuter used to time the rotations.
  var timer;
  // The current image on display.
  var currentImage;
  // The next image to be loaded.
  var newImage;

	var pickNewImage = function() {

		newImage = document.createElement("img");
		newImage.src = path + "/" + (Math.floor(Math.random()*count)) + ".jpg";
		newImage.alt = "Random Gallery Image!";
		newImage.style.display = "none";
		// Add the new image to the DOM.
		domTarget.appendChild(newImage);

	};

	this.rotate = function() {

		// If there is currently an image on display.
		if(currentImage) {

			// Begin the fade-out of the old image.
			new Effect.Fade(currentImage, {

				afterFinish: function () {
					// Remove the current image from the dom.
					currentImage.parentNode.removeChild(currentImage);
					// Set the new image to be the current image.
					currentImage = newImage;
				}

			});

			// Fade the new image in while the old one fades out for a nice transition.
			new Effect.Appear(newImage, {
				// Once the image is done fading in, start to prepare the next one.
				afterFinish: function () {
					pickNewImage();
				}
			});

		}
		// This happens if this is the first time an image is being displayed.
		else {

			// Prepare an image right away.
			pickNewImage();

			Event.observe(newImage, "load", function () {

				currentImage = newImage;

				// Fade the image in.
				new Effect.Appear(newImage, {
					afterFinish: function () {
						pickNewImage();
					}
				});

			});

		}

	};

	this.start = function () {
		timer = new PeriodicalExecuter(this.rotate, 9);
	};

	this.stop = function () {
		timer.stop();
	};

};
//  _____________________________________________________________________________________________________________
// |_____________________________________________________________________________________________________________|
