<?xml version="1.0" encoding="utf-8"?>
<s:BorderContainer xmlns:fx="http://ns.adobe.com/mxml/2009" 
		 xmlns:s="library://ns.adobe.com/flex/spark" 
		 xmlns:mx="library://ns.adobe.com/flex/mx" 
		 backgroundColor="#F5F5F5" borderColor="#CCCCCC" 
		 borderColor.Error="#C50101" backgroundColor.Error="#F3DBDB"
		 height.Ready="45" height="60" width="100%">
	
	<s:layout>
		<s:BasicLayout/>
	</s:layout>
	
	<fx:Script>
		<![CDATA[
			
			import org.umbraco.desktopmediauploader.events.*;
			import org.umbraco.desktopmediauploader.net.*;
			import org.umbraco.desktopmediauploader.util.*;
			
			import flash.desktop.*;
			import flash.events.*;
			import flash.filesystem.*;
			import flash.net.*;
			
			import org.osmf.utils.URL;

			[Bindable] public var file:FileReference;
			[Bindable] public var path:String;
			
			[Bindable] public var uploading:Boolean = false;;
			[Bindable] public var uploaded:Boolean = false;
			
			public function get fullPath():String
			{
				return path + "/" + file.name;
			}
			
			protected function btnUploadRemove_Click(event:MouseEvent):void
			{
				cancel();
			}
			
			protected function file_Complete(event:DataEvent):void
			{
				uploading = false;

				var result:String = StringHelper.cleanXmlString(event.data);
				var xml:XML = new XML(result);
				if (xml.@success == "true")
				{
					uploaded = true;
					
					dispatchEvent(new Event(Event.COMPLETE));
				}
				else
				{
					file_SecurityError(new SecurityErrorEvent(SecurityErrorEvent.SECURITY_ERROR));
				}
			}
			
			protected function file_Progress(event:ProgressEvent):void
			{
				dispatchEvent(event);
			}
			
			protected function file_HttpError(event:HTTPStatusEvent):void
			{
				setCurrentState("Error");
				
				dispatchEvent(event);
			}
			
			protected function file_IOError(event:IOErrorEvent):void
			{
				setCurrentState("Error");
				
				dispatchEvent(event);
			}
			
			protected function file_SecurityError(event:SecurityErrorEvent):void
			{
				setCurrentState("Error");
				
				dispatchEvent(event);
			}
			
			public function upload():void
			{
				setCurrentState("Uploading");
				
				uploading = true;
				
				var vars:URLVariables = new URLVariables();
				vars.parentNodeId = Model.folderId;
				vars.replaceExisting = Model.replaceExisting ? "1" : "0";
				vars.path = path;
					
				var req:URLRequest = DmuRequest.getRequest(DmuRequestAction.UPLOAD, vars);
				
				file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, file_Complete);
				file.addEventListener(ProgressEvent.PROGRESS, file_Progress);
				file.addEventListener(HTTPStatusEvent.HTTP_STATUS, file_HttpError);
				file.addEventListener(IOErrorEvent.IO_ERROR, file_IOError);
				file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, file_SecurityError);
				file.upload(req);
			}
			
			public function pause():void
			{
				if (uploading)
					file.cancel();
				
				uploading = false;
			}
			
			public function cancel():void
			{
				pause();
				
				dispatchEvent(new Event(Event.CANCEL));
			}
			
			private function trim( s:String ):String
			{
				return s.replace( /^([\s|\t|\n]+)?(.*)([\s|\t|\n]+)?$/gm, "$2" );
			}
			
		]]>
	</fx:Script>
	
	<s:states>
		<s:State name="Ready"/>
		<s:State name="Uploading"/>
		<s:State name="Error"/>
	</s:states>
	
	<mx:Image top="6" left="5" source="@Embed('assets/page.png')"/>
	<mx:Label id="lblUploadName" text="{file.name}" fontWeight="bold" color="#000000" left="40" top="4" truncateToFit="true" minWidth="0" right="35" />
	<s:Button id="btnUploadRemove" width="16" height="16" top="5" right="5" useHandCursor="true" buttonMode="true" click="btnUploadRemove_Click(event)" styleName="removeButton" />
	<mx:ProgressBar id="pbUploadProgress" labelPlacement="top" label="" source="{file}" bottom="5" left="5" right="5" excludeFrom="Ready" />
	<mx:Label id="lblUploadPath" left="40" top="20" right="35" fontSize="11" color="#CCCCCC" truncateToFit="true" minWidth="0" fontAntiAliasType="advanced" fontGridFitType="pixel" fontThickness="200" fontSharpness="400"
			  htmlText="&lt;font color='#666666'&gt;{Model.folderPath.replace(/\//g, ' › ')}&lt;/font&gt;{path.replace(/\//g, ' › ')}"/>
	
</s:BorderContainer>
