
	// CVS: $Id: rtbEditor.js,v 1.1.1.1 2006/02/20 21:52:21 xiam Exp $
/*

	Gekko's rtbEditor for Mozilla Firefox
	(c) 2005 by J. C. Nieto <xiam@users.sourceforge.net>
	This program is Free Software

	Check:
	http://www.mozilla.org/editor/midas-spec.html

*/
var rtbMode = Array();
function gEditorInit() {
	var i = j = new Number(0);
	/*
		Checking every <form> for an rtbEditor codeBox.
		If an element with class rtbEditor is found, then when attach
		a function to form.onsubmit event that checks for an update
		on codeBox before posting in.
	*/
	for (i = 0; i < document.forms.length; i++) {
		var rtbBoxes = new Array();
		var n = new Number(0);
		for (j = 0; j < document.forms[i].elements.length; j++) {
			// searching for rtbBoxes in the current form
			if (document.forms[i].elements[j].className.toLowerCase() == 'rtbeditor') {
				tmp_id = document.forms[i].elements[j].id;
				rtbBoxes[n] = new String(tmp_id.substr(tmp_id.indexOf('_')+1));
				n++;
			}
		}
		if (rtbBoxes.length) {
			eval("setEventListener(document.forms["+i+"], 'submit', function () { gEditorSubmit('"+rtbBoxes.join('.')+"') })");
		}
	}
	i = 0;
	/*
		Will checks for every gEditor_n element and will put iframe in designMode
	*/
	while (document.getElementById('gEditor_'+i)) {
		//var gEditor = new Object(document.getElementById('gEditor_'+i).contentWindow);
		//var codeBox = new Object(document.getElementById('codeBox_'+i));
		//gEditor.document.designMode = 'On';
		//gEditor.addEventListener('keypress', mozKeys, true);

		m = (document.getElementById('rtbEditor_'+i).style.display == 'block') ? 1 : 0;

		rtbSetMode(i, m, (!m));

		i++;
	}
}
function show(obj) {
	obj = document.getElementById(obj);
	obj.style.display = 'block';
}
function hide(obj) {
	obj = document.getElementById(obj);
	obj.style.display = 'none';
}
function rtbSetMode(i, m, u) {

	sobj = document.getElementById('rtbSource_'+i);
	vobj = document.getElementById('rtbEditor_'+i);

	(u ? u : m) ? gEditorUpdate(i) : gEditorUpdateCode(i);

	vobj.style.display = (m) ? 'block' : 'none';
	sobj.style.display = (m) ? 'none' : 'block';

	if (m) {
		var obj = document.getElementById('gEditor_'+i).contentWindow;

		switch(getBrowserEngine()) {
			case 'msie':
				obj.document.body.contentEditable = true;
			break;
			default:
				obj.document.designMode = 'On';
			break;
		}

		try {
			obj.document.execCommand('useCSS', false, 0);
		} catch (e) {
			try {
				obj.document.execCommand('styleWithCSS', false, 1);
			} catch(e) {}
		}
	}
	rtbMode[i] = m;
}

/*
	Updates Visual editor with Source editor value
*/
function gEditorUpdate(id) {
	var gEditor = document.getElementById('gEditor_'+id).contentWindow;
	var codeBox = document.getElementById('codeBox_'+id);
	gEditor.document.body.innerHTML = codeBox.value;
}
/*
	Updates Source editor with Visual editor value
*/
function gEditorUpdateCode(id) {
	var gEditor = new Object(document.getElementById('gEditor_'+id).contentWindow);
	var codeBox = new Object(document.getElementById('codeBox_'+id));
	codeBox.value = gEditor.document.body.innerHTML;
}
/*
	If Visual editor is focused at submit time, updates Source editor
*/
function gEditorSubmit(b) {
	b = b.split('.');
	for (i = 0; i < b.length; i++) {
		if (rtbMode[b[i]])
			gEditorUpdateCode(b[i]);
	}
	return true;
}
/*
	Performs an execCommand on Visual editor mode
*/
function gExec(id, command, arg) {
	if (!rtbMode[id]) return false;
	var gEditor = document.getElementById('gEditor_'+id).contentWindow;

	gEditor.focus();

	try {
		gEditor.document.execCommand(command, 0, arg);
	} catch (e) {
		switch (command) {
			case 'inserthtml':
				var nrange = gEditor.document.selection.createRange();
				nrange.pasteHTML(arg);
				nrange.collapse(false);
				nrange.select();
			break;
			default:
				alert('Not supported in this browser.');
			break;
		}
	}
	return false;
}
/*
	TODO: surround selected html
*/
function rtbAlignObject(id) {
	var gEditor = new Object(document.getElementById('gEditor_'+id).contentWindow);
	gEditor.focus();
	var sel = gEditor.getSelection();
	var rng = sel.getRangeAt(0);
	var frg = rng.cloneContents();
	gEditor.document.body.appendChild(frg);
}
/*
	TODO: mozKeys
*/
function mozKeys(e) {
	if (e.ctrlKey) {
		c = String.fromCharCode(e.charCode).toUpperCase();
		switch (c) {
			case 'B': gExec('bold'); break;
			case 'U': gExec('underline'); break;
			case 'I': gExec('italic'); break;
			case 'S': gExec('strikethrough'); break;
		}
		e.preventDefault();
	}
}
function uploadImage(id) {
	window.open(_C_SITE_REL_URL+'runscript.php?action=uploadimage&object='+id, '', 'width=340,height=420');
}
function insertSmiley(id) {
	var p = popupCreate();
	popupLoadHTML(p, _C_SITE_REL_URL+'runscript.php?action=insertsmiley&editor_id='+id);
	popupDisplay(p);
}
setEventListener(window, 'load', function() { gEditorInit(); });
