var crossfadeImages;

/**
  * Init Crossfade
  *
  * Gathers the crossfade images into a global
  * array.
  * 
  * @author Matt James <matt@sephone.com>
  * @param  void
  * @return void
  **/
function initCrossfade()
{
	crossfadeImages = $$('#crossfade_container .crossfade_image');
	// Start it up.
	new PeriodicalExecuter(crossfadeStart, 3);
}

/**
  * Crossfade Start
  *
  * Starts the fade off.
  * 
  * @author Matt James <matt@sephone.com>
  * @param  void
  * @return void
  **/
function crossfadeStart()
{
	// Start to fade index 1.
	var image = crossfadeImages.find(function(image) {
		if (image.getStyle('z-index') == 2) {
			return true;
		}
	});
	
	Effect.Fade(image, {duration: 1, afterFinish: crossfadeFinish});
}

/**
  * Crossfade Finish
  *
  * Runs after the crossfade effect is complete.
  * 
  * @author Matt James <matt@sephone.com>
  * @access public 
  * @param  void
  * @return void
  **/
function crossfadeFinish(EffectObject)
{
	// When completely faded, move to back z-index.
	var frontImageId = EffectObject.element.id == "crossfade_1" ? "crossfade_2" : "crossfade_1";
	var frontImage = $(frontImageId);
	var backImage = EffectObject.element;
	
	// Change src file and make opacity full.
	frontImage.setStyle({zIndex: 2});
	backImage.setStyle({zIndex: 1, opacity: 1});
	backImage.src = path.replace('[x]', nextNumber);
	Effect.Appear(backImage, {duration: .02});
	if (nextNumber == endNumber) {
		nextNumber = startNumber;
	} else {
		nextNumber++;
	}
}