package net.fdms.Function.Loader 
{
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.core.LoaderCore;
	import com.greensock.loading.ImageLoader;
	import com.greensock.loading.SWFLoader;
	import com.greensock.loading.LoaderMax;
	import net.fdms.Action.MouseAction;
	import net.fdms.Function.FlowControl;
	import net.fdms.Resource.ProgressBar;
	import net.fdms.Resource.Resource;
	/**
	 * ...
	 * @author ...
	 */
	public class LoadControl 
	{
		private static var resLib:Array = [];
		private static var loaderLib:Array = [];
		private static var loader:LoaderMax;
		private static var foo:LoaderCore;
		
		public function LoadControl() 
		{
			
		}
		
		public static function load(loadQueue:Array):void {
			loader = new LoaderMax( { onOpen:onStart, onProgress:onProgress, onComplete:onComplete, onError:onErr } );
			//MouseAction.MouseActionOff();
			resLib = loadQueue;
			var n:int = resLib.length;
			for (var a:int = 0; a < n; a++ ) {
				addLoader(Resource.getConfig(resLib[a].toString()));
			}
			loader.load();
		}
		
		private static function addLoader(config:Object):void {
			
			switch(config.src[0].toString()) {
				case "img":
					foo = new ImageLoader(config.src[1].toString(), { name:config.id.toString(), onError:onChildErr,autoDispose:true } );
					break;
				case "swf":
					foo = new SWFLoader(config.src[1].toString(), { name:config.id.toString(), onError:onChildErr } );
					break;
				/*case "xml":
					loader.append(new XMLLoader(src, { name:id } ));
					break;
				case "txt":
					loader.append(new DataLoader(src, { name:id } ));
					break;
				case "mp3":
					loader.append(new MP3Loader(src, { name:id } ));
				case "mov":
					loader.append(new VideoLoader(src, { name:id } ));*/
				default:
					break;
			}
			loaderLib.push(foo);
			loader.append(foo);
			
			//trace(loaderLib);
		}
		
		
		
		private static function onStart(e:LoaderEvent):void {
			ProgressBar.onStart();
			//MouseAction.MouseActionOff();
		}
		
		private static function onProgress(e:LoaderEvent):void {
			ProgressBar.ProgressFunc(e);
		}
		
		private static function onComplete(e:LoaderEvent):void {
			
			ProgressBar.onFinish();
			var n:int = loaderLib.length;
			for (var a:int = 0; a < n; a++) {
				Resource.setObject(loaderLib[a].name, loaderLib[a].content.getChildAt(0));
				Resource.setLoader(loaderLib[a].name, loaderLib[a]);
			}
			FlowControl.showQueue(resLib);
			
			//MouseAction.MouseActionOn();
			
			init();
		}
		
		private static function onErr(e:LoaderEvent):void {
			trace("出错 ");
		}
		
		/**
		 * 载入队列子项目出错
		 * @param	e
		 */
		private static function onChildErr(e:LoaderEvent):void {
			trace(e.target.name + "载入出错,从载入队列中移除");
			removeResLoad(e.target.name);
		}
		
		/**
		 * 移除错误的Loader
		 * @param	name
		 */
		private static function removeResLoad(name:String):void {
			var errLoader:LoaderCore = loader.getLoader(name);
			errLoader.dispose();
			errLoader.unload();
			loader.remove(errLoader);
			loaderLib.splice(loaderLib.indexOf(errLoader), 1);
			resLib.splice(resLib.indexOf(name), 1);
			
		}
		
		private static function init():void {
			resLib = [];
			var n:int = loaderLib.length;
			for (var a:int = 0; a < n; a++) {
				loader.remove(loaderLib[a]);
				//loaderLib[a].unload();
				loaderLib[a] = null;
			}
			loaderLib = [];
			loader.empty();
			loader.unload()
			loader = null;
			//loader = new LoaderMax( { onOpen:onStart, onProgress:onProgress, onComplete:onComplete, onError:onErr } ); 
			
		}
	}

}