Ajax.InPlaceRichEditor = Class.create();
Object.extend(Ajax.InPlaceRichEditor.prototype, Ajax.InPlaceEditor.prototype);
Object.extend(Ajax.InPlaceRichEditor.prototype,
{
	convertHTMLLineBreaks : function(text)
	{
		return text;
	},
	getText: function()
	{
		if (this.options.rows == 1)
			return this.element.innerHTML.replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"').replace(/&#039;/g, '\'').replace(/&amp;/g, '&');
		else
			return this.element.innerHTML;
	},
	enterEditMode: function(evt)
	{
		if (this.saving) return;
		if (this.editing) return;

		this.editing = true;
		this.onEnterEditMode();

		if (this.options.externalControl)
		{
			Element.hide(this.options.externalControl);
		}

		Element.hide(this.element);
		this.createForm();
		this.element.parentNode.insertBefore(this.form, this.element);
		Field.scrollFreeActivate(this.editField);

		if (this.options.textarea)
		{
			Element.addClassName(this.editField, 'input');
			tinyMCE.addMCEControl(this.editField, 'value');
		}
		else
		{
			Element.addClassName(this.editField, 'short');
			this.editField.style.backgroundColor = '';
		}

		// stop the event to avoid a page refresh in Safari
		if (evt)
		{
			Event.stop(evt);
		}
		return false;
	},
	onclickCancel: function()
	{
		if (this.options.textarea)
		{
			tinyMCE.removeMCEControl('value');
		}

		this.onComplete();
		this.leaveEditMode();
		return false;
	},
	onSubmit: function()
	{
		// onLoading resets these so we need to save them away for the Ajax call
		var form = this.form;

		if (this.options.textarea)
		{
			var tinyVal = tinyMCE.getContent('value');

			if (tinyVal)
				this.editField.value = tinyVal;

			tinyMCE.removeMCEControl('value');
		}

		var value = this.editField.value;

		// do this first, sometimes the ajax call returns before we get a chance to switch on Saving...
		// which means this will actually switch on Saving... *after* we've left edit mode causing Saving...
		// to be displayed indefinitely
		this.onLoading();

		if (this.options.evalScripts)
		{
			new Ajax.Request(
				this.url, Object.extend({
				parameters: this.options.callback(form, value),
				onComplete: this.onComplete.bind(this),
				onFailure: this.onFailure.bind(this),
				asynchronous:true, 
				evalScripts:true
				}, this.options.ajaxOptions));
		} else  {
			new Ajax.Updater(
				{ success: this.element,
				// don't update on failure (this could be an option)
				failure: this.element }, 
				this.url, Object.extend({
				parameters: this.options.callback(form, value),
				onComplete: this.onComplete.bind(this),
				onFailure: this.onFailure.bind(this)
			}, this.options.ajaxOptions));
		}
		// stop the event to avoid a page refresh in Safari
		if (arguments.length > 1)
		{
			Event.stop(arguments[0]);
		}
		return false;
	}
});

tinyMCE.init({
    mode: 'none',
    theme: "advanced",
    theme_advanced_disable: "image,strikethrough",
    theme_advanced_buttons1: "undo,redo,|,cut,copy,paste,|,formatselect",
    theme_advanced_buttons2: "bold,italic,underline,|,bullist,numlist,|,justifyleft,justifycenter,justifyright,justifyfull,|,link,unlink,|,hr,|,outdent,indent,|,cleanup",
    theme_advanced_buttons3: "",
    theme_advanced_toolbar_location: "top",
    theme_advanced_toolbar_align: "left",
    theme_advanced_path: false,
    theme_advanced_statusbar_location: "bottom", 
    theme_advanced_resizing: false
    });

Element.observe(window, 'load', function () {
        var edittexts = $(document).getElementsByClassName('editText');
        for (var i = 0; i < edittexts.length; i++) {
            new Ajax.InPlaceRichEditor(edittexts[i], '/tinycms/save/' + edittexts[i].id + '/', {rows: 20});
        }
    });
