package classes.matuto.loaders
{
	
	import classes.matuto.display.Display;
	import classes.matuto.erro.Erro;
	
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.system.LoaderContext;
	import flash.utils.*;
	import classes.fl.motion.easing.*;
	import classes.greensock.*;
	import classes.matuto.utils.display.DisplayObjectUtils;
		
	public class ImagemLoad extends Display
	{
		
		private 		var loader		:Loader;
		private   		var bitMap		:Bitmap;
		private   		var url			:String; 
		private  		var _eP			:Number;
		public  		var alvo_mc		:Sprite; 
		private 		var testeAlvo	:Boolean;
		
		public static const PROGRESS	:String = "PROGRESS";
		public static const COMPLETE	:String = "COMPLETE";
		
		public function ImagemLoad() 
		{
			testeAlvo = false;
		}
				
		public function load(param1:String):void
		{
		    loader              		= new Loader();
			var context:LoaderContext   = new LoaderContext();
			context.checkPolicyFile 	= true;
			configureListeners(loader);
			loader.load(new URLRequest(param1),context);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE        , loadComplete);
		    loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);
            loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR , loadErro);	 
		}
		
		private function loadProgress(e:ProgressEvent):void
		{
			_eP = Math.round( (e.bytesLoaded / e.bytesTotal)*100 );
			dispatchEvent(new Event(PROGRESS));
			return;
		}
		
		private function loadComplete(e:Event):void
		{
			
			var loaderInfo:LoaderInfo = LoaderInfo(loader.contentLoaderInfo);
			bitMap               	  = loaderInfo.content as Bitmap;

		
			bitMap.cacheAsBitmap = true;
			bitMap.smoothing     = true;
			
			
			bitMap.alpha = 0;
			
			dispatchEvent(new Event(COMPLETE));
			
			if (!testeAlvo)
			{
				addChild(bitMap);
				TweenMax.to(bitMap,1,{autoAlpha:1,ease:Exponential.easeInOut});

			}else
			{
				//bitMap.width         = alvo_mc.width+1;
				//bitMap.height        = alvo_mc.height;
				
				DisplayObjectUtils.resizeProporcao(bitMap, alvo_mc.width + 1, alvo_mc.height);
				TweenMax.to(bitMap,1,{autoAlpha:1,ease:Exponential.easeInOut});

				alvo_mc.addChild(bitMap);
			}
			
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE        , loadComplete);
		    loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, loadProgress);
            loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR , loadErro);
			
			return;
		}
		
	
		
		//==============================GET===================================================//
		
		public function get getLargura():Number
		{
		   return bitMap.width;	
		}
		
		public function get getAltura():Number
		{
		   return bitMap.height;	
		}
		
		public function getBitmap():Bitmap
		{
		   return bitMap;	
		}
		
		public function get getPorcento():int
		{
		   return _eP;	
		}
		
		//===================================================================================//

		
		//==============================SET==================================================//
		
		
		public function set setComAlvo(value:Boolean):void
		{
			testeAlvo = value;
		}
		
		
		
		//===================================================================================//
		
		
		private function loadErro(e:Event):void
		{
		
			Erro.add("ERRO NO LOADER DA IMAGEM");
			
		     return;	
		}
		
		public override function dispose():void
		{   
			
			if (alvo_mc && alvo_mc.numChildren)
			{
				while(alvo_mc.numChildren)
				alvo_mc.removeChild(bitMap);
				
				loader.unload();
			}
			
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE        , loadComplete);
		    loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, loadProgress);
            loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR , loadErro);
			
		}
		
		private function configureListeners(dispatcher:IEventDispatcher):void
		{
			dispatcher.addEventListener(Event.OPEN, openHandler);
			dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
		}
		
		private function openHandler(event:Event):void 
		{
			
		}
		
		private function progressHandler(event:ProgressEvent):void
		{
			
		}
		
		private function securityErrorHandler(event:SecurityErrorEvent):void
		{
			
		}
		
		private function httpStatusHandler(event:HTTPStatusEvent):void
		{
			
		}
		
		private function ioErrorHandler(event:IOErrorEvent):void 
		{
			
		}
		
		
		
		
    }//end class		
}