function CAjaxEIP(my_id, fckeditor_path)
{
	// public String
	this.id = my_id;

	// public String
	this.handler_url = undefined;

	// public Function
	this.error_function = null;
	// public Function
	this.pre_function = null;
	// public Function
	this.post_function = null;

	// public Bool
	this.editing = false;

	// private Object
	this.current_element = null;
	// private String
	this.current_edit_type = null;
	// private String
	this.current_action = null;
	// private String
	this.current_id = null;

	// private String
	this.original_html = null;

	// private String
	this.fckeditor_path = fckeditor_path;
	this.fckeditor = null;

	// Key code MACRO
	this.KEY_ENTER					= 13;
	this.KEY_ESCAPE					= 27;


	// public
	this.Init = function()
	{
	};


	// public
	this.Edit = function(element, edit_type, id, action, pre_function, post_function)
	{
		// Already editing something
		if ( this.editing )
		{
			return false;
		}
		// Nothing to finish editing
		else
		{
			// Define WYSIWYG editor size
			var editor_width = '100%';
			var editor_height = element.offsetHeight;
			if ( editor_height < 400 )
			{
				editor_height = 400;
			}

			this.editing = true;
			this.current_element = element;
			this.current_edit_type = edit_type;
			this.current_id = id;
			this.current_action = action;

			// Save current HTML
			this.original_html = element.innerHTML;

			// Register functions
			this.pre_function = pre_function;
			this.post_function = post_function;

			// Get HTML element value
			if ( this.current_edit_type == 'html' )
			{
				var value = element.innerHTML;
			}
			// Get text value contained in element
			else if ( this.current_edit_type == 'textarea' || this.current_edit_type == 'input' )
			{
				if ( element.innerText )
				{
					var value = element.innerText;
				}
				else
				{
					var value = element.textContent;
				}
			}


			// BEGIN: Create edit field
			if ( this.current_edit_type == 'html' )
			{
				// Create editor
				this.fckeditor = new FCKeditor('__edit_in_place_field', editor_width, editor_height, 'Achka', value);
				this.fckeditor.BasePath = this.fckeditor_path + '/';

				element.innerHTML = '<div class="ajax_eip" id="__edit_in_place_field">';
				element.innerHTML = this.fckeditor.CreateHtml();
				element.innerHTML += '</div>';
			}
			else if ( this.current_edit_type == 'textarea' )
			{
				element.innerHTML = '<div><textarea name="content" class="ajax_eip" id="__edit_in_place_field">' + value + '</textarea></div>';
				element.innerHTML += '<input type="button" value="Sauver" onclick="' + this.id + '.Save()" />';
				element.innerHTML += '<input type="button" value="Annuler" onclick="' + this.id + '.Cancel()" />';

				// Focus field
				var input = byId('__edit_in_place_field');
				input.focus();
			}
			else if ( this.current_edit_type == 'input' )
			{
				element.innerHTML = '<input type="text" name="content" class="ajax_eip" id="__edit_in_place_field" value="' + value + '" />';

				// Define on key down function
				var input = byId('__edit_in_place_field');
				input.onkeydown = dojo.lang.hitch(this, this.OnKeyDown);
				input.onblur = dojo.lang.hitch(this, this.Save);

				// Focus field
				input.focus();
			}
			// END: Create edit field


			return true;
		}
	};


	this.GetKeyCode = function(evt)
	{
	    for ( prop in evt )
	    {
	        if( prop == 'which' )
	        {
	            return evt.which;
	        }
	    }

	    return evt.keyCode;
	};


	this.OnKeyDown = function(evt)
	{
        if ( !evt && window.event )
        {
            evt = window.event;
        }

		if ( this.GetKeyCode(evt) == this.KEY_ENTER )
        {
        	this.Save();
		}

		else if ( this.GetKeyCode(evt) == this.KEY_ESCAPE )
        {
        	this.Cancel();
        }
	};


	this.Save = function()
	{
		if ( this.current_edit_type == 'html' )
		{
			var value = FCKeditorAPI.GetInstance('__edit_in_place_field').GetXHTML(true);

			// Full screen mode is enabled
			if ( FCKeditorAPI.GetInstance('__edit_in_place_field').Commands.GetCommand("FitWindow").GetState() )
			{
				// Exit full screen mode
				FCKeditorAPI.GetInstance('__edit_in_place_field').Commands.GetCommand("FitWindow").Execute();
			}
		}
		else if ( this.current_edit_type == 'textarea' || this.current_edit_type == 'input' )
		{
			var value = byId('__edit_in_place_field').value;
		}

		// Set new HTML
		this.current_element.innerHTML = value;

		this.editing = false;

		if ( this.pre_function )
		{
			this.pre_function(this.current_id, value);
		}

		// Do server side job
		dojo.io.bind({
			url: this.handler_url,
			method: "post",
			content: { action: this.current_action, id: this.current_id, html_id: this.current_element.id, content: value },
			load: dojo.lang.hitch(this, this.EditOK),
			error: this.error_function,
			mimetype: "text/plain",
			preventCache: true
		});
	};


	this.Cancel = function()
	{
		if ( this.current_edit_type == 'html' )
		{
			// Full screen mode is enabled
			if ( FCKeditorAPI.GetInstance('__edit_in_place_field').Commands.GetCommand("FitWindow").GetState() )
			{
				// Exit full screen mode
				FCKeditorAPI.GetInstance('__edit_in_place_field').Commands.GetCommand("FitWindow").Execute();
			}
		}

		// Restore original HTML
		this.current_element.innerHTML = this.original_html;

		this.editing = false;
	};


	this.EditOK = function(type, response, evt)
	{
		if ( this.post_function )
		{
			this.post_function(type, response, evt);
		}
	};
}
