var TextOverlay =
{
	_private_functions:
	{
		_DIRECTION_INPUT: 1,
		_DIRECTION_TEXTAREA: 2,

		_createCallbacks: function()
		{
			var b = this;

			var open = function () { b._synctext(TextOverlay._private_functions._DIRECTION_TEXTAREA); return b._displayOverlay(); };
			var close = function () 
			{ 
				b._synctext(TextOverlay._private_functions._DIRECTION_INPUT); b._resetTimer(); return b._closeOverlay(); 
			};
			var update_textarea = function () { return b._synctext(TextOverlay._private_functions._DIRECTION_TEXTAREA); };
			var update_input = function () { return b._synctext(TextOverlay._private_functions._DIRECTION_INPUT); };
			var reset_timers = function () { return b._setTimer();};

			jslib_Core.public_utility_functions.addEventListener(this._elem, "keypress", open, false);
			jslib_Core.public_utility_functions.addEventListener(this._elem, "focus", open, false);

			jslib_Core.public_utility_functions.addEventListener(this._elem, "keypress", update_textarea, false);

			jslib_Core.public_utility_functions.addEventListener(this._textarea, "keypress", update_input, false);
			jslib_Core.public_utility_functions.addEventListener(this._textarea, "keypress", reset_timers, false);
			jslib_Core.public_utility_functions.addEventListener(this._textarea, "blur", close, false);
		},

		_synctext: function(dir)
		{
			if ( dir == TextOverlay._private_functions._DIRECTION_INPUT )
			{
				var value = this._textarea.value;
				var count = 0;
	

				while (value.indexOf("\n") && count < 1000)
				{
					if (this._p_nl)
						value = value.replace("\n", "\u00b7");
					else
						value = value.replace("\n", " ");

					count++;
				}

				this._elem.value = value;
			}
			else if (dir == TextOverlay._private_functions._DIRECTION_TEXTAREA )
			{
				var value = this._elem.value;
				var count = 0;

				while (value.indexOf("\u00b7") && count < 1000)
				{
					if (this._p_nl)
						value = value.replace("\u00b7", "\n");
					else
						value = value.replace("\u00b7", "");

					count++;
				}

				this._textarea.value = value;
			}

			return;
		},

		_displayOverlay: function()
		{
			this._overlay.style.display = "";
			var width = parseInt(jslib_Core.public_utility_functions.getComputedWidth(this._textarea));
			var height = parseInt(jslib_Core.public_utility_functions.getComputedHeight(this._textarea));

			var elem_width = parseInt(jslib_Core.public_utility_functions.getComputedWidth(this._elem));
			var elem_height = parseInt(jslib_Core.public_utility_functions.getComputedHeight(this._elem));

			var pos = jslib_Core.public_utility_functions.findPos(this._elem);

			pos[0] += ( elem_width - width ) / 2;
			pos[1] += ( elem_height - height ) / 2;

			this._overlay.style.left = pos[0] + "px";
			this._overlay.style.top = pos[1] + "px";
			this._overlay.style.position = "absolute";

			this._textarea.focus();

			this._setTimer();

		},

		_resetTimer: function()
		{
			if (this._timer_id == -1)
				return;

			window.clearTimeout(this._timer_id);

			this._timer_id = -1;
			return;
		},

		_setTimer: function()
		{
			var b = this;

			if (this._timer_id != -1)
				this._resetTimer();

			if (this._timer_id != -1)
				return;

			var f = function () { b._timer_id = -1; b._closeOverlay(); };

			this._timer_id = window.setTimeout(f, this._timeout_time);

			return;
		},

		_closeOverlay: function()
		{
			this._overlay.style.display = "none";
			this._resetTimer();
		},

		_init: function()
		{
			this._overlay = document.createElement("DIV");

			jslib_Core.public_utility_functions.setClass(this._overlay, "overlay-base");
			this._overlay.style.display = "none";

			this._textarea = document.createElement("TEXTAREA");
			jslib_Core.public_utility_functions.setClass(this._textarea, this._class);
			this._textarea.setAttribute("wrap", "soft");

			this._overlay.appendChild(this._textarea);

			if (document.body != undefined)
				document.body.appendChild(this._overlay);

			this._createCallbacks();
		}
	},

	inst: function(elem, class_name, timeout_time, preserve_newlines)
	{
		this._elem = elem;
		this._timeout_time = timeout_time;
		this._p_nl = preserve_newlines;


		if (typeof(this._elem) == "string")
			this._elem = document.getElementById(this._elem);

		if (this._elem == null)
			return null;

		this._class = class_name;

		this._init = TextOverlay._private_functions._init;
		this._createCallbacks = TextOverlay._private_functions._createCallbacks;
		this._synctext = TextOverlay._private_functions._synctext;
		this._displayOverlay = TextOverlay._private_functions._displayOverlay;
		this._closeOverlay = TextOverlay._private_functions._closeOverlay;
		this._setTimer = TextOverlay._private_functions._setTimer;
		this._resetTimer = TextOverlay._private_functions._resetTimer;

		this._init();
	}
};


