package classes.matuto.core
{
	
	
	import classes.matuto.erro.Erro;
	
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.utils.setTimeout;
	
	
	public class PreloaderDefaultBase extends Sprite
	{
		
		private             var loader	:Loader;
		private             var pagina	:Object; 
		private             var porcento:Number;
		
		public static const PROGRESS	:String = "PROGRESS";
		public static const COMPLETE	:String = "COMPLETE";
				
		public var onCompleteRemove		:Function;
		public var onComplete			:Function;
		public var onClose				:Function;
		
		public var estaCarregado		:Boolean = false;
		
		
		
		public function  PreloaderDefaultBase() 
		{
			
			
		}
		
		
		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  					  = (carregado*100)/total; 
			
			progress();
			
		}
		
		private function _complete(e:Event):void 
		{
			pagina                                                       = e.currentTarget.content as Object;
			pagina.visible = false;

			onComplete();
			
			loader.unload();
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,_complete);
			loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, _progress);		
			loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, erroPreloader);	
		
			complete();
			
			estaCarregado = true;	
		}
		
		public function inicializeModule():void
		{
			getSWF.init();
			getSWF.resize();	
			
			setTimeout(function():void{
				
				pagina.visible = true;
				onCompleteRemove();
				disposeLocal();
				
			},100);

		}
		
		
		//======================================get====================================================//
		
		
		public function get getSWF():Object
		{
			return pagina;
		}
		
		public function get getPorcento():Number
		{
			return porcento;	
		}
		
		
		
		//=============================================================================================//
		
		
		public function disposeLocal():void
		{
			
			loader.unload();
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,_complete);
			loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, _progress);		
			loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, erroPreloader);	
			
			pagina = null;
			loader = null;
			
			
		}
		
		private function erroPreloader(param1:IOErrorEvent):void
		{
			Erro.add("ERRO NO LOADER DO SWF");
		}
		
		
		//USER
		
		public function init():void
		{
			
		}
		
		public function close():void
		{
			onClose(); 	
		}
		
		public function complete():void
		{
			
			
		}
		
		public function progress():void
		{
			
			
		}
		
		
		public function resize():void
		{
			
			
			
		}
		
		public function dispose():void
		{
			
		
		}
		
		
		
		
	}//END CLASS
	
}