//Import TweenLite
import gs.*;
import gs.easing.*;
 
//Create an array which will contain all image URLs
var imageURLs = new Array();
 
//This array will contain all the image holders
var imageHolders = new Array();
 
//We want to know how many images have been loaded
var loadedImages:Number = 0;
 
//Add all of our image URLs into the array
imageURLs.push("http://flashmymind.com/images/image1.gif");
imageURLs.push("http://flashmymind.com/images/image2.gif");
imageURLs.push("http://flashmymind.com/images/image3.gif");
imageURLs.push("http://flashmymind.com/images/image4.gif");
imageURLs.push("http://flashmymind.com/images/image5.gif");
imageURLs.push("http://flashmymind.com/images/image6.gif");
imageURLs.push("http://flashmymind.com/images/image7.gif");
imageURLs.push("http://flashmymind.com/images/image8.gif");
imageURLs.push("http://flashmymind.com/images/image9.gif");
imageURLs.push("http://flashmymind.com/images/image10.gif");
imageURLs.push("http://flashmymind.com/images/image11.gif");
imageURLs.push("http://flashmymind.com/images/image12.gif");
imageURLs.push("http://flashmymind.com/images/image13.gif");
imageURLs.push("http://flashmymind.com/images/image14.gif");
imageURLs.push("http://flashmymind.com/images/image15.gif");
imageURLs.push("http://flashmymind.com/images/image16.gif");
imageURLs.push("http://flashmymind.com/images/image17.gif");
imageURLs.push("http://flashmymind.com/images/image18.gif");
imageURLs.push("http://flashmymind.com/images/image19.gif");
imageURLs.push("http://flashmymind.com/images/image20.gif");
 
//The number of rows and columns we will have
var rows:Number = 4;
var columns:Number = 5;
 
//Store the image width and height (40x40)
var imageWidth:Number = 40;
var imageHeight:Number = 40;
 
//Padding between the images
var padding:Number = 1.3;
 
//Left edge space
var left:Number = 20;
 
//Create the image holders (= empty movie clips) and position them on the stage
for (var i = 0; i < rows; i++) {
 
	for (var j = 0; j < columns; j++) {
 
		//Create a new holder
		var imageHolder:MovieClip = new MovieClip();
 
		//Position the holder
		imageHolder.x = left + j * imageWidth * padding;
		imageHolder.y = i * imageHeight * padding;
 
		//Add a StarPreloader to the center of the holder
		var preloader:StarPreloader = new StarPreloader();
		preloader.x = imageWidth / 2;
		preloader.y = imageHeight / 2;
		imageHolder.addChild(preloader);
 
		//We need to know which preloader belongs to which image holder,
		//so we set the preloader to be a property of the holder.
		imageHolder.preloader = preloader;
 
		//Add ENTER_FRAME so we can rotate the preloader
		preloader.addEventListener(Event.ENTER_FRAME, rotatePreloader);
 
		//Add the holder to the stage
		addChild(imageHolder);
 
		//Add the holder to the imageHolders array
		imageHolders.push(imageHolder);
	}
}
 
//Call the function that loads an image that is first in the imageURLs array
loadImage();
 
//This function loads the first image in the imageURLs array
function loadImage():void {
 
	//Create the loader
	var loader:Loader = new Loader();
 
	//Get the URL to load
	var urlRequest:URLRequest = new URLRequest(imageURLs[0]);
 
	//Load the image
	loader.load(urlRequest);
 
	//Listen when the loading is complete
	loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
 
	//Remove the first URL in the imageURLs array since we
	//started to load that image.
	imageURLs.splice(0,1);
}
 
//This function is called when an image is loaded
function imageLoaded(e:Event):void {
 
	//Save the loaded bitmap to a local variable
	var image:Bitmap = (Bitmap)(e.target.content);
 
	//Save the image's holder to a local variable
	var holder:MovieClip = imageHolders[loadedImages];
 
	//Add the image to the holder
	holder.addChild(image);
 
	//Tween the image holder
	TweenLite.from(holder, 3, {alpha: 0});
 
	//Tween the preloader.
	//We call the function "tweenFinished" when the animation is done.
	//We pass the preloader and the image holder as parameters to the function.
	TweenLite.to(holder.preloader, 1, {scaleX: 2, scaleY: 2, alpha: 0, 
	 onComplete:tweenFinished, onCompleteParams:[holder.preloader, holder]});
 
	//Update the loaded images count
	loadedImages++;
 
	//We load the next image if there are still more URLs in the array
	if (imageURLs.length > 0) {
		loadImage();
	}
}
 
//This function rotates a preloader
function rotatePreloader(e:Event):void {
 
	//Rotate the preloader
	e.target.rotation += 5;
}
 
//This function is called when a preloader is finished tweening
function tweenFinished(preloader:MovieClip, holder:MovieClip):void {
 
	//Remove the preloader from the holder
	holder.removeChild(preloader);
}