/**
	Copyright © 2005 Jeff Watkins <http://metrocat.org/>

	Except where otherwise noted, this software is licensed under the Creative
	Commons Attribution License. To view a copy of this license, visit
	http://creativecommons.org/licenses/by/2.5/ or send a letter to
	Creative Commons, 543 Howard Street, 5th Floor, San Francisco,
	California, 94105, USA.

	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 NON-
	INFRINGEMENT. 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.
 **/

function print( str )
{
	var traceElement= document.getElementById( "trace" );
	if (!traceElement)
		return;

	node= document.createElement( "pre" );
	node.style.margin= "0";
	if (str)
		node.appendChild( document.createTextNode( str ) );
	traceElement.appendChild( node );
	traceElement.scrollTop= traceElement.scrollHeight-traceElement.offsetHeight+5;
}

var debuggerEnabled= false;
var debuggerVisible= false;

function performCommand()
{
	var commandElement= document.getElementById( "command" );
	var command= commandElement.value;

	if (!command)
		return false;

	function print( e, html )
	{
		var traceElement= document.getElementById( "trace" );
		node= document.createElement( e );
		node.style.margin= "0";
		if (html)
			node.innerHTML= html;
		traceElement.appendChild( node );
	}

	print( "pre", command );
	try
	{
		var result= eval(command);
		print( "div", "<b>Result:</b>" );
		print( "pre", toLiteral(result) );
		commandElement.value="";
	}
	catch (e)
	{
		print( "div", "<b>Exception:</b> " + e );
	}
	print( "hr" );

	var traceElement= document.getElementById( "trace" );
	traceElement.scrollTop= traceElement.scrollHeight-traceElement.offsetHeight+5;

	return false;
}

function debuggerTabWasClicked()
{
	var debuggerWindow= document.getElementById( "debugger" );

	var commandField= document.getElementById( "command" );
	if (!commandField)
		return;

	commandField.focus();

	var scrollHeight= debuggerWindow.scrollHeight;
	var offsetHeight= debuggerWindow.offsetHeight;

	if (!debuggerWindow.originalHeight)
	{
		debuggerWindow.originalHeight= offsetHeight;
		debuggerWindow.style.height= "auto";
	}
	else if (offsetHeight==debuggerWindow.originalHeight)
	{
		debuggerWindow.style.height= "auto";
	}
	else
		debuggerWindow.style.height= debuggerWindow.originalHeight + "px";
}

function buildDebugger()
{
	var text;

	var debuggerWindow= document.createElement( "div" );
	debuggerWindow.setAttribute( "id", "debugger" );
	debuggerWindow.setAttribute( "style", 'display:none; position: fixed; z-index: 2000; left: 0; bottom: 0; width: 100%; height: 12px; font-family: "lucida grande", verdana, sans serif; font-size: 10px;' );

	var tab= document.createElement( "div" );
	tab.setAttribute( "id", "tab" );
	tab.setAttribute( "style", 'margin-left: 10px; position: relative; top: -2px;' );

	var tabContent= document.createElement( "span" );
	tab.appendChild( tabContent );

	tabContent.setAttribute( "id", "tabContent" );
	tabContent.setAttribute( "style", 'padding-left: 10px; padding-right: 10px; padding-top: 2px; padding-bottom: 4px; background-color: #DDD; cursor: pointer; border-top: 2px solid #AAA; border-left: 2px solid #AAA; border-right: 2px solid #AAA;' );
	tabContent.onclick= debuggerTabWasClicked;
	tabContent.appendChild( document.createTextNode( "Debugger" ) );

	debuggerWindow.appendChild( tab );

	var debuggerContent= document.createElement( "div" );
	debuggerContent.setAttribute( "id", "debuggerContent" );
	debuggerContent.setAttribute( "style", 'padding: 5px; background-color: #DDD; border-top: 2px solid #AAA;' );

	debuggerWindow.appendChild( debuggerContent );

	var commandForm= document.createElement( "form" );
	commandForm.setAttribute( "id", "commandForm" );
	commandForm.onsubmit= performCommand;
	debuggerContent.appendChild( commandForm );

	var command= document.createElement( "textarea" );
	command.setAttribute( "rows", "5" );
	command.setAttribute( "id", "command" );
	command.setAttribute( "style", 'display: block; width: 100%; padding: 2px;' );
	commandForm.appendChild( command );

	var debuggerButtons= document.createElement( "div" );
	debuggerButtons.setAttribute( "id", "debuggerButtons" );
	debuggerButtons.setAttribute( "style", 'text-align: right;' );
	commandForm.appendChild( debuggerButtons );

	var execute= document.createElement( "input" );
	execute.setAttribute( "type", "submit" );
	execute.setAttribute( "value", "Execute" );
	debuggerButtons.appendChild( execute );

	var trace= document.createElement( "div" );
	trace.setAttribute( "id", "trace" );
	trace.setAttribute( "style", 'border: 2px solid #CCC; background-color: #EEE; padding: 2px; height: auto; max-height: 200px; overflow: auto; font-family: monaco, "lucida sans typewriter", "courier new"; font-size: 11px;' );

	debuggerContent.appendChild( trace );

	document.body.appendChild( debuggerWindow );
	if (debuggerEnabled)
		debuggerWindow.style.display="block";
	if (debuggerVisible)
		debuggerTabWasClicked();
}

function enableDebugger( visible )
{
	var debuggerWindow= document.getElementById( "debugger" );
	if (debuggerWindow)
		debuggerWindow.style.display= "block";

	debuggerEnabled= true;
	debuggerVisible= visible;
}

if (window.addEventListener)
	window.addEventListener( "load", buildDebugger, false );
else if (window.attachEvent)
	window.attachEvent( "onload", buildDebugger );
