/*
 * Copyright 2006 Mat Gessel <mat.gessel@gmail.com>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */


/**
 * Debugging utilities
 */

/**
 * Array
 */
Array.prototype.toString = function()
{
	return "Array[" + this.length + "]";
}

/**
 * Debug
 */
Debug.s_enabled = false;
Debug.s_window = null;
Debug.s_pendingWrite = "";

if (Debug.s_enabled)
{
	window.onerror = function(message, url, line)
	{
		Debug.writeln("Error ("  + url + ":" + line + ") \n" + message);
	};
}

function Debug() {}

Debug.write = function(string)
{
	if (Debug.s_enabled)
	{
		Debug.s_pendingWrite += string;
		if (! DebugWindow.s_initializing)
		{
			DebugWindow.getInstance().write(string);
			Debug.s_pendingWrite = "";
		}
	}
};

Debug.writeln = function(string)
{
	if (Debug.s_enabled)
	{
		if (string != null)
		{
			Debug.write(string);
		}
		Debug.write("\n");
	}
};

Debug.prettyPrint = function(object)
{
	if (Debug.s_enabled)
	{
		Debug.writeln(object);
		if (object.constructor == Array)
		{
			for (var i = 0; i < object.length; i++)
			{
				Debug.writeln("  [" + i + "]" + object[i]);
			}
		}
		else
		{
			for (member in object)
			{
				Debug.writeln("  +" + member + "=" + object[member]);
			}
		}
	}
};

/**
 * DebugWindow
 */
DebugWindow.s_instance = null;
DebugWindow.s_initializing = false;

function DebugWindow()
{
	DebugWindow.s_initializing = true;
	var WIDTH = 640;
	var HEIGHT = 480;
	var url = "";
	var name = "debug";
	var features = "width=" + WIDTH + ", height=" + HEIGHT + ",menubar=no,status=no,toolbar=no,resizable=yes,scrollbars=yes";
	var replace = false;
	this.m_document = null;
	this.m_window = window.open(url, name, features, replace);
	if (this.m_window != null)
	{ // popup not blocked
		var x = screen.width / 2 - WIDTH / 2;
		var y = screen.height / 2 - HEIGHT / 2;
		this.m_window.moveTo(x, y);
		this.newDocument();
	}
	DebugWindow.s_initializing = false;
}

DebugWindow.getInstance = function()
{
	if (DebugWindow.s_instance == null || DebugWindow.s_instance.isClosed())
	{
		DebugWindow.s_instance = new DebugWindow();
	}
	return DebugWindow.s_instance;
};

DebugWindow.prototype.newDocument = function()
{
	var header = "<HEAD>\n\
		<TITLE>Debugging Window</TITLE>\n\
		</HEAD>\n\
		<BODY>\n\
		<H4>Debug Output</H4>\n\
		<DIV style='float:right;position:fixed;top:10;right:10;'>\n\
		<INPUT type='button' value='Close Window' onclick='window.close()'/><BR>\n\
		<INPUT type='button' value='Close Document' onclick='document.close()'/>\n\
		</DIV>\n\
		<P><PRE>\n"
	this.m_document = this.m_window.document;
	this.m_document.open("text/html");
	this.m_document.write(header);
};

DebugWindow.close = function()
{
	this.m_window.close();
	this.m_window = null;
	this.m_document = null;
}

DebugWindow.prototype.isClosed = function(string)
{
	return (this.m_window != null) ? this.m_window.closed : true;
};

DebugWindow.prototype.write = function(string)
{
	if (this.m_window != null)
	{
		this.m_document.write(string);
		this.m_window.scrollBy(0, 25);
	}
};

DebugWindow.prototype.writeln = function(string)
{
	if (this.m_window != null)
	{
		this.m_document.writeln((string != null) ? string : "");
		this.m_window.scrollBy(0, 25);
	}
};

