var palette=["FFFFFF","FFCCCC","FFCC99","FFFF99","FFFFCC","99FF99","99FFFF",
"CCFFFF","CCCCFF","FFCCFF","CCCCCC","FF6666","FF9966","FFFF66","FFFF33","66FF99",
"33FFFF","66FFFF","9999FF","FF99FF","C0C0C0","FF0000","FF9900","FFCC66","FFFF00",
"33FF33","66CCCC","33CCFF","6666CC","CC66CC","999999","CC0000","FF6600","FFCC33",
"FFCC00","33CC00","00CCCC","3366FF","6633FF","CC33CC","666666","990000","CC6600",
"CC9933","999900","009900","339999","3333FF","6600CC","993399","333333","660000",
"993300","996633","666600","006600","336666","000099","333399","663366","000000",
"330000","663300","663333","333300","003300","003333","000066","330099","330033"];

function pformat(action,param){
	rtec.document.execCommand(action,"",param);
	rtec.focus();
}
function format(action){
	pformat(action,"");
}
function insertImage(url){
var img='<img src="'+url.replace('"','')+'" alt="">';
rtec.focus();
var selection = null, range = null;
var iframeWindow = document.getElementById('rtec').contentWindow;
var iframeDocument = iframeWindow.document;


if(typeof iframeWindow.getSelection != 'undefined'){
selection = iframeWindow.getSelection();

if(typeof selection.getRangeAt != 'undefined'){
range = selection.getRangeAt(0);

}else if(typeof selection.baseNode != 'undefined'){
range = iframeDocument.createRange();
range.setStart(selection.baseNode,selection.baseOffset);
range.setEnd(selection.extentNode,selection.extentOffset);

if(range.collapsed){
range.setStart(selection.extentNode,selection.extentOffset);
range.setEnd(selection.baseNode,selection.baseOffset);
}

}


var rangecopy = range.cloneRange();
var newimg = iframeDocument.createElement('img');
newimg.alt = "";
newimg.src = url;
rangecopy.collapse(true);
range.deleteContents();
rangecopy.insertNode(newimg);
//selection.collapse(newimg,newimg.length);
} else if(typeof iframeDocument.selection != 'undefined'){
selection = iframeDocument.selection;
range = selection.createRange();
range.pasteHTML(img);
}
iframeWindow.focus();
}
function insertEmot(){
    insertImage(this.src.replace("http://realmofforts.com",""));
}
function togglePalette(id){
	if(document.getElementById(id).style.display=="none")
		showPalette(id);
	else
		hidePalette(id);
	rtec.focus();
}
function showPalette(id){
    document.getElementById(id).style.left=mousex+2;
    document.getElementById(id).style.top=mousey+2;
	document.getElementById(id).style.display="block";
}
function hidePalette(id){
	document.getElementById(id).style.display="none";
}
function colorPalette(){
	document.writeln('<div id="palette"><table width="150" height="100" cellpadding="0" cellspacing="1" border="1" align="center"><tr>');
	for(var i=0;i<palette.length;i++){
		document.writeln('<td class="pColor" id="#'+palette[i]+'" bgcolor="#'+palette[i]+'" width="10" height="10"><img width="1" height="1"></td>');
		if(i%10==9 && i!=palette.length-1)
			document.writeln('</tr><tr>');
	}
	document.writeln('</tr></table></div>');
}
function rte_init(){
	hidePalette("palette");
	hidePalette("imgpalette");
	if (document.getElementsByTagName){
		var pal = document.getElementsByClassName('pColor');
        var emot = document.getElementsByClassName('emi');
	}else if (document.all){
		var pal = document.all.classes('pColor');
        var emot = document.all.classes('emi');
    }
	for (var i=0; i < pal.length; i++) {
		pal[i].onmouseover = paletteOver;
		pal[i].onmouseout = paletteOut;
		pal[i].onclick = paletteClick;
	}
    for (var i=0; i < emot.length; i++) {
		emot[i].onclick = insertEmot;
	}
}
function paletteOver(){
	this.style.border='1px dotted white';
	this.style.cursor='pointer';
}
function paletteOut(){
	this.style.border='1px solid gray';
}
function paletteClick(){
	hidePalette("palette");
	pformat("ForeColor",this.id);
}
function sync(){
	document.getElementById("body").value=rtec.document.body.innerHTML;
}
setLoad(rte_init);