/*

	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) {
			// attaching event
			// addEventListener (for Gecko Engine)
			eval("document.forms[i].addEventListener('submit', function () { gEditorSubmit('"+rtbBoxes.join('.')+"') }, 0)");
		}
	}
	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 = new Object(document.getElementById('gEditor_'+i).contentWindow);
		obj.document.designMode = 'On';

		// this was needed because I can't allow style attribute for unprivileged users...
		eval("var rtbUserLevel = rtbEditor_"+i+"_level;");
		try {
			obj.document.execCommand('styleWithCSS', false, (rtbUserLevel > 1) ? true : false);
		} catch(e) {
			obj.document.execCommand('useCSS', false, (rtbUserLevel > 1) ? false : true);
		}
	}
	rtbMode[i] = m;
}


/*
	Updates Visual editor with Source editor value
*/
function gEditorUpdate(id) {
	var gEditor = new Object(document.getElementById('gEditor_'+id).contentWindow);
	var codeBox = new Object(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, exec, arg) {
	if (!rtbMode[id]) return false;
	var gEditor = new Object(document.getElementById('gEditor_'+id).contentWindow);
	gEditor.document.execCommand(exec, 0, arg);
	gEditor.focus();

	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();
	}
}
window.onload = function () { gEditorInit(); }