var background = function()
{
	var imageRect;
	var minRect;
	var imageLoaded = false;
	
	var options;
	var images;
	var imageSizes;
	
	var timer;
	var currIndex;
	var prevIndex;
	var z = 1000;
	
	var showOnNextLoad = false;
	
	function init( opts )
	{
		images = [];
		imageSizes = [];
	
		options = $.extend({
			interval: 5000,
			speed: 750,
			base: "",
			rotate: true,
			random: true,
			loaderPosition: "50%"
		}, opts );
			
		currIndex = -1;
		
		stage.addResizeListener( this );
		
		var files = options.images;
		if ( options.random ) fisherYates(files);
		
		$(options.container).show().addClass("ajax-load").css("background-position", options.loaderPosition + " 50%");
		
		$.preloadImages( files,
			{
				base: options.base,
				loaded: _handleImageLoaded,
				sequential: true,
				images: options.images
			});
		
		
	}
	
	function release()
	{
		
	}
	
	/*
	Fisher-Yates Shuffle Algorithm
	http://sedition.com/perl/javascript-fy.html
	*/
	function fisherYates ( myArray ) {
  		var i = myArray.length;
		  if ( i == 0 ) return false;
		  while ( --i ) {
		     var j = Math.floor( Math.random() * ( i + 1 ) );
		     var tempi = myArray[i];
		     var tempj = myArray[j];
		     myArray[i] = tempj;
		     myArray[j] = tempi;
		   }
	}
	
	function _handleImageLoaded( img, cnt, total )
	{
		$(img).hide();
		$(options.container).removeClass( "ajax-load" );
		$(options.container).append( img );
		images[cnt-1] = img;
		
		imageSizes[cnt-1] = {width: $(img).width(), height: $(img).height()};
		
		if ( images.length == 1 )
		{
			showNextImage();
		}
		 else if ( showOnNextLoad )
		{
			showOnNextLoad = false;
			showNextImage();
		}
	}
	
	function showNextImage()
	{
		if ( timer ) clearTimeout( timer );
		
		if ( currIndex+1 >= images.length )
		{
			// all images are loaded, reset to 0
			if ( images.length == options.images.length )
			{
				prevIndex = currIndex;
				currIndex = 0;
			}
			 else
			{
				showOnNextLoad = true;
				return;
			}
		}
		 else
		{
			prevIndex = currIndex;
			currIndex++;
		}
		
		_resizeCurrentImage();
		var img = images[currIndex];
		$(img).remove();
		$(options.container).append(img);
		$(images[currIndex]).fadeIn( options.speed, _onTransitionComplete );
	}
	
	function _onTransitionComplete()
	{
		if ( prevIndex >= 0 ) $(images[prevIndex]).hide();
		if ( options.images.length > 1 && options.rotate )
		{
			timer = setTimeout( showNextImage, options.interval );
		}
	}
	
	function onStageResize( $rect )
	{
		if ( currIndex >= 0 )
		{
			_resizeCurrentImage();
		}
	}
	function _resizeCurrentImage()
	{
		var stageRect = stage.getRect();
		var imageRect = RectScaler.createRect( 0, 0, imageSizes[currIndex].width, imageSizes[currIndex].height );
		var newRect = RectScaler.scaleToRectangle( imageRect, stageRect, "scale-fill" );
			
		$(options.container).css({"width": stageRect.width, "height": stageRect.height});
		if ( currIndex >= 0 )
		{
			var img = images[currIndex];
			img.css({"top": newRect.y + "px", "left": newRect.x + "px", "width": newRect.width + "px", "height": newRect.height + "px"} );
		}
	}
	
	return {
		init: init,
		release: release,
		onStageResize: onStageResize
	}
}();

