function CrossFader( images, speed, containerId, zIndex )
{
	this.containerRef = document.getElementById( containerId );
	this.imagesRef = new Array();
	this.currImage = 0;
	this.prevImage = null;
	this.speed = speed;
	this.zIndex = ( zIndex ) ? zIndex : 0;
	this.containerRef.style.zIndex = this.zIndex;

	// Set the opacity level of an image

	function setOpacity( imageRef, opacity )
	{
		if ( imageRef.style )
		{
			if ( imageRef.style.MozOpacity != null )
			{  
				imageRef.style.MozOpacity = ( opacity / 100 ) - 0.001;
			}
			else if ( imageRef.style.opacity != null )
			{
				imageRef.style.opacity = ( opacity / 100 ) - 0.001;
			}
			else if ( imageRef.style.filter != null )
			{
				imageRef.style.filter = "alpha(opacity=" + opacity + ")";
			}
		}
	}

	// Handle the next step of the crossfade process

	function cycle( obj, currOpacity )
	{
		if ( currOpacity < 100 )
		{
			setOpacity( obj.imagesRef[ obj.currImage ], currOpacity );
			setOpacity( obj.imagesRef[ obj.prevImage ], 100 - currOpacity );
			currOpacity += obj.speed;
		}
		else
		{
			if ( obj.prevImage != null )
			{
				setOpacity( obj.imagesRef[ obj.prevImage ], 0 );
			}

			obj.prevImage = obj.currImage;
			if ( ++obj.currImage > ( obj.imagesRef.length - 1 ) )
			{
				obj.currImage = 0;
			}

			currOpacity = 0;
		}

		window.setTimeout( function () { cycle( obj, currOpacity ); }, 35 );
	}

	// Create the images in the container

	for ( var i in images )
	{
		var image = new Image();
		image.src = images[ i ];
		image.style.position = "absolute";
		image.style.zIndex   = this.zIndex;
		setOpacity( image, 0 );
		this.imagesRef.push( this.containerRef.appendChild( image ) );
	}

	setOpacity( this.imagesRef[ this.currImage ], 100 );
	cycle( this, 100 );
}
