/*
* tmtfactory website
* ------------------
* $Id: htmleditor.js,v 1.1 2009-11-04 12:03:12 aruzafa Exp $
*/

/**
* @file
* @brief JavaScript file for the HTMLEditor widget.
*/

try
{
	tinyMCE.init
	({
		mode: "textareas",
		editor_selector : "wysiwyg",

		width: "100%",
		height: "400px",

		convert_newlines_to_brs: false,
		entity_encoding : "raw",

		theme: "advanced",
		theme_advanced_toolbar_location : "top",
		theme_advanced_buttons1: "cut, copy, paste, undo, redo, formatselect, fontselect, fontsizeselect",
		theme_advanced_buttons2: "bold, italic, underline, sup, sub, |, justifyleft, justifycenter, justifyright, justifyfull, |, bullist, numlist, |, link, unlink, forecolor, backcolor",
		theme_advanced_buttons3: "",
		plugins : "inlinepopups",
		dialog_type : "modal",

		save_callback: "dump_html",
	});
}
catch (e)
{
}

dump_html = function (element_id, html, body)
{
	var wysiwyg = $(element_id);
	var plain;

	if (wysiwyg &&
	wysiwyg.htmleditor &&
	wysiwyg.htmleditor.plain)
	{
		html = html.replace(/^\s*$/, "");

		wysiwyg.htmleditor.plain.value = (html == "") ?
		"<html />" :
		"<html><![CDATA[\n\n" + html + "\n\n]]></html>";
	}

	return html;
}
htmleditor_submit = function (e)
{
	e.stopPropagation();
	e.preventDefault();
	return false;
} 

window_load = function (e)
{
	var i, j;
	var htmleditors = $$(".widget.htmleditor");
	var htmleditor;
	var textareas;

	for (i = 0; i < htmleditors.length; i++)
	{
		htmleditor = htmleditors[i];

		textareas = htmleditor.getElementsBySelector(".wysiwyg");
		if (textareas.length > 0)
		{
			textareas[0].htmleditor = htmleditor;
			htmleditor.wysiwyg = textareas[0];
		}

		textareas = htmleditor.getElementsBySelector(".plain");
		if (textareas.length > 0)
		{
			textareas[0].htmleditor = htmleditor;
			htmleditor.plain = textareas[0];
		}

		htmleditors[i].observe("submit", htmleditor_submit);
	}
}

Event.observe(window, "load", window_load); 

