/*
EchoBox - A fairly simple message center
Copyright (c) 2008 Thomas Peri, http://www.tumuski.com/

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The Software shall be used for Good, not Evil.

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/**
 * EchoBox
 * version 2008-06-28
 *
 * @param string id  The id of the element to write to.
 * @param number scrollback  The number of scrollback lines to keep.
 */
function EchoBox(id, scrollback)
{
	var box, lines, self = this; 

	function poll()
	{
		var first, temp;
		
		if (typeof id != 'string')
		{
			box = id; // assume it's the element itself
		}
		else if ((temp = document.getElementById(id)))
		{
			// found it!  transfer all the text from old to new
			// then let the new replace the old
			while((first = box.firstChild))
			{
				box.removeChild(first);
				temp.appendChild(first);
			}
			box = temp;
		}
		else
		{
			setTimeout(poll, 100);
		}
	}
	
	function out(message)
	{
		if (message)
		{
			box.appendChild(document.createTextNode(message));
			box.scrollTop = box.scrollHeight;
			return true;
		}
	}
	
	function chop()
	{
		var first;
		while (lines > scrollback)
		{
			lines--;
			do {
				first = box.firstChild;
				box.removeChild(first);
			} while (-1 == first.data.indexOf('\r\n'));
		}
	}
	
	function newline(message)
	{
		if (out(message+'\r\n'))
		{
			box.scrollLeft = 0;
		}
		lines++;
		
		if (scrollback > 0)
		{
			chop();
		}
	}
	
	/**
	 * Write a message to this EchoBox.
	 *
	 * @param string message  The message to write.
	 */
	this.write = function(message)
	{
		var i = 0, match;
		message = String(message);
		while (i < message.length)
		{
			// get the next line, including a possible linebreak, trapped
			match = message.substring(i).match(/([^\r\n]*)(\r\n|\r|\n)*/);
			
			if ((match.length > 2) && (match[2]))
			{
				newline(match[1]);
			}
			else
			{
				out(match[1]);
			}

			// update the index before looping
			i += match[0].length;
		}
	};
	
	this.writeln = function(message)
	{
		self.write(message+'\r\n');
	}

	if (!scrollback) {
		scrollback = 100;
	}
	
	box = document.createElement('div');
	lines = 0;
		
	poll();
	
}
