var currentEditor = null;
var richEditorHovered = false;

function richEditor(targetElement) {
	/*
	 * jQuery object for the target
	 */
	this.target = targetElement;
	/*
	 * container of the target element
	 */
	this.containerEditor = this.target.parent();
	this.mceObj = null; // tinymce object
	this.charLimit = 9500;
	this.tooManyCharsMessage = "The question text contains too many characters. Modifications were not saved.";
	this.hovered = false; // whether the richeditor is hovered or not
	this.charCount = -1; // last read character count
	this.content = ""; // last read content

	this.clean = function clean() {
		for (edId in tinyMCE.editors) {
			tinyMCE.editors[edId].remove;
		}
	};

	this.enable = function enable() {
		this.clean();
		if (this.target != null) {
			tinyMCE.execCommand('mceAddControl', true, this.target.attr("id"));
			this.mceObj = tinyMCE.get(this.target.attr("id"));
		}
		this.containerEditor.find(".mceEditor").hover(function() {
			this.hovered = true;
			richEditorHovered = this.hovered;
		}, function() {
			this.hovered = false;
			richEditorHovered = this.hovered;
		});
		richEditorHovered = true;
		return true;
	};

	this.disable = function disable() {
		if ((this.target != null) && (this.mceObj != null)) {
			this.content = this.mceObj.getContent();
			this.charCount = this.content.length;
			if (this.charCount > this.charLimit) {
				messagesShow(this.tooManyCharsMessage);
				return false;
			}
			this.disableOnblur();

			if (this.mceObj.isDirty()) {
				this.save();
			}
			this.mceObj.remove();
			tinyMCE.execCommand('mceRemoveControl', true, this.target
					.attr("id"));
			this.mceObj = null;
			this.containerEditor.find("input[type='hidden'][name='"+this.target.attr("id")+"']").remove();
		}
		return true;
	};
	this.enableOnblur = function enableOnblur() {
		jQuery(document).bind("click.richClick", function(event) {
			var mceParent = jQuery(event.target).parents(".mceListBoxMenu");
			if ((!richEditorHovered) && (mceParent.length == 0)) {
				currentEditor.disable();
			}
		});
	};
	this.disableOnblur = function disableOnblur() {
		jQuery(document).unbind("click.richClick");
	};
	this.save = function save() {
		this.target.val(this.content);
		this.changed = false;

		var id = this.target.attr("id");
		id = id.substring(id.lastIndexOf('_') + 1, id.length);
		if (id == "headerBody") {
			sync("header", 1, this.target);
		} else {
			sync("question", id, this.target);
		}
	};
};

function enableRichEditorFromRichDisplay(jqObj) {
	if (cleanup()) {
		currentEditor = new richEditor(jqObj);
		currentEditor.enable();
		currentEditor.enableOnblur();
	}
}

function cleanup() {
	if (currentEditor != null) {
		if (currentEditor.disable()) {
			currentEditor = null;
		} else {
			return false;
		}
	}
	return true;
}

function callSortable(entityType, pageUid, parentEntityUid, uidList) {
	Seam.Component.getInstance("surveyCreatorAction")
			.getSortableChanges(entityType, pageUid, parentEntityUid, uidList,
					callSortableCallback);
}
function callSortableCallback(result) {
	if (result == "false") {
		// alert("resorting failed");
	}
}
function callDeleteListener(entityName, entityId) {
	Seam.Component.getInstance("surveyCreatorAction").deleteListener(
			entityName, entityId, callDeleteListenerCallback);
}
function callDeleteListenerCallback(result) {
	if (result == false) {
		// alert("delete failed ");
	}
}
var timerKeepAlive = null;
function keepAlive(page) {
	if (page == "creator") {
		timerKeepAlive = setInterval("callKeepAlive()", 90000);
	}
}
function callKeepAlive() {
	Seam.Component.getInstance("surveyCreatorAction").keepAlive(
			callKeepAliveCallback);
}
function callKeepAliveCallback(result) {
	// keep alive finished
}
function updateAnswerSize(prefix,answerUid)
{
	var answer=jQuery("#"+prefix+answerUid);
	var configuration=answer.width()+" "+answer.height();
	setConfiguration(answerUid, configuration);
}
function setConfiguration(answerUid, configuration) {
	Seam.Component.getInstance("surveyCreatorAction").setConfiguration(
			answerUid, configuration, setConfigurationCallback);
}
function setConfigurationCallback(data) {
}
