//======================================================================================
// WSBasicEditor Functions Definition:
//======================================================================================
/*
each editor should support the following methids:

function WSPlainEditor()
{
	this.sEditorId;
	this.setFocus;
	this.getEditorFrame;
	this.getEditorElement;
	this.getEditorContent;
	this.setEditorContent;
	this.getCaretPosition;
	this.setCaretPosition;
}
*/
//======================================================================================
// WSPlainEditor Functions Definition:
//======================================================================================
function WSPlainEditor(sEditorId,sEditorName,sCSSFileName)
{
	this.sEditorId = sEditorId;
	this.sEditorName = (sEditorName? sEditorName : sEditorId);
	this.sCSSFileName = (sCSSFileName ? sCSSFileName : "whitesmoke_editor.css");
}
//--------------------------------------------------------------------------------------
WSPlainEditor.prototype.setFocus = function()
{
	return true;	
}
//--------------------------------------------------------------------------------------
WSPlainEditor.prototype.getEditorFrame = function()
{
	var oEditorFrame = window.frames[this.sEditorName] || window.frames[this.sEditorId];
	if (! oEditorFrame)
	{
		var oEditorInst = window.document.getElementById(this.sEditorId);
		if (oEditorInst)
		{
			if (oEditorInst.contentWindow) // For Mozilla, IE5.5 and IE6
			{
				oEditorFrame = oEditorInst.contentWindow;
			}
			else if (oEditorInst.contentDocument) // For NS6
			{
				oEditorFrame = oEditorInst.contentDocument;
			}
		}
	}
	if (! oEditorFrame)
	{
		alert("error: could not get editor frame!");
	}
	return oEditorFrame;	
}
//--------------------------------------------------------------------------------------
WSPlainEditor.prototype.getEditorElement = function()
{
	var oEditorElement = document.getElementById(this.sEditorId);
	return oEditorElement;
}
//--------------------------------------------------------------------------------------
WSPlainEditor.prototype.getEditorContent = function()
{
	var sHTML = new String();
	var oEditorFrame = this.getEditorFrame();
	if (oEditorFrame)
	{
		sHTML = oEditorFrame.document.body.innerHTML;
		if (!sHTML.length || sHTML.length <= 0)
		{
			//alert("WSPlainEditor.prototype.getEditorContent(2), sHTMl is empty");
		}
	}
	return sHTML;
}
//--------------------------------------------------------------------------------------
WSPlainEditor.prototype.setEditorContent = function(sHTML)
{
	var oEditorFrame = this.getEditorFrame();
	if (oEditorFrame)
	{
		oEditorFrame.document.open();
		oEditorFrame.document.write(sHTML);
		oEditorFrame.document.close();
		this.setEditorCSS();
		return true;
	}
	return false;
}
//-----------------------------------------------------------------------------------------
//Set a CSS file to be used by the editor.
//-----------------------------------------------------------------------------------------
WSPlainEditor.prototype.setEditorCSS = function()
{
   var oEditorFrame = this.getEditorFrame();
   if (oEditorFrame)
   {
      var linkEle = oEditorFrame.document.createElement("LINK");
      linkEle.setAttribute("rel","stylesheet");
      linkEle.setAttribute("type","text/css");
      //linkEle.setAttribute("media","screen");
      linkEle.setAttribute("href",this.sCSSFileName);
      oEditorFrame.document.getElementsByTagName("head")[0].appendChild(linkEle);
      return true;
   }
   return false;
}
//======================================================================================
// WSTinyMCE Functions Definition:
//======================================================================================
function WSTinyMCE(sEditorId,sCSSFileName)
{
	this.sEditorId = (sEditorId &&  sEditorId.indexOf("mce_editor_")!=-1 ? sEditorId : "mce_editor_0");
	this.sEditorName = this.sEditorId; //(sEditorName? sEditorName : sEditorId);
	this.sCSSFileName = (sCSSFileName ? sCSSFileName : "whitesmoke_editor.css");
	
	//this.init(sReplacedElementId,nToolbars);	
}
//--------------------------------------------------------------------------------------
WSTinyMCE.prototype.init = function(sReplacedElementId,nToolbars)
{
	var sTB1= "";
	var sTB2= "";
	var sTB3= "";

	if (nToolbars == 0)
	{
		//sTB1 = "copy,cut,paste";
		//sTB1 = "bold,italic,underline,separator,forecolor,backcolor,separator,replace,separator,copy,cut,paste,separator,print";
	}
   else if (nToolbars == 1)
	{
		//sTB1 = "whitesmokecontrols,separator,bold,italic,underline,separator,forecolor,backcolor,separator,copy,cut,paste";
		sTB1 = "whitesmokecontrols,separator,copy,cut,paste";
		sTB2 = "";
	}
	else //if (nToolbars == 2)
	{
		sTB1 = "formatselect,fontselect,fontsizeselect,separator,bold,italic,underline,strikethrough,separator,charmap,separator,forecolor,backcolor,separator,print";
		sTB2 = "whitesmokecontrols,separator,justifyleft,justifycenter,justifyright,justifyfull,separator,bullist,numlist,outdent,indent,separator,hr,removeformat,separator,link,unlink,replace,separator,copy,cut,paste,pasteword,pastetext";
	}
	
	tinyMCE.init({
		mode : "exact",
		elements: sReplacedElementId,
		theme : "advanced",
		content_css : this.sCSSFileName,
		height: "100%",
		width: "100%",

		auto_reset_designmode : true, //currect a design-bug in mozzila
		//auto_focus : "mce_editor_0",
		//focus_alert : true, 
	
		theme_advanced_layout_manager : "SimpleLayout",
		theme_advanced_toolbar_location : "top",
		//theme_advanced_path_location : "bottom",
		theme_advanced_toolbar_align : "left",
		
		//plugins : "table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,zoom,flash,searchreplace,print,contextmenu,paste,directionality,fullscreen",
		//buttons : "undo,redo,separator,link,unlink,anchor,image,cleanup,help,code,justifyleft,justifycenter,justifyright,justifyfull,separator,bullist,numlist,outdent,indent,separator,hr,removeformat,separator,link,unlink,image,charmap,separator,code,help",
		plugins : "searchreplace,print,paste,whitesmoke_plugin",

		//theme_advanced_buttons1 : !bShowToolbar? "" : "formatselect,fontselect,fontsizeselect,separator,bold,italic,underline,strikethrough,separator,charmap,separator,forecolor,backcolor,code",
		//theme_advanced_buttons2 : !bShowToolbar? "" : "justifyleft,justifycenter,justifyright,justifyfull,separator,bullist,numlist,outdent,indent,separator,hr,removeformat,separator,link,unlink,replace,separator,copy,cut,paste,pasteword,pastetext,separator,print",
		//theme_advanced_buttons3 : "bold,italic,underline,separator,forecolor,backcolor,separator,replace,separator,copy,cut,paste,separator,print",
		theme_advanced_buttons1 : sTB1,
		theme_advanced_buttons2 : sTB2,
		theme_advanced_buttons3 : sTB3,


		custom_undo_redo : false, //disable tinymce own undo/redo information
		event_elements : "a,img,span,div",

		button_tile_map : true, // warnning: doesn't work with some DOCTYPE declarations
		cleanup : false,
		verify_html : false,
		visual : false,
		trim_span_elements : false,
		inline_styles : true,
		apply_source_formatting : true
	});
}
//--------------------------------------------------------------------------------------
WSTinyMCE.prototype.setFocus = function()
{
	//contentWindow.focus();
	//tinyMCE.execInstanceCommand("mce_editor_0", "mceFocus", false, "mce_editor_0");
	tinyMCE.execCommand("mceFocus", false, this.sEditorId);
}
//--------------------------------------------------------------------------------------
WSTinyMCE.prototype.getEditorFrame2 = function()
{
	var oEditorInst = null;
	oEditorInst = tinyMCE.getInstanceById(this.sEditorId);
	if (oEditorInst)
	{
		var oEditorFrame = oEditorInst.getWin();
		return oEditorFrame;
	}
	else
	{
		return getEditorFrame();
	}
}
//--------------------------------------------------------------------------------------
WSTinyMCE.prototype.getEditorFrame = function()
{
	var oEditorFrame = window.frames[this.sEditorName] || window.frames[this.sEditorId];
	if (! oEditorFrame)
	{
		var oEditorInst = window.document.getElementById(this.sEditorId);
		if (oEditorInst)
		{
			if (oEditorInst.contentWindow) // For Mozilla, IE5.5 and IE6
			{
				oEditorFrame = oEditorInst.contentWindow;
			}
			else if (oEditorInst.contentDocument) // For NS6
			{
				oEditorFrame = oEditorInst.contentDocument;
			}
		}
	}
	if (! oEditorFrame)
	{
		alert("error: could not get editor frame!");
	}
	return oEditorFrame;	
}
//--------------------------------------------------------------------------------------
WSTinyMCE.prototype.getEditorElement = function()
{
	var oEditorElement = document.getElementById(this.sEditorId);
	return oEditorElement;
}
//--------------------------------------------------------------------------------------
WSTinyMCE.prototype.getEditorContent = function()
{
	//var sHTML = tinyMCE.getInstanceById(this.sEditorId).getBody().innerHTML;
	tinyMCE.execCommand("mceFocus", false, this.sEditorId);
	var sHTML = tinyMCE.getContent();
	return sHTML;
}
//--------------------------------------------------------------------------------------
WSTinyMCE.prototype.setEditorContent = function(sHTML)
{
	tinyMCE.execCommand("mceFocus", false, this.sEditorId);
	tinyMCE.setContent(sHTML);
	//this.setEditorCSS();
	return true;
}
//--------------------------------------------------------------------------------------
WSTinyMCE.prototype.getCaretPosition = function()
{
	return 0;
}
//-----------------------------------------------------------------------------------------
//Set a CSS file to be used by the editor.
//-----------------------------------------------------------------------------------------
WSTinyMCE.prototype.setEditorCSS = function()
{
	var oEditorFrame = this.getEditorFrame();
	if (oEditorFrame)
	{
		var linkEle = oEditorFrame.document.createElement("LINK");
		linkEle.setAttribute("rel","stylesheet");
		linkEle.setAttribute("type","text/css");
		linkEle.setAttribute("media","screen");
		linkEle.setAttribute("href",this.sCSSFileName);
		var oHeadElements = null;
		for (var i=0; i<5; ++i)
		{
			var oHeadElements = oEditorFrame.document.getElementsByTagName("head");		
			if (oHeadElements && oHeadElements.length>0)
			{
				oHeadElements[0].appendChild(linkEle);
				break;
			}
		}
		return true;
	}
	return false;
}
//-----------------------------------------------------------------------------------------

