
// Declaration
function __Debug()
{
// public:

/*
	this.assert = Debug_assert;
	this.print = Debug_print;
	this.stackTrace = Debug_stackTrace;
*/

	this.assert = Debug_dummy;
	this.print = Debug_dummy;
	this.stackTrace = Debug_dummy;

	this.k_nPriority_noUse = 0;
	this.k_nPriority_low = 1;
	this.k_nPriority_normal = 2;
	this.k_nPriority_high = 3;
	this.k_nPriority_critical = 4;


// private:
	this.init = Debug_init;
	this.getFuncName = Debug_getFuncName;

//	this.wndDebug = window.open( "/S1/Common/DebugWindow.html", "wndDebug", "toolbar=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=600, height=400" );

	var date = new Date();
	this.nTickCount_init = ( +date );
	this.nTickCount_lastPrinted = this.nTickCount_init;

	this.strBuffer = "";
	this.isBufferEmpty = true;
	this.isRecursiveDetect = false;
}


// Implementaion
function Debug_dummy()
{
}

function Debug_assert( /*bool*/ expression )
{
	if ( expression == false )
	{
		this.print( "assertion failed!!!", this.getFuncName( this.assert.caller ), this.k_nPriority_critical );
		//alert( "Assertion Failed. Please See Debug Window." );
	}
}

function Debug_print( /*string*/ strMessage, /*string*/ strCallerName, /*int*/ nPriority )
{
	if ( this.isRecursiveDetect )
		return;

	// Recursive Call ÀÎÁö È®ÀÎ
	// 10 Step ±îÁö¸¸ È®ÀÎ
	var fnCallStackPointer = this.print.caller;
	var nCheckCount = 0;
	while ( nCheckCount < 10 && fnCallStackPointer )
	{
		if ( fnCallStackPointer == Debug_print )
		{
			this.isRecursiveDetect = true;
			alert( "Recursive Call Detect!!!" );
			this.stackTrace();
			return;
		}
		nCheckCount++;
		fnCallStackPointer = fnCallStackPointer.caller;
	}

	if ( !strMessage )
		strMessage = "";
	if ( !strCallerName )
		strCallerName = this.getFuncName( this.print.caller );
	if ( !nPriority )
		nPriority = this.k_nPriority_normal;

	var date = new Date();
	var nTickCount_fromLastPrinted = ( +date ) - this.nTickCount_lastPrinted;
	var nTickCount_fromInit = ( +date ) - this.nTickCount_init;
	this.nTickCount_lastPrinted = (+date );

	var strTemp = "";
	if ( nPriority >= this.k_nPriority_high )
		strTemp += "[ "+ ( +nTickCount_fromLastPrinted ) +" ][ "+ ( +nTickCount_fromInit ) +" ][ "+ strCallerName +" ] <font color=red>" + HTMLEncode( strMessage ) + "</font><br>";
	else
		strTemp += "[ "+ ( +nTickCount_fromLastPrinted ) +" ][ "+ ( +nTickCount_fromInit ) +" ][ "+ strCallerName +" ] " + HTMLEncode( strMessage ) + "<br>";
	
	try
	{
		if ( !this.isBufferEmpty )
		{
			this.isBufferEmpty = true;
			this.wndDebug.document.all.divContent.innerHTML += this.strBuffer;
			this.strBuffer = "";
		}

		this.wndDebug.document.all.divContent.innerHTML += strTemp;
	}
	catch( e )
	{
		this.strBuffer += strTemp;
		this.isBufferEmpty = false;
	}
}

// (!!!) ¾Æ±Ô¸ÕÆ® ´ýÇÁµµ °°ÀÌ ÇÏ°í ½ÍÀºµ­... fnCallStackPointer.arguments °¡ ¸ÔÁö ¾Ê´Â´Ù. ¤Ð_¤Ì
function Debug_stackTrace()
{
	var strTemp = "";
	var fnCallStackPointer = this.stackTrace.caller;
	strTemp += "[ Start Call Stack Dump of "+ this.getFuncName( fnCallStackPointer ) +" ]<br>";
	while ( fnCallStackPointer )
	{
		strTemp += " &nbsp; &nbsp; &nbsp; [ "+ this.getFuncName( fnCallStackPointer ) +" ]<br>";
		fnCallStackPointer = fnCallStackPointer.caller;
	}
	strTemp += " &nbsp; &nbsp; &nbsp; [ IE ]<br>"
	strTemp += "[ End Call Stack Dump ]<br>";

	try
	{
		if ( !this.isBufferEmpty )
		{
			this.isBufferEmpty = true;
			this.wndDebug.document.all.divContent.innerHTML += this.strBuffer;
			this.strBuffer = "";
		}

		this.wndDebug.document.all.divContent.innerHTML += strTemp;
	}
	catch( e )
	{
		this.strBuffer += strTemp;
		this.isBufferEmpty = false;
	}
}



function Debug_init()
{
}

function Debug_getFuncName( /*function*/ fn )
{
	var result = null
	if ( fn )
	{
		var str = fn.toString();
		result = str.substring( 9, str.indexOf( "(" ) ) + "()";
	}
	else
	{
		result = "(IE)";
	}

	return result;
}

// Singleton object »ý¼º
var Debug = new __Debug();
Debug.init();
