/*

Copyright (c) ScanFrame 2008.
All Rights Reserved.

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

Identification:
  LIB

Initial Author:
  AVO 20080826

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

*/


// Sentry
if (!AddaptTinyMCE) {

function AddaptTinyMCE_InitCallback(inst)
{
	//AddaptTinyMCE.AddElemEvents(inst);
}

var AddaptTinyMCE =
{
	// Flag to prevewnt double initialization.
	Initialized: false,
	//
	TimeoutCounter: 10,
	// Set some default colors.
	ColorFocus: 'red',
	ColorNormal: 'green',
	//
	ResizeEditorBox: function (inst)
	{
		alert("Editor: " + inst.editorId + " is now initialized.");
	},
	//
	InitDelayed: function ()
	{
		window.setTimeout((function(){AddaptTinyMCE.Init()}), 300);
	},
	//
	Init: function ()
	{
		if (this.Initialized)	return;
		var elems = document.getElementsByTagName('TEXTAREA');
		for (var i = 0; i < elems.length; i++)
		{
			if (elems.item(i).className == 'text_html')
			{
				// Set the flag that it is initialized.
				//this.Initialized = true;
				//
				if (typeof tinyMCE != 'object')
					DebugOut("tinyMCE object could not be found! (not loaded)!" + (typeof tinyMCE), true);
				//
				tinyMCE.init
				({

					init_instance_callback: AddaptTinyMCE_InitCallback,
					mode : "specific_textareas",
					editor_selector : "text_html",
					skin : "o2k7",
					theme : "advanced",
					tab_focus : ':prev,:next',
					/*
					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,|,forecolor,backcolor,|" +
						",justifyleft,justifycenter,justifyright,justifyfull",
					theme_advanced_buttons2 : ",formatselect,copy,paste,pastetext,|,cleanup,code,|,fullscreen",
					theme_advanced_buttons3 : "",
					theme_advanced_buttons4 : ""
				});
				// Make the events be called.
				setTimeout('AddaptTinyMCE.AddEvents()', 0);
				// Only do this once.
				break;
			}
		}
		// Clear the class name to prevent reentry.
		for (var i = 0; i < elems.length; i++)
			if (elems.item(i).className == 'text_html')
				elems.item(i).className = null;
	},
	// Function to initialize and add events,
	AddElemEvents: function(elem)
	{
		var pelem = document.getElementById(elem.id + '_tbl');
		pelem.style.borderStyle = GetStyleProperty(elem, 'border-top-style');
		pelem.style.borderWidth = GetStyleProperty(elem, 'border-top-width');
		this.ColorNormal = GetStyleProperty(elem, 'border-top-color');
		elem.className = 'text_html_focus';
		this.ColorFocus = GetStyleProperty(elem, 'border-top-color');
		pelem.style.borderColor = this.ColorNormal;
		var felem = document.getElementById(elem.id + '_ifr');
		felem.contentWindow.name = elem.id + '_tbl';
		if (BrowserDetect.browser == 'Explorer')
		{
			felem.parent_table = pelem;
			felem.onfocus = (function()
			{
				this.parent_table.style.borderColor = AddaptTinyMCE.ColorFocus;
			});
			felem.onblur = function()
			{
				this.parent_table.style.borderColor = AddaptTinyMCE.ColorNormal;
			}
		}
		else
		{
			AddEvent(felem.contentWindow, 'focus', (function (event)
			{
				//DebugOut('Intercepted focus: ' +  event.target);
				document.getElementById(this.name).style.borderColor = AddaptTinyMCE.ColorFocus;
			}));
			AddEvent(felem.contentWindow, 'blur', (function ()
			{
				//DebugOut('Intercepted blur: ' + this.name);
				document.getElementById(this.name).style.borderColor = AddaptTinyMCE.ColorNormal;
			}));
		}
	},
	// Function to initialize and add events,
	AddEvents: function()
	{
		var elems = document.body.getElementsByTagName('TEXTAREA');
		for (var i = 0; i < elems.length; i++)
		{
			var elem = elems.item(i);
			// Deactivate the tab list item.
			if (elem.className == 'text_html')
			{
				//ToggleDisplay(elem);
				// Get some colors
				var pelem = document.getElementById(elem.id + '_tbl');
				if (!pelem)
				{
					if (this.TimeoutCounter-- > 0)
						setTimeout('AddaptTinyMCE.AddEvents()', 300);
					else
						DebugOut("Cound not find Element: " + elem.id + '_tbl', true);
					return;
				}
				this.AddElemEvents(elem);
			}
		}
	},
	// Loads the neded script(s).
	Initialize: function ()
	{
		var url = 'jscripts/tiny_mce/tiny_mce.js';
		// Event en proper javascript reading only works in Firefox.
		if (BrowserDetect.browser == 'Explorer')
		{
		  document.write('<script type="text/javascript" src="' + url + '"></script>');
			setTimeout('AddaptTinyMCE.Init()', 0);
		}
		else
		{
			javascript_import(url);
			if (BrowserDetect.browser == 'Firefox')
				AddEvent(window, 'load', (function(){AddaptTinyMCE.Init()}));
			else
				AddEvent(window, 'load', (function(){AddaptTinyMCE.InitDelayed()}));
		}
	}
}
// Initialize a chain of events.
AddaptTinyMCE.Initialize();

// Sentry End
}
