﻿function smile2edit(textid, smile, replace) {
	var itext;
	var tedit = null;

	if (replace == 1)
		itext = "<img class='wpml_ico' src='" + smile + "' />";
	else
		itext = " " + smile + " "; // add space to separate smilies

	if (typeof tinyMCE != "undefined")
		tedit = tinyMCE.get('content');

	if (tedit == null || tedit.isHidden() == true) {
		tarea = document.getElementById(textid);
		insert_text(itext, tarea);
	} else if ((tedit.isHidden() == false) && window.tinyMCE) {
		window.tinyMCE.execInstanceCommand('content', 'mceInsertContent', false, itext);
	}
}

function smile2comment(textid, smile, replace) {
	tarea = document.getElementById(textid);
	if (tarea == null) {
		alert('wp-monalisa: Textarea not found. Please contact the webmaster of this site.');
		return;
	}
	if (replace == 1)
		insert_text("<img class='wpml_ico' src='" + smile + "' />", tarea);
	else
		insert_text(" " + smile + " ", tarea); // add space to separate smilies
}

function insert_text(stxt, obj) {
	try {
		if (typeof window.CKEDITOR.instances.comment != 'undefined') {
			window.CKEDITOR.instances.comment.insertHtml(stxt);
			return;
		}

		if (typeof CKEDITOR.instances.content != 'undefined') {
			CKEDITOR.instances.content.insertHtml(stxt);
			return;
		}
	}
	catch (e) { }

	if (document.selection) {
		obj.focus();
		document.selection.createRange().text = stxt;
		document.selection.createRange().select();
	}
	else if (obj.selectionStart || obj.selectionStart == '0') {
		intStart = obj.selectionStart;
		intEnd = obj.selectionEnd;
		obj.value = (obj.value).substring(0, intStart) + stxt + (obj.value).substring(intEnd, obj.value.length);
		obj.selectionStart = obj.selectionEnd = intStart + stxt.length;
		obj.focus();
	}
	else {
		obj.value += stxt;
	}
}
