package com.squidder.base.baseLoader.types {
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import com.squidder.base.baseLoader.BaseLoaderProperties;	

	/**
	 * Loader item for text-based files (.txt, .xml, etc).
	 * @author Jon Reiling
	 */
	public class TextItem extends BaseLoaderItem {

		protected var _urlLoader : URLLoader;

		/**
		 * Item constructor.
		 * @param url URL of the item.
		 * @param props Properties, if any, of the item.
		 */
		public function TextItem( url : String, props : BaseLoaderProperties ) {
			
			super( url , props );
			
			_urlLoader = new URLLoader( );
			_urlLoader.addEventListener( Event.COMPLETE , _handleComplete , false , 0 , true );
			_urlLoader.addEventListener( ProgressEvent.PROGRESS , _handleProgress , false , 0 , true );
			_urlLoader.addEventListener( IOErrorEvent.IO_ERROR , _handleError , false , 0 , true );
		}

		/**
		 * Begin loading the item.
		 */
		override public function load() : void {
			
			_urlLoader.load( new URLRequest( _url ) );
		}

		/**
		 * Set the content of the item for access.
		 */
		override protected function _setContent() : void {
			_content = _urlLoader.data;
		}	

		/**
		 * Clean out the item.
		 */
		override public function clean() : void {
			
			_urlLoader.removeEventListener( Event.COMPLETE , _handleComplete );
			_urlLoader.removeEventListener( ProgressEvent.PROGRESS , _handleProgress );
			_urlLoader.removeEventListener( IOErrorEvent.IO_ERROR , _handleError );
			try {
				_urlLoader.close();
			} catch ( e:* ) { }
			_urlLoader = null;
			
			if ( !_props.isPersistent ){
			
				_content = null;	
			}
			
		}			
	}
}
