// =======================================================
//  CLASS Debug
// =======================================================

var g_oDebug = new Debug();
var undefined;

// Constructor
// ===========
function Debug()
{
	// Constants
	this.DEBUG_JS = "js";
	this.DEBUG_LMS_ERROR = "lmserror";
	this.DEBUG_FLASH = "flash";

	this.DUMP_HIERARCHY = 1;
	this.DUMP_PATH = 2;

	// Properties
	this.m_oDebugWindow = undefined;
	this.debugMode = false;
	this.lmsDebug = false;
	this.css = "assets/css/debug.css";

	// Determine debug state
	// Get all settings from URL parameters
	var sSettings = document.location.search.substr(1);
	var aAllSettings = (sSettings.length > 0) ? unescape(sSettings).split("&") : new Array();
	var nSettingsCount = aAllSettings.length;

	var aSetting;
	var sName;
	var sValue;
	for (var i = 0; i < nSettingsCount; i ++)
	{
		aSetting = aAllSettings[i].split ("=");
		sName = aSetting[0].toLowerCase();
		sValue = aSetting[1].toLowerCase();
		if (sName == "debugmode" && sValue == "true")
		{
			this.debugMode = true;
		}
		else if (sName == "lmsdebug" && sValue == "true")
		{
			this.lmsDebug = true;
		}
	}
}

// showMessage()
// =============
Debug.prototype.showMessage = function(p_sMessage, p_sType)
{
	if (this.debugMode)
	{
		if (p_sType == undefined) p_sType = this.DEBUG_JS;
		// Test for null throughout as Netscape blocks popups as document is loading.
		if (this.m_oDebugWindow == undefined || this.m_oDebugWindow == null || this.m_oDebugWindow.closed)
		{
			this.m_oDebugWindow = window.open("", "debug", "width=800,height=320,scrollbars=yes,resizable=yes");

			if (this.m_oDebugWindow != null)
			{
				try
				{
					this.m_oDebugWindow.document.writeln("<html><head>");
				}
				catch(e)
				{
					if (e.message.toLowerCase().indexOf("access is denied") != -1)
					{
						var sUnique = Math.round(Math.random() * 1000).toString();
						this.m_oDebugWindow = window.open("", "debug" + sUnique, "width=800,height=320,scrollbars=yes,resizable=yes");
						this.m_oDebugWindow.document.writeln("<html><head>");
					}
				}
				finally
				{
					this.m_oDebugWindow.document.writeln("<link rel='stylesheet' type='text/css' href='" + this.css + "'></link>");
					this.m_oDebugWindow.document.writeln("<title>Cylix Debug Window</title>");
					this.m_oDebugWindow.document.writeln("</head><body>");
				}
			}
		}

		if (this.m_oDebugWindow != null)
		{
			this.m_oDebugWindow.document.writeln ("<p class='" + p_sType.toLowerCase() + "'>" + p_sType + ": " + p_sMessage + "</p>");
			this.m_oDebugWindow.scrollBy(0, 500);
		}
	}
};

// dumpObject()
// ============
Debug.prototype.dumpObject = function(p_oObject, p_nDisplay, p_nLevel, p_sPath)
{
	// Defaults
	if (p_nLevel == undefined) p_nLevel = 0;
	if (p_sPath == undefined) p_sPath = "";
	if (p_nDisplay == undefined) p_nDisplay = this.DUMP_PATH;

	var i;
	var sIndent = "";
	var sType;
	var oProperty;

	// Create indent string
	for (i=0; i<p_nLevel; i++)
	{
		sIndent += "&nbsp;&nbsp;&nbsp;&nbsp;";
	}

	// List properties recursively
	var sPath;
	for (var sPropertyName in p_oObject)
	{
		oProperty = p_oObject[sPropertyName];
		sType = typeof oProperty;

		if (sType == "object")
		{
			if (p_nDisplay == this.DUMP_HIERARCHY)
			{
				this.showMessage(sIndent + "<b>" + sPropertyName + ":</b>");
			}
			else
			{
				if (sPropertyName.substring(0) >= "0" && sPropertyName.substring(0) <= "9")
				{
					sPath = p_sPath + "[" + sPropertyName + "]";
				}
				else
				{
					sPath = p_sPath + "." + sPropertyName;
				}
			}
			this.dumpObject(oProperty, p_nDisplay, p_nLevel+1, sPath);
		}
		else if (sType != "function")
		{
			if (p_nDisplay == this.DUMP_HIERARCHY)
			{
				this.showMessage(sIndent + "<b>" + sPropertyName + ": </b>" + p_oObject[sPropertyName]);
			}
			else
			{
				this.showMessage("<b>" + p_sPath + "." + sPropertyName + ": </b>" + p_oObject[sPropertyName]);
			}
		}
	}
}