<?xml version="1.0" encoding="utf-8"?>
<s:TitleWindow xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" 
			   width="260" height="350" title="Select a folder..." creationComplete="folderPickerDialog_CreationComplete(event)" 
			   close="folderPickerDialog_Close(event)" borderVisible="true" chromeColor="#EEEEEE" borderColor="#666666">
	<s:layout>
		<s:VerticalLayout/>
	</s:layout>
	
	<fx:Script>
		<![CDATA[
			import org.umbraco.desktopmediauploader.events.*;
			import org.umbraco.desktopmediauploader.net.*;
			
			import mx.controls.Alert;
			import mx.events.CloseEvent;
			import mx.events.FlexEvent;
			import mx.managers.*;

			protected function folderPickerDialog_CreationComplete(event:FlexEvent):void
			{
				CursorManager.setBusyCursor();
				
				DmuRequest.makeRequest(DmuRequestAction.FOLDER_LIST,
					folderListLoader_Complete,
					folderListLoader_Error);
			}
			
			protected function folderListLoader_Complete(event:DmuRequestEvent):void
			{
				CursorManager.removeBusyCursor();
				
				var xml:XML = event.result;
				
				trFolders.dataProvider = xml.folder;
				trFolders.labelField = "@name";
				trFolders.validateNow();
				
				// Reselect current folder
				if (Model.folderId)
				{
					var node:XMLList = xml..folder.(@id == Model.folderId.toString());
					if (node.length() > 0)
					{
						expandParents(node[0]);

						trFolders.selectedItem = node[0];
						
						var idx:int = trFolders.getItemIndex(node[0]);
						trFolders.scrollToIndex(idx);
					}
					else
					{
						Model.folderId = undefined;
						Model.folderName = undefined;
					}
				}
			}
			
			protected function folderListLoader_Error(event:Event):void
			{
				CursorManager.removeBusyCursor();
				
				Alert.show("Unable to load folder list", "Ooops!");
			}
			
			protected function folderPickerDialog_Close(event:CloseEvent):void
			{
				PopUpManager.removePopUp(this);
			}


			protected function trFolders_DoubleClick(event:MouseEvent):void
			{
				var item:XML = trFolders.selectedItem as XML;
				
				Model.currentFolder = item;
				Model.folderId = item.@id;
				Model.folderName = item.@name;
				
				var pathItem:XML = item;
				var path:String = pathItem.@name;
				while(pathItem.parent())
				{
					pathItem = pathItem.parent();
					
					if(pathItem.@name != undefined)
					{
						path = pathItem.@name +"/"+ path;
					}
				}
				
				Model.folderPath = path;
				
				PopUpManager.removePopUp(this);
			}
			
			private function expandParents(node:XML):void 
			{	
				if (node && !trFolders.isItemOpen(node)) 
				{
					trFolders.expandItem(node, true);
					expandParents(node.parent());
				}
			}

		]]>
	</fx:Script>
	
	<fx:Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx:Declarations>
	
	<mx:Tree width="100%" height="100%" id="trFolders"
			 defaultLeafIcon="@Embed('assets/folder.png')" 
			 folderClosedIcon="@Embed('assets/folder.png')" 
			 folderOpenIcon="@Embed('assets/folder-open.png')" 
			 paddingLeft="5" paddingRight="5" paddingTop="2" paddingBottom="2" 
			 doubleClickEnabled="true" doubleClick="trFolders_DoubleClick(event)" borderVisible="false"></mx:Tree>
	
</s:TitleWindow>
