var TextWidget = function(	id, 
							name, 
							fckPath,
							getLastRevision, 
							getMLTextareaWidget, 
							saveContent, 
							getRevisionDates, 
							getRevision, 
							revisionsLabel,
							currentLabel
							)
{
	var wDiv = jQuery("#"+id);
	
	/**
	*	Open the editor
	*/
	var openEditor = function()
	{
		wDiv.find(".text").fadeOut("fast", function()
		{
			//Get the html for the editor
			jQuery.get(getMLTextareaWidget, null, function(data)
			{
				//Build the editor form
				wDiv.find(".text").html("<form id=\""+id+"-form\" action=\""+saveContent+"\" method=\"post\">"+data+"</form>");
				
				//Find each tab and insert the revisions list at the end
				wDiv.find(".text div div").each(function(key, element)
				{
					element = jQuery(element);
                    try{
                        var locale = element.find("input[name^=\""+name+"\"]").attr("name").split("[")[1];
                    }
                    catch(e)
                    {
                        locale = '';
                    }
					locale = locale.substr(0, locale.length-1);
					
					//Get the revision dates to insert
					jQuery.post(getRevisionDates, {locale: locale}, function(data)
					{
						/*
							Build this html:
							
							<div class="Revisions">
								<span>[revisionsLabel]</span>
								<ul>
									<li><a class="RevisionDate" href="javascript:void(null)" name="[timestamp].[locale]">[date] ([currentLabel])</a></li>
									<li><a class="RevisionDate" href="javascript:void(null)" name="[timestamp].[locale]">[date]</a></li>
									...
								</ul>
							</div>
						*/
						var html = "<div class=\"revisions\"><span>"+revisionsLabel+"</span><ul>";
						jQuery(data).each(function(key, value)
						{
							html += "<li><a class=\"revision-date\" href=\"javascript:void(null)\" name=\""+value.timestamp+"."+locale+"\">"+value.date + ((key==0)?" ("+currentLabel+")":'') + "</a></li>";
						});
						
						html += "</ul></div>";
						
						element.append(html);
					}, "json");
				});
				
				//Attach a click handler to the revision links
				wDiv.find(".revisions a.revision-date").live("click", function(event)
				{
					var a = jQuery(this);
					//Get the timestamp and the locale from the name attribute
					var timestamp = a.attr("name").split(".");
					var locale = timestamp[1];
					timestamp = timestamp[0];
					
					//Get the text at this revision date from the server
					jQuery.post(getRevision, {timestamp:timestamp, locale:locale}, function(data)
					{
						var editor = FCKeditorAPI.GetInstance(name+"["+locale+"]");
						if(editor)
						{
							editor.SetHTML(data);
						}
						
					}, "text");
				});
				
			}, "html");
		}).parent().prepend("<span class=\"loading\"></span>");
	}
	this.openEditor = openEditor;
	
	/**
	*	The cancel button
	*/
	wDiv.find(".cancel").click(function()
	{
		//Hide the buttons
        wDiv.find(".cancel").hide();
        wDiv.find(".save").hide();
        wDiv.find(".text").fadeOut("fast", function()
			{
				//Get the latest text
				jQuery.getJSON(getLastRevision, null, function(data)
				{
					wDiv.find(".loading").remove();
					
					//Fix the googlemaps bug...
					rx = /http:\/\/maps.google.com/i;
					if(rx.test(data.text))
					{
						window.location = window.location;
					}
					
					wDiv.find(".text").fadeIn("fast").html(data.text);
				});
			});
	});
	
	/**
	*	Save button
	*/
	wDiv.find(".save").click(function()
	{
		//Get all the hidden inputs that fck has
		wDiv.find("input[name^=\""+name+"\"]").each(function(id, element)
		{
			//The input name is also the instance name so get that and then get the editor instance
			var instanceName = jQuery(element).attr("name");
			var editor = FCKeditorAPI.GetInstance(instanceName);
			var val = editor.GetHTML();
			//Set the value of the hidden input to watever is in the editor
			jQuery(element).val(val);
		});
		
		//Submit the form (only the hidden inputs)
		wDiv.find("form#"+id+"-form").ajaxSubmit({complete:function(data)
		{
			wDiv.find(".cancel").click();
		}});
		
		return false;
	});
	
	//Initial state
	wDiv.find(".text").dblclick(openEditor);
	wDiv.find(".cancel").hide();
	wDiv.find(".save").hide();
}

window.FCKeditor_OnComplete = function(editorInstance)
{
    var name = editorInstance.Name;
    var divId = name.split('[')[0];

    var textDiv = jQuery("#"+divId);
    if(textDiv.find("form div input[name='"+name+"']").length > 0)
    {
   		textDiv.parent().find(".loading").remove();
		textDiv.parent().find(".save").show().parent().find(".cancel").show();
		textDiv.fadeIn("fast");
	}
}
