package net.fdms.Function.Loader 
{
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import net.fdms.Action.MouseAction;
	import net.fdms.Action.ResControl;
	import net.fdms.Frame.Space;
	import net.fdms.Resource.Resource;
	/**
	 * ...
	 * @author Dawnboy
	 */
	public class Loader 
	{
		private static var loadQueue:Array = [];
		private static var clearTimer:Timer = null;
		public function Loader() 
		{
			
		}
		
		public static function addLoad(...res):void {
			var rn:int = res.length;
			
			for (var a:int = 0; a < rn; a++ ) {
				if (!Resource.Status(res[a].toString())){
					var tmp:Array = Resource.getPreloadChild(res[a].toString());	
					var tn:int = tmp.length;
					for (var b:int = 0; b < tn; b++) {
						loadQueue.push(tmp[b].toString());
					}
				}
			}

		}
		
		/**
		 * 开始  首先清除SPACE
		 */
		public static function start():void {
			MouseAction.MouseActionOff();
			var foo:Array = [];
			var n:int = loadQueue.length;
			if(n){
				for (var a:int = 0; a < n; a++) {
					var config:Object = Resource.getConfig(loadQueue[a]);
					if (config.space) {
						if (!Space.checkEmpty(config.space)) {
							foo.push(ResControl.removeRes(Space.getSpaceChild(config.space)));
						}
					}
				}
				if(foo.length){
					foo.sort(Array.NUMERIC);
					var time:Number = foo[foo.length - 1] * 1000;
					clearTimer = new Timer(time, 1);
					clearTimer.addEventListener(TimerEvent.TIMER_COMPLETE, load);
					clearTimer.start();
				}else {
					load();
				}
			}
		}
		
		/**
		 * 执行载入
		 * @param	e
		 */
		private static function load(e:* = null):void {
			if (clearTimer) {
				clearTimer.stop();
				clearTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, load);
			}
			LoadControl.load(loadQueue);
			loadQueue = [];
		}
		
		public static function removeLoader(ID:String):void {
			
		}
		
		public static function removeSpaceLoader(space:String):void {
			var n:int = loadQueue.length;
			for (var a:int = 0; a < n; a++ ) {
				var config:Object = Resource.getConfig(loadQueue[a]);
				if (config) {
					if(config.space == space) {
						loadQueue.splice(a, 1);
					}
				}
			}
			
		}
	}

}