/***********************************************************
 * Author Chris Hobden <chris@impera.co.uk>                *
 * Copyright (c) Oshun Ltd, 2003-2005. All rights reserved.*
 * See http://www.imperal.co.uk/licence/                   *
 ***********************************************************/

function isisHTMLEditor(fid, isisjslib_root){
	this.holder=document.getElementById(fid);
	this.root=isisjslib_root ? isisjslib_root : '/isisjslib/';
	this.init();
}
isisHTMLEditor.prototype.execCommand = function(e, cmdID, UI, param) {
	var editor = this;
	if(!e.srcElement){
		e.srcElement=e.target;
	}
	this.focus();
	cmdID = cmdID.toLowerCase();
	switch(cmdID){
		case "bold":
		case "underline":
		case "italic":
		case "justifyleft":
		case "justifycenter":
		case "justifyright":
		case "cut":
		case "copy":
		case "paste":
		case "inserthorizontalrule":
		case "indent":
		case "outdent":
		case "removeformat":
		case "insertorderedlist":
		case "insertunorderedlist":
		case "unlink":
		if(editor.buttonList[cmdID].enabled){
			if(this.editorFrame.contentWindow.document.queryCommandEnabled(cmdID)){
				this.editorFrame.contentWindow.document.execCommand(cmdID, UI, param);
			}
		}
		break;
	}
};
isisHTMLEditor.prototype.init=function(){
	var buttons = [
	["button", "bold", "Bold", "images/editor_bold.gif"],
	["button", "underline", "Underline", "images/editor_underline.gif"],
	["button", "italic", "Underline", "images/editor_italic.gif"],
	["devider"],
	["button", "justifyleft", "Allign Left", "images/editor_alignleft.gif"],
	["button", "justifycenter", "Allign Center", "images/editor_aligncenter.gif"],
	["button", "justifyright", "Allign Right", "images/editor_alignright.gif"],
	["devider"],
	["button", "indent", "Indent", "images/editor_indent.gif"],
	["button", "outdent", "Outdent", "images/editor_outdent.gif"],
	["button", "insertorderedlist", "Insert Ordered List", "images/editor_orderedlist.gif"],
	["button", "insertunorderedlist", "Insert Unordered List", "images/editor_unorderedlist.gif"],
	["devider"],
	["button", "removeformat", "Unformat", "images/editor_removeformat.gif"]
	];
	var editor = this;


	toolbar = new isisToolBar(buttons, function(e, cmdID, UI, param) {
		if(!e.srcElement){
			e.srcElement=e.target;
		}
		editor.focus();
		cmdID = cmdID.toLowerCase();
		switch(cmdID){
			case "bold":
			case "underline":
			case "italic":
			case "justifyleft":
			case "justifycenter":
			case "justifyright":
			case "cut":
			case "copy":
			case "paste":
			case "inserthorizontalrule":
			case "indent":
			case "outdent":
			case "removeformat":
			case "insertorderedlist":
			case "insertunorderedlist":
			case "unlink":
			if(this.buttonList[cmdID].enabled){
				if(editor.editorFrame.contentWindow.document.queryCommandEnabled(cmdID)){
					editor.editorFrame.contentWindow.document.execCommand(cmdID, UI, param);
				}
			}
			break;
			case "togglelock":
				editor.toggleLock();
			break;
		}
	}, this.root);
	var toolTable = document.createElement("table");
	toolTable.width = "100%";
	toolTable.height = "26px";
	toolTable.border = "0px";
	toolTable.cellSpacing = "0px";
	toolTable.cellPadding = "0px";
	this.holder.appendChild(toolTable);
	toolTable.appendChild(document.createElement("tbody"));
	this.toolTableRow = toolTable.insertRow(-1);
	this.toolTableRow.style.height="26px";
	td=this.toolTableRow.insertCell(-1);
	td.appendChild(toolbar.getBar());
	this.createDoc();
	editor.initDoc();
};
isisHTMLEditor.prototype.focus=function(){
	this.editorFrame.contentWindow.focus();
};
isisHTMLEditor.prototype.getEditor=function(){
	return this.editorDiv;
};
isisHTMLEditor.prototype.toggleLock=function(){
	if(this.editorFrame.contentWindow.document.designMode == "on"){
		this.editorFrame.contentWindow.document.designMode = "off";
	}else{
		this.editorFrame.contentWindow.document.designMode = "on";
	}
};
isisHTMLEditor.prototype.lock=function(){
	this.editorFrame.contentWindow.document.designMode = "off";
};
isisHTMLEditor.prototype.unlock=function(){
	this.editorFrame.contentWindow.document.designMode = "on";
};
isisHTMLEditor.prototype.createDoc = function(){
	this.editorFrame = document.createElement("iframe");
	this.holder.appendChild(this.editorFrame);
	if(this.holder.style.width!=''){
		this.editorFrame.style.width=parseInt(this.holder.style.width)+'px';
	}else if(this.holder.offsetWidth>0){
		this.editorFrame.style.width=this.holder.offsetWidth+'px';
	}
	if(this.holder.style.height!=''){
		this.editorFrame.style.height=(parseInt(this.holder.style.height)-26)+'px';
	}else if(this.holder.offsetHeight>0){
		this.editorFrame.style.height=(this.holder.offsetHeight-26)+'px';
	}
	
	this.editorFrame.id='R'+Math.floor(Math.random()*99999);
};
isisHTMLEditor.prototype.initDoc = function(){
	this.editorFrame.contentWindow.document.open();
	var html = "<html>\n";
	html += "<head>\n";
	var links = document.getElementsByTagName('LINK');
	var l=links.length;
	for (var i=0; i < l; i++) {
		if (links[i].href) {
			html += "<link rel=\"stylesheet\" type=\"text/css\" href=\""+links[i].href+"\">\n";
		}
	}
	html += "<style>" + " html,body { border: 0px; }</style>\n";
	html += "</head>\n";
	html += "<body>\n";
	html += "</body>\n";
	html += "</html>";
	this.editorFrame.contentWindow.document.write(html);
	this.editorFrame.contentWindow.document.close();
};