package  {
	import flash.display.MovieClip;
	
	public class PreviewHolder extends MovieClip
	{
	
		var startingWidth = 1024;
		var startingHeight = 1024;
		
		public function PreviewHolder()
		{
			startingWidth = this.width;
			startingHeight = this.height;
		}
		
		var clips:Array = new Array();
		public function AddClip(clip)
		{
			clips.push(clip);
			this.addChild(clip);
			ArrangeClips();
		}
		
		public function ArrangeClips()
		{
			var numCols = Math.ceil(Math.sqrt(clips.length));
			var maxY = 0;
			var maxX = 0;
			var clipIndex = 0;
			for (var row = 0; row < numCols; row++)
			{
				var clipX = 0;
				var clipY = maxY;
				for (var col = 0; col < numCols; col++)
				{
					if (clipIndex == clips.length) break;
					clips[clipIndex].x = clipX;
					clips[clipIndex].y = clipY;
					clipX += clips[clipIndex].width;
					var clipBottom = clips[clipIndex].y + clips[clipIndex].height;
					if (clipBottom > maxY) maxY = clipBottom;

					var clipRight = clips[clipIndex].x + clips[clipIndex].width;
					if (clipRight > maxX) maxX = clipRight;
					clipIndex++;
				}
			}
			
			var scale = Math.min(startingWidth / maxX, startingHeight / maxY);
			if (scale > 1) scale = 1;
			
			this.scaleX = this.scaleY = scale;
		}

	}
	
}
