/*

Copyright (c) ScanFrame 2008.
All Rights Reserved.

$Source: /export/cvs/primo/com/jscripts/htmledit.js,v $
$Author: arjan $
$Revision: 1.11 $

Identification:
  LIB

Initial Author:
  AVO 20080826

Purpose:
	Looks for <textarea> objects to convert them into tinyMCE editors.

*/

// Sentry start to prevent multiple loading.
if (!AddaptTinyMCE)
{
// -----------------------------------------------------------------------------
/**
* Registers the Tiny MCE object when needed.
* @var objecy
*/
var AddaptTinyMCE =
{
	// Prevents rentry of loading the script.
	IsLoading: false,
	// Set some default colors.
	ColorFocus: 'red',
	ColorNormal: 'green',
	IdCounter: 0,
	/**
	* Elements that need to be initialized.
	* @returns Array
	*/
	GetElements: function ()
	{
		retval = Array();
		var elems = document.getElementsByTagName('TEXTAREA');
		for (var i = 0; i < elems.length; i++)
		{
			if (elems.item(i).className.match(/text_html|text_html_focus/))
				retval.push(elems.item(i));
		}
		return retval;
	},
	/**
	* Callback function to add events for focussing.
	* @param inst object
	*/
	InitCallback: function (inst)
	{
		var self = this;
		// Get the initial element.
		var elem_p = document.getElementById(inst.id + '_parent');
		//
		var elem = elem_p.parentNode.childNodes.item(1);
		// Get the element for the border.
		var elem_b = document.getElementById(inst.id + '_tbl');
		//
		if (System && System.Colors)
		{
			elem_b.style.border = '1px solid ' + System.Colors.InputBorder;
			elem_b.style.borderRadius = '3px';
		}
		else
			elem_b.style.border = '1px solid ' + self.ColorNormal;
		//
		var felem = document.getElementById(inst.id + '_ifr');
		SfLib.AddEvent(felem.contentWindow, 'focus', (function (event)
		{
			if (System && System.Colors)
				elem_b.style.borderColor = System.Colors.InputBorderFocus;
			else
				elem_b.style.borderColor = self.ColorFocus;
		}));
		SfLib.AddEvent(felem.contentWindow, 'blur', (function ()
		{
			if (System && System.Colors)
				elem_b.style.borderColor = System.Colors.InputBorder;
			else
				elem_b.style.borderColor = self.ColorNormal;
		}));
	},
	/**
	* Private function and is called by initialize.
	* @returns void
	*/
	Init2: function ()
	{
		this.IsLoading = false;
		tinyMCE.init
		({
			init_instance_callback: function(inst) {self.InitCallback(inst);},
			mode:"none",
			theme:"simple",
			language:"en"
		});
		var elems = this.GetElements();
		for (var i = 0; i < elems.length; i++)
			if (!elems[i].id.length)
			{
				elems[i].id = 'timy_mce_editor_' + this.IdCounter++;
				tinyMCE.execCommand("mceAddControl",true,elems[i].id);
			}
	},
	/**
	* Private function and is called by initialize.
	* @returns void
	*/
	Init: function ()
	{
		this.IsLoading = false;
		var elems = this.GetElements();
		// For the callback.
		var self = this;
		if (elems.length)
		{
			// Make TinyMCE initialize the elements.
			tinyMCE.init
			({
				init_instance_callback: function(inst) {self.InitCallback(inst);},
				mode: "none",
				editor_selector: 'text_html',
				skin: "o2k7",
				theme: "advanced",
				tab_focus: ':prev,:next',
				language: 'en',
				//theme_advanced_resizing : true,
				//theme_advanced_statusbar_location : "bottom",
				theme_advanced_toolbar_location: "top",
				theme_advanced_toolbar_align: "left",
				// Theme options
				plugins: "contextmenu,paste,fullscreen,visualchars,nonbreaking",
				theme_advanced_buttons1: "newdocument,|,removeformat,bold,italic,underline,strikethrough,sub,sup,hr,|,forecolor,backcolor,|" +	",justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist",
				theme_advanced_buttons2: ",formatselect,copy,paste,pastetext,|,cleanup,code,|,fullscreen",
				theme_advanced_buttons3: "",
				theme_advanced_buttons4: ""
			});
		}
		// Clear the class name to prevent reentry.
		for (var i = 0; i < elems.length; i++)
		{
			elems[i].className = '';
			if (!elems[i].id.length)
				elems[i].id = 'timy_mce_editor_' + this.IdCounter++;
			tinyMCE.execCommand("mceAddControl", true, elems[i].id);
		}
	},
	// Loads the needed script(s) and can be called multiple times..
	Initialize: function (not_delayed)
	{
		// When loading bail out.
		if (this.IsLoading)	return;
		//
		var self = this;
		// Check if it is needed to load or activate Tiny MCE.
		if (this.GetElements().length || not_delayed)
		{
			// Check if the script has loaded checking the Tiny MCE object.
			if (typeof tinyMCE != 'object')
			{
				this.IsLoading = true;
				// Load the script and call the initialization function.
				SfLib.JavascriptImport('jscripts/tiny_mce/tiny_mce.js', 'tiny_mce',
				// Call the Init() function delayed.
					function(){window.setTimeout((function(){self.Init();}), 300);});
			}
			// When loaded the Init() function can be called immediately.
			else
				this.Init();
		}
	}
};
// Initialize a chain of events.
switch (BrowserDetect.browser)
{
	default:
		AddaptTinyMCE.Initialize(true);
		break;

	case 'Explorer':
		SfLib.AddEvent(window, 'load', function(){AddaptTinyMCE.Initialize();});
		break;
}
//-----------------------------------------------------------------------------
// Sentry End
}

