if (typeof Ajax != 'undefined')
{
	// Save these default InPlaceEditor functions that we are going to override so that if upstream is changed,
	// we don't have to change the methods below.

	var parentAjaxInPlaceEditorGetText = Ajax.InPlaceEditor.prototype.getText;
	var parentAjaxInPlaceEditorRemoveForm = Ajax.InPlaceEditor.prototype.removeForm;

	/*
	 * Extend the InPlaceEditor so we get the spinner and can handle validation errors.
	 */
	Object.extend(Ajax.InPlaceEditor.prototype,{
		/**
		 * Overriden to take care of HTML entities
		 */
		getText: function()
		{
			var text = parentAjaxInPlaceEditorGetText.bind(this)();

			text = text.replace(/&amp;/gi, "&");
			text = text.replace(/&quot;/gi, "\"");
			text = text.replace(/&nbsp;/gi, " ");
			text = text.replace(/&lt;/gi, "<");
			text = text.replace(/&gt;/gi, ">");
			text = text.replace(/&#39;/gi, "'");

			return text;
		},
		/**
		 * Overriden to wrap the form in a table, and table in a DIV
		 * to gain control of viewport space
		 **/
		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();

			// If specified, change input name

			if (this.options.inputName)
			{
				this.editField.name = this.options.inputName;
			}

			// Wrap form in a DIV

			var div = document.createElement("div");

			div.appendChild(this.form);

			// Wrap DIV in a table

			var table = document.createElement('table');
			var row = document.createElement('tr');
			var cell = document.createElement('td');

			cell.appendChild(div);
			row.appendChild(cell);
			table.appendChild(row);

			cell.style.border = "0px";
			row.style.border = "0px";
			table.style.border = "0px";

			// Set table as our real element

			this.newElement = table;

			this.element.parentNode.insertBefore(this.newElement, this.element);

			if (!this.options.loadTextURL)
			{
				Field.scrollFreeActivate(this.editField);
			}

			// stop the event to avoid a page refresh in Safari

			if (evt)
			{
				Event.stop(evt);
			}

			return false;
		},
		/**
		 * Overriden since our editor element has been wrapped in a table
		 */
		removeForm: function()
		{
			parentAjaxInPlaceEditorRemoveForm.bind(this)();

			if(this.newElement)
			{
				if (this.newElement.parentNode)
				{
					Element.remove(this.newElement);
				}

				this.newElement = null;
			}
		}
	});

	Ajax.InPlaceCollectionEditor = Class.create();
	Object.extend(Ajax.InPlaceCollectionEditor.prototype, Ajax.InPlaceEditor.prototype);
	Object.extend(Ajax.InPlaceCollectionEditor.prototype, {
	  createEditField: function() {
	    if (!this.cached_selectTag) {
	      var selectTag = document.createElement("select");
	      var collection = jQuery(this.options.collection || []);
	      var optionTag;
	      
	      collection.each(function(i, e) {
	        optionTag = document.createElement("option");

	        if (e.value != undefined && e.text != undefined && e.text instanceof Array && e.text.length == 2)
	        {
	        	e.value = e.text[0];
	        	e.text = e.text[1];
	        }

	        if (e.value != undefined)
	        {
	        	optionTag.value = e.value;
	        }
	        else
	        {
	        	optionTag.value = (e instanceof Array) ? e[0] : e;
	        }

	        if((typeof this.options.value == 'undefined') && ((e instanceof Array) ? this.element.innerHTML == e[1] : (e.text != undefined && e.text == this.element.innerHTML || e == optionTag.value)))
	        {
	        	optionTag.selected = true;
	        }

	        if(this.options.value==optionTag.value)
	        {
	        	optionTag.selected = true;
	        }

	        if (e.text != undefined)
	        {
	        	optionTag.appendChild(document.createTextNode(e.text));
	        }
	        else
	        {
	        	optionTag.appendChild(document.createTextNode((e instanceof Array) ? e[1] : e));
	        }

	        selectTag.appendChild(optionTag);
	      }.bind(this));
	      this.cached_selectTag = selectTag;
	    }

	    this.editField = this.cached_selectTag;
	    if(this.options.loadTextURL) this.loadExternalText();
	    this.form.appendChild(this.editField);

	    var inputName = (this.options.inputName || "value");

	    this.options.callback = function(form, value) {
	    	return encodeURIComponent(inputName) + "=" + encodeURIComponent(value);
	    }
	  }
	});

	function setupAjaxRadioButtons(el, url, options)
	{
		options = Object.extend({
			callback: function(form)
			{
				var value = form.val;

				jQuery(form).find('input[type=radio]').each(function()
				{
					if (this.checked)
					{
						value = this.value;
					}
				});

				return encodeURIComponent(inputName) + "=" + encodeURIComponent(value);
			}
		}, (options || {}));

		var inputName = (options.inputName || "value");
		var editor = new Ajax.InPlaceEditor(el, url, options);

		Object.extend
		(
			editor,
			{
				createEditField: function()
				{
					// Create a DIV

					var div = document.createElement("div");

					// Add elements

					for (i = 0; i < options['collection'].length; i++)
					{
						var optionValue = options['collection'][i]['value'];
						var optionText = options['collection'][i]['text'];

						// Create radio

						var radio = document.createElement('input');

						radio.type = 'radio';
						radio.name = 'value';
						radio.value = optionValue;

						if (optionValue == options['value'])
						{
							radio.checked = true;
						}

						// Create text describing radio

						var span = document.createElement('span');

						span.innerHTML = optionText;

						// Break a line (if necessary)

						if (i > 0)
						{
							div.appendChild(document.createElement('br'));
						}

						// Add elements to form

						div.appendChild(radio);
						div.appendChild(span);
					}

					// Wrap DIV in a TABLE

					var table = document.createElement('table');
					var row = document.createElement('tr');
					var cell = document.createElement('td');

					cell.appendChild(div);
					row.appendChild(cell);
					table.appendChild(row);

					cell.style.border = "0px";
					row.style.border = "0px";
					table.style.border = "0px";

					this.form.appendChild(table);

					// Setup values

					this.form.val = options['value'];
					this.editField = this.form.childNodes[0];
				}
			}
		);
	}
}
