$.fn.editable = function(url, options)
{
	// Options
	
	options = arrayMerge(
	{
		"border" 	: "1px solid red",
		"cancel" 	: "Annuler",
		"click" 	: "double",
		"ok"		: "Valider",
		"type" 		: "text",
		"url" 		: url
	},options);
	
	
	
	// Event  
	
	if(options.click == "simple")
	{
		this.click(function()
		{
			if(options.type == "FCKeditor")
			{
				function_FCKeditor($(this));	
			}
			else
			{
				function_editable($(this));	
			}
		});
	}
	else if(options.click == "double")
	{
		this.dblclick(function()
		{
			if(options.type == "FCKeditor")
			{
				function_FCKeditor($(this));	
			}
			else
			{
				function_editable($(this));	
			}						
		});
	}
	

	
	
	// Function
	
	function function_FCKeditor(me)
	{
		var fck = new FCKeditor("FCKeditor_"+me.attr('id'));
		fck.BasePath = "js/fckeditor/";
		fck.Config["CustomConfigurationsPath"] ='../../myConfigFCKeditor.js';
		fck.ToolbarSet = 'FCKeditorToolbar';
		fck.Value = me.html();
		fck.Width = me.width();
		fck.Height = me.height()+120;

		var ok='<input type="button" class="submit_FCKeditor" id="submit_'+ me.attr('id') +'" value="'+options.ok+'"/>';
		
		var annuler='<input type="button" class="cancel_FCKeditor" id="cancel_'+ me.attr('id') +'" value="'+options.cancel+'"/>';
		

		var memoire = me.html();
		var div = '<div style="display:none;" class="memoire_FCKeditor" id="memoire_'+ me.attr('id') +'">'+memoire+'</div>';
		
		
		
		me.html(fck.CreateHtml(fck))
		me.after(ok+annuler+div);
		
		
		$(".submit_FCKeditor").click(function(){
			var tmpsid = $(this).attr('id').split('_');
			var id= tmpsid[1];

			var fck_submit = FCKeditorAPI.GetInstance('FCKeditor_'+id);
			var fckHTML = fck_submit.GetHTML();
			
			var data = {};
			data['value'] = fckHTML;
			data['id']=id;
			data['FCKeditor'] = "1";
			
			
			$.ajax(
			{
				type: "POST",
				url: options.url,
				data: data,
				success: function(){ 
					$('#'+id).html(fckHTML); 
					$('#memoire_'+id).remove();
					$('#submit_'+id).remove();
					$('#cancel_'+id).remove();
   				}
			}); 
		});
		$(".cancel_FCKeditor").click(function(){
			var tmpsid = $(this).attr('id').split('_');
			var id= tmpsid[1];
			
			$('#'+id).html($('#memoire_'+id).html()); 
			$('#memoire_'+id).remove();
			$('#submit_'+id).remove();
			$('#cancel_'+id).remove();
			
		});
	}
	
	
	
	
	
	
	
	function function_editable(me)
	{
		if(!me.hasClass('.editable_editing'))
		{
			$('.editable_editing').each( function()
			{
				cancel_editable($(this));
			});
			
			me.addClass('editable_editing');

			myContent	= me.html();
			myId		= me.attr('id');
			myWidth 	= me.width();
			myHeight	= me.height();
			
			if (options.type == "text")
			{
				me.html('<input type="text" value="'+HTMLEncode(myContent)+'" id="'+myId+'" autocomplete="off" /><div class="editable_memoire" style="display:none;">'+myContent+'</div>  ');
				
				me_children = me.children('input');

			}
			else if (options.type == "textarea")
			{
				me.html('<textarea id="'+myId+'" >'+myContent+'</textarea><br/><div style="position:absolute;"><input type="button" class="editable_area_ok" value="'+options.ok+'"><input type="button"  class="editable_area_annuler" value="'+options.cancel+'"></div><div class="editable_memoire" style="display:none;">'+myContent+'</div>  ');
				
				me_children = me.children('textarea');
			}

			
			me_children.width(myWidth);
			me_children.height(myHeight);
				
			copy_style(me, me_children);			
			
			me_children.focus();
			
			
			
			
			// Event 
			
			
			if (options.type == "text")
			{
				me_children.blur(function()
				{
					cancel_editable($(this).parent());
				});
				
				me_children.keydown(function (e) 
				{
					if(e.keyCode=="27") // ESC
					{
						cancel_editable($(this).parent());
					}
					else if(e.keyCode=="13") // ENTER
					{
						valid_editable(myId, $(this).attr('value'), $(this).parent());
						
						return false;
					}
				});
			}
			else if (options.type == "textarea")
			{
				me_children.keydown(function (e) 
				{
					if(e.keyCode=="27") // ESC
					{
						cancel_editable($(this).parent());
					}
				});
				
				me.children('div').children('.editable_area_annuler').click(function () 
				{
					cancel_editable($(this).parent().parent());
				});
				
				me.children('div').children('.editable_area_ok').click(function () 
				{
					valid_editable(myId, $(this).parent().parent().children("textarea").val(), $(this).parent().parent());
				});
			
			}
			
		}
		
	}
	
	
	
	
	
	
	
	// Function annex
	function valid_editable(myId, myContent, me)
	{
		var data = {};
		data['value'] = myContent;
		data['id']= myId;
			
		$.ajax(
		{
			type: "POST",
			url: options.url,
			data: data,
			success: function(msg){
				me.html(msg);
			}
		});
		
		me.removeClass('editable_editing');
	}
	
	function cancel_editable(me)
	{
		me.html(me.children('.editable_memoire').html());
		me.removeClass('editable_editing');
	}
	
	function copy_style(source, dest)
	{
		dest.css('font-size',source.css('font-size'));
		dest.css('font-family',source.css('font-family'));
		dest.css('font-weight',source.css('font-weight'));
		dest.css('color',source.css('color'));
		dest.css('text-align',source.css('text-align'));
		dest.css('background-color',source.css('background-color'));
		
		dest.css('border', options.border);
	}
	
	function arrayMerge(a, b) 
	{
		if (a) 
		{
			if (b) for(var i in b) a[i] = b[i];
			return a;
		} 
		else 
		{
			return b;		
		}
	};
	
	function HTMLEncode(wText){
		if(typeof(wText)!="string"){
			wText=wText.toString();
		};
		wText=wText.replace(/&/g, "&amp;") ;
		wText=wText.replace(/"/g, "&quot;") ;
		wText=wText.replace(/</g, "&lt;") ;
		wText=wText.replace(/>/g, "&gt;") ;
		return wText;
	};
};