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;
	}
});

Ajax.InPlaceCalendarEditor = Class.create();
Object.extend(Ajax.InPlaceCalendarEditor.prototype, Ajax.InPlaceEditor.prototype);
Object.extend(Ajax.InPlaceCalendarEditor.prototype,
{
	createSelectBox: function(collection, value)
	{
		var selectTag = document.createElement("select");

		selectTag.onchange = function(e)
		{
			var fields = this.parentNode.getElementsByTagName('INPUT');
			for (i = 0; i < fields.length; i++)
			{
				var field = fields[i];
				if (field.type == 'checkbox')
				{
					field.checked = false;
				} 
			}
		}

		collection.each(function(e, i)
		{
			optionTag = document.createElement("option");
			optionTag.value = (e instanceof Array) ? e[0] : e;

			var optionName = (e instanceof Array) ? e[1] : e;

			if (value == optionTag.value)
				optionTag.selected = true;

			optionTag.appendChild(document.createTextNode(optionName));
			selectTag.appendChild(optionTag);
		}.bind(this));

		return selectTag;
	},
	createCheckBox: function(value)
	{
		var labelTag = document.createElement("label");

		checkTag = document.createElement("input");
		checkTag.type = "checkbox";
		checkTag.id = "valueCheck";
		checkTag.checked = value;

		labelTag.appendChild(checkTag);
		labelTag.appendChild(document.createTextNode(' never'));

		return labelTag;
	},
	createEditField: function()
	{
		if (!this.cached_Div)
		{
			var parts = this.options.value.split('-');

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

			var days = new Array();

			for (var i = 1; i <= 31; i++)
				days[days.length] = [(i < 10) ? '0' + i : i, i];

			theDiv.appendChild(this.createSelectBox(days, parts[0]));

			var months = [['01', 'January'], ['02', 'February'], ['03', 'March'], ['04', 'April'], ['05', 'May'], ['06', 'June'], ['07', 'July'], ['08', 'August'], ['09', 'September'], ['10', 'October'], ['11', 'November'], ['12', 'December']];

			theDiv.appendChild(this.createSelectBox(months, parts[1]));

			var years = new Array();

			for (var i = 1900; i <= 2020; i++)
				years[years.length] = [(i < 10) ? '0' + i : i, i];

			theDiv.appendChild(this.createSelectBox(years, parts[2]));

			if (this.options.never)
				theDiv.appendChild(this.createCheckBox(parts[0] == '0000'));

			this.cached_Div = theDiv;
		}

		this.editField = this.cached_Div;

		if (this.options.loadTextURL)
			this.loadExternalText();

		this.form.appendChild(this.editField);

		this.options.callback = function(form, value)
		{
			return "value=" + encodeURIComponent(value);
		}
	},
	onSubmit: function()
	{
		// onLoading resets these so we need to save them away for the Ajax call
		var form = this.form;
//		var value = this.editField.value;

		var controls = this.editField.getElementsByTagName('select');

		var value = controls[2].value + '-' + controls[1].value + '-' + controls[0].value;

		if (this.options.never)
		{
			var check = this.editField.getElementsByTagName('input')[0];
			if (check.checked)
				value = '0000-00-00';
		}

		// 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: null
				}, 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;
	}
});

Ajax.CheckBoxUpdater = Class.create();
Object.extend(Ajax.CheckBoxUpdater.prototype, Ajax.Updater.prototype);
Object.extend(Ajax.CheckBoxUpdater.prototype,
{
	updateContent : function() {

	    var receiver = this.success() ?
    	  this.container.success : this.container.failure;
    	  
		var response = this.transport.responseText;
		
	    if (receiver) {
			new Effect.Highlight(receiver.parentNode);
	    }
	    
	    if (this.success()) {
	      if (this.onComplete)
	        setTimeout(this.onComplete.bind(this), 10);
	    }
	}
});

function checkboxUpdater(obj, uri)
{
	new Ajax.CheckBoxUpdater(obj, uri,
		{
			parameters : 'value=' + (obj.checked ? obj.value : 0),
			method : 'get'
		}
	)
}

view_inline = function(anachor){
	var listElement = anachor.parentNode.parentNode;
	var view = $(listElement.id+"_view");
	if(view)
	{
		new Effect.toggle(view, "appear", {duration: 0.5,afterFinish :function(e){e.element.style.opacity=""}});
	}
	else
	{
		view = document.createElement("div");
	    view.id = listElement.id+"_view";
	    view.style.display="none";
	    view.style.backgroundColor="#fff";
	    view.style.padding="0px 10px";
	    view.style.border="1px solid #F6F6F6";
	    view.style.margin="2px -3px -3px -3px";
	    listElement.appendChild(view);
	    new Ajax.Updater(
			view,
			anachor.href.replace(/#.*$/, ""),
			{
				evalScripts : true,
				onSuccess : function() { new Effect.toggle(view, "appear", {duration: 0.5,afterFinish :function(e){e.element.style.opacity=""}});}
			}
		);
	}
	return false;
}
