package classes.gustavo.form
{
	

	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.net.*;
	import flash.net.FileReference;
	import flash.net.Responder;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.utils.setTimeout;

	 
	public class Anexo extends Sprite
	{
		
		
        private var uploadURL			:URLRequest;
        private var file				:FileReference;
		
		private var bt_anexo			:*;
		public var texto				:TextField;
		public var pct_txt				:TextField;
		private var nome				:String;
		
		public static var ON_COMPLETE	:String = "onComplete";		
		public static var ON_SELECT		:String = "onSelect";		
		
		private var urlPhp				:String;
		private var filterRestricao		:String;
		private var nomeRestricao		:String;
		private var nomeDaFoto			:String;
		public  var estaCarregada		:Boolean = false;
		
		private var ganb:*;
		
		
		public function Anexo() 
		{
			
			
		}
		
		public function inicialize(_urlPhp:String,_filterRestricao:String,_nomeRestricao:String):void 
		{
			
			
			urlPhp 				= _urlPhp;
			filterRestricao		= _filterRestricao;
			nomeRestricao		= _nomeRestricao;
			
			_init();
			
		    return void;
		}
		
		public function change(_urlPhp:String):void 
		{
		
			urlPhp 				= _urlPhp;
					
		    return void;
		}
		
		private function _init():void
		{
			
			bt_anexo 	= _btAnexo;
			texto		= _arquivo;
			pct_txt 	= _pct;
			
			
			
			bt_anexo.buttonMode = true;
			bt_anexo.addEventListener(MouseEvent.CLICK,select );
			
		}
		
		public function init():void
		{
			pct_txt.text = " ";
		}
		
		private function configureListeners(dispatcher:IEventDispatcher):void
		{
			
			dispatcher.addEventListener(Event.CANCEL, cancelHandler);
			dispatcher.addEventListener(Event.COMPLETE, completeHandler);
			dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
			dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			dispatcher.addEventListener(Event.OPEN, openHandler);
			dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			dispatcher.addEventListener(Event.SELECT, selectHandler);
		
			try{
				
				dispatcher.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadCompleteDataHandler);
				
			}catch(e:Error){
				
				throw e;
				
			}
            
        }
		
		public function get nome_arquivo():String
		{
			return nome;
		}
		
        private function cancelHandler(event:Event):void 
		{
        }

        private function completeHandler(event:Event):void 
		{
			var file:FileReference = FileReference(event.target);
        }

        private function uploadCompleteDataHandler(event:DataEvent):void 
		{
			
			nome 			= String(event.data);
			pct_txt.text 	= "Arquivo carregado.";
			
			estaCarregada   = true;
			dispatchEvent( new Event( ON_COMPLETE ) );
        }	
		
        private function httpStatusHandler(event:HTTPStatusEvent):void 
		{
        }
        
        private function ioErrorHandler(event:IOErrorEvent):void 
		{
        }

        private function openHandler(event:Event):void 
		{
			dispatchEvent( new Event(Event.OPEN) );	
        }

        private function progressHandler(event:ProgressEvent):void 
		{
            var file:FileReference = FileReference(event.target);
			var pct:int = int((event.bytesLoaded / event.bytesTotal) * 100);
			
			pct_txt.text = "Carregando arquivo: " + pct + "%";
			
        }

        private function securityErrorHandler(event:SecurityErrorEvent):void 
		{
        }

        private function selectHandler(event:Event):void 
		{
			dispatchEvent( new Event( ON_SELECT ) );	
			ganb = event.target;
			var file:FileReference = FileReference(event.target);
			file.upload(uploadURL);
			texto.text = file.name; 
        }
		
		
		public function select( e:Event=null ):void
		{
			
            uploadURL 			  = new URLRequest();
            
			uploadURL.url 		  = urlPhp;
			uploadURL.contentType = "application/octet-stream";
			uploadURL.method 	  = URLRequestMethod.POST;
            
			file = new FileReference();
			configureListeners(file);	

			file.browse([new FileFilter(nomeRestricao,filterRestricao)]);
				
			//uploadURL.url = "http://localhost/flash/libary/upload/upload.php";
			//file.browse([ new FileFilter("Arquivos de texto (*.doc, *.pdf, *.rtf, *.zip, *.rar)", "*.doc;*.pdf;*.rtf;*.zip;*.rar") ]);
			
		}
		
		public function enviar():void
		{	
			if( file ) file.upload( uploadURL );
		}
		
		
		public function close():void
		{	
			texto.text = pct_txt.text = "";	
		}
		
		
	}
	
}
