package classes.matuto.loaders
{
	
	
	import classes.matuto.erro.Erro;
	
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	
	
	public class SwfLoad extends Sprite
	{
		
		private             var loader:Loader;
		private             var pagina:Object; 
	    private             var porcento:int;
		
		public static const PROGRESS	:String = "PROGRESS";
		public static const COMPLETE	:String = "COMPLETE";
		
		public function  SwfLoad() 
		{
                             			
		    
		}

		
		public function load(url:String):void
		{
			
			loader = new Loader();			
			
			loader.load(new URLRequest(url));
		    loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, _progress);
		    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, _complete);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, erroPreloader);
			
		}
		
		private function _progress(event:ProgressEvent):void
		{
			
			var carregado:Number 	      = event.bytesLoaded;
			var total    :Number          = event.bytesTotal;
			porcento  					  = Math.round((carregado*100)/total); 
			
			dispatchEvent(new Event(PROGRESS));

		}
	
		private function _complete(e:Event):void 
		{

			pagina                                                       = e.currentTarget.content as Object;
            addChild(pagina as DisplayObject);
			
			dispatchEvent(new Event(COMPLETE));
						
			loader.unload();
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,_complete);
            loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, _progress);		
            loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, erroPreloader);	
		
		}
		
		
		//======================================get====================================================//
		
		
		public function get getSWF():Object
		{
			return pagina;
		}
		
		public function get getPorcento():int
		{
		
			return porcento;
		
		}
		
		
		
		//=============================================================================================//
		
		
		public function dispose():void
		{

			loader.unload();
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,_complete);
			loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, _progress);		
			loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, erroPreloader);	
		
			pagina = null;
			
					
		}
		
		private function erroPreloader(param1:IOErrorEvent):void
		{
			Erro.add("ERRO NO LOADER DO SWF");
		}
		
	
	}//END CLASS

}