/*
--------------------------------------------------------------------------------
	Author(s): Music Bay AB, www.musicbay.se
	Created: 2007-01-22
	Last modified: 2007-01-28
--------------------------------------------------------------------------------
* 
* TextEditor AJAX component
* 
* Example:
* 
* <div class="textEditor">
* 	<input class="textEditorFields" type="hidden" value="tid,title,content" />
* 	<input class="textEditorURL" type="hidden" value="/xml/editText.php" />
* 	<input class="textEditor[tid]" type="hidden" value="0" />
* 	<h1 class="textEditor[title]">Välkommen</h1><input type="text" class="textEditor[title]" style="display: none">
* 	<p class="textEditor[content]">Testcontent</p>
* 	<textarea class="textEditor[content]" style="display: none;"></textarea>
* 	<a href="#" class="textEditorEdit">Edit</a>
* 	<a href="#" class="textEditorSave">Save</a>
* 	<a href="#" class="textEditorCancel">Cancel</a>
* </div>
*/

function TextEditorField(fieldName, onElement, offElement) {
	
	this.fieldName = fieldName;
	this.onElement = onElement;
	this.offElement = offElement;
}

/* Should be replaced with appropriate function */
function showTextEditorElement(element) {
	
	showElement(element);
}

/* Should be replaced with appropriate function */
function hideTextEditorElement(element) {
	
	hideElement(element);
}

function TextEditor(editor) {
	
	/* Target URL */
	var targetURL = editor.getElementsByClassName("textEditorURL");
	if(targetURL.length != 1) return textEditorError("There must be exactly one textEditorURL");
	else targetURL = targetURL[0].value;
	
	var editButtons = editor.getElementsByClassName("textEditorEdit");
	var cancelButtons = editor.getElementsByClassName("textEditorCancel");
	var saveButtons = editor.getElementsByClassName("textEditorSave");
	
	/* Extract fields */
	var fields = new Array();
	{
		var f = editor.getElementsByClassName("textEditorFields");
		if(f.length != 1) return textEditorError("Not exactly 1 textEditorFields element for textEditor " + i + "!");
		fields = new String(f[0].value).split(',');
			
		for(var j = 0; j < fields.length; j++) {
				
			var elements = editor.getElementsByClassName("textEditor_" + fields[j]);

			if(elements.length == 2) fields[j] = new TextEditorField(fields[j], elements[1], elements[0]);
			else if(elements.length == 1) fields[j] = new TextEditorField(fields[j], null, elements[0]);
			else return textEditorError("textEditor_" + fields[j] + " must exist 1 or 2 times in editor (now is " + elements.length + " times)");
		}
	}
	
	/* Enable edit buttons */
	var editFunction = function() {
			
		for(var j = 0; j < fields.length; j++) {
				
			if(fields[j].onElement != null) {

				var content = fields[j].offElement.innerHTML;
				var regexp = new RegExp("<br.{0,2}>", "gi");
				content = content.replace(regexp, "\n");

				fields[j].onElement.value = content;
				showTextEditorElement(fields[j].onElement);
				hideTextEditorElement(fields[j].offElement);
			}
		}
				
		for(var j = 0; j < saveButtons.length; j++) showTextEditorElement(saveButtons[j]);
		for(var j = 0; j < cancelButtons.length; j++) showTextEditorElement(cancelButtons[j]);
		for(var j = 0; j < editButtons.length; j++) hideTextEditorElement(editButtons[j]);
	}
	for(var j = 0; j < editButtons.length; j++) {

		showTextEditorElement(editButtons[j]);	
		editButtons[j].onclick = function() { editFunction(); hideTextEditorElement(this); }
	}
		
	/* Enable cancel buttons */
	var cancelFunction = function() {

		for(var j = 0; j < fields.length; j++) {
				
			if(fields[j].onElement != null) {
				
				hideTextEditorElement(fields[j].onElement);
				showTextEditorElement(fields[j].offElement);
			}
		}
				
		for(var j = 0; j < saveButtons.length; j++) hideTextEditorElement(saveButtons[j]);
		for(var j = 0; j < cancelButtons.length; j++) hideTextEditorElement(cancelButtons[j]);
		for(var j = 0; j < editButtons.length; j++) showTextEditorElement(editButtons[j]);
	}
	for(var j = 0; j < cancelButtons.length; j++) {
			
		hideTextEditorElement(cancelButtons[j]);
		cancelButtons[j].onclick = cancelFunction;	
	}
		
	/* Enable save buttons */
	var saveFunction = function() {
			
		/* Convert fields to parameter array */
		var parameters = new Array(fields.length);
		for(var j = 0; j < fields.length; j++) {

			if(fields[j].onElement != null) {
				
				var content = fields[j].onElement.value;
				var regexp = new RegExp("\\n", "gi");
				content = content.replace(regexp, "<br />");
				
				parameters[fields[j].fieldName] = content;
			}
			else parameters[fields[j].fieldName] = fields[j].offElement.value;
		}

		new Ajax.Request(targetURL, {
			method:'post',
			parameters: parameters,
			onSuccess: function(transport) {
					
				for(var j = 0; j < fields.length; j++) {
				
					if(fields[j].onElement != null) {

						var content = fields[j].onElement.value;
						var regexp = new RegExp("\\n", "gi");
						content = content.replace(regexp, "<br />");

						fields[j].offElement.innerHTML = content;

						hideTextEditorElement(fields[j].onElement);
						showTextEditorElement(fields[j].offElement);
					}
				}
						
				for(var j = 0; j < saveButtons.length; j++) hideTextEditorElement(saveButtons[j]);
				for(var j = 0; j < cancelButtons.length; j++) hideTextEditorElement(cancelButtons[j]);
				for(var j = 0; j < editButtons.length; j++) showTextEditorElement(editButtons[j]);
			},
			onFailure: function() {
					
				textEditorError('Error saving text')
			}
		});
	}
	for(var j = 0; j < saveButtons.length; j++) {
		
		hideTextEditorElement(saveButtons[j]);	
		saveButtons[j].onclick = saveFunction;
	}
}

function textEditorError(message) {
	
	alert("TextEditor error: " + message);
}

function initTextEditors() {
	
	var editors = document.getElementsByClassName("textEditor");
	
	for(var i = 0; i < editors.length; i++) editors[i] = new TextEditor(editors[i]);
}