Vn.resource ('js/htk/image-editor.xml');
Vn.define (function () {

/**
 * A form to handle the image database, it allows to add new images or
 * replace it.
 **/
Htk.ImageEditor = new Class
({
	Extends: Htk.Widget
	
	,initialize: function (props)
	{
		this.builderInit (this.constructor.Xml);

		this.$('max-size').value = 10 /* MB */ * 1048576;
		this.$('form').action =
			'//'+ Vn.Config['image_host'] +'/rest.php?action=image';

		this.parent (props);
	}
	
	,onNameChange: function ()
	{
		var newValue = this.$('name').value;
		
		if (!newValue)
			newValue = null
	
		this.signalEmit ('name-changed', newValue);
	}
	
	,onFormSubmit: function ()
	{
		this.$('submit').disabled = true;
		this.$('spinner').start ();
	}
	
	,onIframeLoad: function ()
	{
		this.$('submit').disabled = false;
		this.$('spinner').stop ();

		try {
			var responseText = this.$('iframe').contentDocument.body.textContent;
			var response = eval ('('+ responseText +')');

			if (response.data)
			{
				this.signalEmit ('file-uploaded', this.$('name').value);
				Htk.Toast.showMessage (_('ImageAdded'));
			}
			else
				Htk.Toast.showError (response.error.message +' ('+ response.error.code +')');
		}
		catch (e) {}
	}
	
	,setData: function (image, directory)
	{
		this.$('name').value = image;
		this.$('schema').value = directory;
	}
}).extend
({
	Xml: 'js/htk/image-editor.xml'
});

});