// cube console
// bboss1984@gmail.com 
  
  // after the dom is loaded - execute these functions
	$(document).ready( function()
	{
	  // bind 'myForm' and provide a simple callback function 
        $('#commands').ajaxForm({
			dataType:  'json', 
			success: processJson
        });

	  // focus cursor		
		document.commands.command.focus();	

		scrollcontent();		
	});

	var linebuffer;
	var databuffer;

	function updatebuffer()
	{			
		$('#output').text(databuffer.replace(/\|/g, '\n')); 
		scrollcontent(); 
	}

	function dumpbuffer()
	{	
	  // remove a line from the line buffer and place it in output
		$('#output').append(linebuffer.shift() + '<br>\n');
		scrollcontent();

	  // call dumpbuffer again (after delay) to dump next line
		if (linebuffer.length > 0)
		{
			setTimeout("dumpbuffer()", 5);
		}
	}

	function scrollcontent()
	{
		var position = $('#output').attr('scrollHeight');
		$('#output').attr('scrollTop', position);
	}

  // process call back
	function processJson(data)
	{
	  // clear command box
	  	$('#command').attr('value', '');
		document.commands.command.focus();	

	  // echo command
		$('#output').append('<br>\n&gt; ' + data.command + ' ' + data.param + '<br>\n');

	  // run the returned function
		data.run();
	}
