<?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="368" height="340" 
		 close="onCloseHandler(event)"
		 skinClass="org.gallery3.organize.skins.UploadTitleWindowSkin"
		 title="Add album (needs translation)">
	<fx:Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx:Declarations>
	
	<fx:Script>
		<![CDATA[
			import mx.events.CloseEvent;
			import mx.managers.PopUpManager;
			import mx.rpc.AsyncToken;
			import mx.rpc.Responder;
			import mx.rpc.events.FaultEvent;
			import mx.rpc.events.ResultEvent;
			import mx.utils.StringUtil;
			
			import org.gallery3.rest.GalleryAlbum;
			import org.gallery3.rest.GalleryItem;
			
			import spark.events.TextOperationEvent;
			
			private var _album: GalleryAlbum = null;
			private static var WHITESPACE_PATTERN: RegExp = /\s/g;
			
			protected function onCloseHandler(event:CloseEvent): void {
				PopUpManager.removePopUp(this);
			}

			public function get parentAlbum(): GalleryAlbum {
				return _album;
			}
			public function set parentAlbum(value: GalleryAlbum): void {
				_album = value;
			}

			protected function onAddFormHandler(event:MouseEvent): void {
				var newAlbum: GalleryAlbum = GalleryItem.createAlbum(_album.url);
				newAlbum.title = albumTitle.text;
				newAlbum.description = description.text;
				newAlbum.name = albumName.text;
				newAlbum.slug = slug.text;
				var result: AsyncToken = newAlbum.save();
				var self: AddAlbumDialog = this;
				result.addResponder(new mx.rpc.Responder(
					function(event: ResultEvent): void {
						PopUpManager.removePopUp(self);
						_album.refresh();
					},
					function(event: FaultEvent): void {
						throw new Error(event.message);
						// @todo do we want issue an error
					}));
			}

			protected function onCancelHandler(event:MouseEvent): void {
				PopUpManager.removePopUp(this);
			}

			protected function onChangeHandler(event:TextOperationEvent): void {
				submitForm.enabled = true;
			}
			
			protected function onChangeTitleHandler(event:TextOperationEvent): void {
				submitForm.enabled = true;
				slug.text = albumName.text = albumTitle.text.replace(WHITESPACE_PATTERN, "_").toLowerCase();
			}
			
		]]>
	</fx:Script>
	<s:Label y="10" text="Title" id="labelTitle" 
			 paddingTop="5" paddingBottom="5" 
			 width="94" paddingLeft="5" x="10"/>
	<s:TextInput y="10" id="albumTitle"
				 width="224" x="128"
				 change="onChangeTitleHandler(event)"/>
	<s:Label x="10" y="40" text="Description" paddingLeft="5" 
			 paddingTop="5" paddingBottom="5" id="labelDescription"/>
	<s:TextArea x="128" y="40" width="224" id="description"
				change="onChangeHandler(event)"/>
	<s:Label x="10" y="200" text="Directory name" paddingLeft="5" 
			 paddingTop="5" paddingBottom="5" width="94" id="labelName" />
	<s:TextInput x="128" y="200" width="224" id="albumName"
				 change="onChangeHandler(event)"/>
	<s:Label x="10" y="230" text="Internet Address" paddingLeft="5" 
			 paddingTop="5" paddingBottom="5" id="labelSlug" width="110"/>
	<s:TextInput x="128" y="230" width="224" id="slug" 
				 change="onChangeHandler(event)"/>
	<s:HGroup width="100%" bottom="10" right="10">
		<mx:Spacer width="100%" />
		<s:Button label="Add" id="submitForm" enabled="false"
				  click="onAddFormHandler(event)"/>
		<s:Button label="Cancel" id="cancelForm" 
				  click="onCancelHandler(event)"/>
	</s:HGroup>
</s:TitleWindow>
