package classes.matuto.loaders
{
    import flash.display.Sprite;
    import flash.events.*;
    import flash.net.*;
    import flash.text.TextField;
 
    public class PHPLoad extends Sprite 
	{
		
		public  var data		:Object;
		private var loader		:URLLoader;
		private var urlreq		:URLRequest;
		public  var onComplete	:Function;
		
		/**
		 * 
		 * @param url URL do PHP, pode ser local ou online, se tiver URLVariavle ira com POST tambem
		 * @param variable params do PHP
		 * @param _onComplete Função para caso ocorra o "getInstance()"
		 * 
		 */
        public function PHPLoad(url:String,variable:URLVariables=null,_onComplete:Function=null)
		{
            loader								= new URLLoader();
			urlreq								= new URLRequest(url);
			
			loader.dataFormat 			  		= URLLoaderDataFormat.VARIABLES;
			
			if(variable)
			{
				urlreq.method 				  	= URLRequestMethod.POST;
				urlreq.data 				  	= variable;
			}
			
			onComplete						 	= _onComplete;

			if(onComplete != null)
			init();
        }
		
		public function init():void
		{
			configureListeners(loader);
			
			try 
			{			
				loader.load(urlreq);			
				
			} catch (error:Error) 
			{
				trace("ERRO");
			}
		
		}

        private function configureListeners(dispatcher:IEventDispatcher):void
		{
            dispatcher.addEventListener(Event.COMPLETE, completeHandler);
            dispatcher.addEventListener(Event.OPEN, openHandler);
            dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        }
 
        public function completeHandler(event:Event):void 
		{
            var loader:URLLoader = URLLoader(event.target);
            data				 = loader.data;
			
			if(onComplete != null)
			onComplete(getObject());
			
			dispatchEvent(new Event(Event.COMPLETE));
        }
			
		/**
		 * 
		 * @return Retorno o objeto do PHP
		 * 
		 */
		public function getObject():Object
		{
		   return data;
		}
 
        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 
		{
			trace("ERRO PHP");
		}
		
		
		/**
		 * 
		 * @param url URL do PHP, pode ser local ou online, se tiver URLVariavle ira com POST tambem
		 * @param variable params do PHP
		 * @param _onComplete Função para caso ocorra o "getInstance()"
		 * 
		 */
		public static function getInstance(url:String,variable:URLVariables=null,_onComplete:Function=null):PHPLoad
		{
		   return new PHPLoad(url,variable,_onComplete);
		}
	
		
		/*
		 *
		
				import classes.matuto.loaders.PHPLoad;

		
				
				private var phpLoad:PHPLoad;

		
		
				var urlVar:URLVariables = new URLVariables();
				urlVar.funcao		    = "permissao";
				
				var urlVar2:URLVariables = new URLVariables();
				urlVar2.teste		    = "MASSA";
				
				var url:String 			= "http://www.mundosafari.com.br/testes/facebook/php/facebook/MatutoFb.php";
				var url2:String 		= "http://localhost:8888/matutoV4/html/php/Teste.php";
				
				PHPLoad.getInstance(url,urlVar,function(e:Object):void
				{ 
				trace(e);
				});
				
				PHPLoad.getInstance(url2,urlVar2,function(e:Object):void
				{ 
				trace(e.teste);
				});
				
				phpLoad					= new PHPLoad(url,urlVar);
				phpLoad.addEventListener(Event.COMPLETE,onCompletePhp);
				phpLoad.init();
		
		*/
		
		
    }//end class
}