package net.dawnboy 
{
	import com.greensock.loading.core.DisplayObjectLoader;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	/**
	 * 显示列表操作:
	 * getNumChild:获取对象的子实例数;
	 * displayList:获取目标成员的显示列表;
	 * getChildByName:遍历FLASH舞台,通过实例名(Name:字符串)获取实例
	 * 
	 * 2011/1/14 14:42
	 * @author Dawnboy
	 */
	public class DisplayList extends MovieClip
	{
		/**
		 * 显示列表存储数组:
		 * 存储格式:
		 * [成员名,成员引用(目标对象),成员配置=null,[下级成员]]
		 */
		public static var _displaylist:Array = new Array();
		public function DisplayList() 
		{
			
		}
		public static function getNumChild(d:DisplayObjectContainer):int {
			var NUM:int;
			if (d) {
				NUM = d.numChildren;
			}
			return NUM;
		}
		
		//输出显示列表
		public static function displayList(id:String = null):void {
			var depths:int = 0;
			var outPut:String = "";
			getIndex(_displaylist);
			function getIndex(list:Array):void {
				if(list.length){
				for (var a:int = 0; a < list.length; a++) {
					outPut += depths + " : ";
					if (depths) {
						outPut+=drawTable(depths);
					}
					if (list.length - 1 - a) {
						outPut += drawTable(depths)+ "┣ " + list[a][0];
						
					}else{
						outPut += drawTable(depths)+ "┗ " + list[a][0];
					}
					if (list[a][3].length) {
						depths++;
						outPut += "\n";
						getIndex(list[a][3]);
					}
					outPut += "\n";
				}
				depths = 0;
				}
			}

			trace(outPut);
			/*var a:String;
			for(a in _displaylist) {
				trace(_displaylist[a]);
			}*/
		}
		
		//通过字符串查找对象
		public static function getChildByID(TargetName:String):DisplayObject {
			return getListById(TargetName)[1];
		}
		
		//向列表添加新的显示对象
		public static function addDisplayMember(id:String, newMember:DisplayObject, config:XMLList = null, parent:* = null):void {
			if (parent) {
				var tList:Array;
				if (parent is String) {
					tList = getListById(parent)[3];
					if (tList) {
						tList.push([id, newMember, config,[]]);
					}else {
						_displaylist.push([id, newMember, config,[]]);
					}
				}else if(parent is DisplayObjectContainer) {
					tList = getListByObject(parent)[3];
					if (tList) {
						tList.push([id, newMember, config,[]]);
					}else {
						_displaylist.push([id, newMember, config,[]]);
					}
				}else {
					ErrLib.ErrID(0);
				}
			}else {
				_displaylist.push([id, newMember, config,[]]);
			}
			
		}	
		
		//通过ID获取目标对象数据
		public static function getListById(id:String):Array {
			var index:Array = [];
			getIndex(_displaylist,id);
			function getIndex(list:Array, str:String):void {
				for (var a:int = 0; a < list.length; a+=1) {
					if(!index){
						if (list[a][0] == id) {
							index = list[a];
							break;
						}
						if (list[a][3]) {
							getIndex(list[a][3], id);
						}
					}else {
						break;
					}
				}
			}
			return index;
		}
		
		//通过实例对象获取目标对象数据
		public static function getListByObject(target:DisplayObject):Array {
			var index:Array = [];
			
			getIndex(_displaylist,target);
			function getIndex(list:Array, target:DisplayObject):void {
				for (var a:int = 0; a < list.length; a+=1) {
					if(!index){
						if (list[a][1] == target) {
							index = list[a];
							break;
						}
						if (list[a][3]) {
							getIndex(list[a][3], target);
						}
					}else {
						break;
					}
				}
			}
			return index;
		}
	
		
		private static function addTable(a:int):String {
			var c:String=""
			for (var b:int = 0; b < a; b += 1) {
				c += "  ";
			}
			return c;
		}
		private static	function drawTable(a:int):String {
			var c:String = "";
			for (var b:int = 0; b < a; b += 1) {
				c +="┃   "
			}
			return c;
		}
	}
}