﻿// JavaScript Document

// ATTENTION
//
// this method of prototyping is required to be able to access function's "arguments.callee"
// it makes references to existing functions available within class prototypes

function BK_Debug()
{
	//
	// Class to open a window and permit to write into it
	//
	// prerequisit : set state to "true" to enable writing to window content
	//
	
	this._debugWindow	= null;
	this._state			= false;
}

function BK_Debug_append(msg)
{
	//
	// append msg to window text content
	//
	
	if(this._state)
	{
		if(this._debugWindow == null)
		{
			this._debugWindow = window.open('', 'BK_DebugWindow', 'height=200,width=400,scrollbars=1');
		}
		
		this._debugWindow.document.writeln(msg + "<br />");
	}
}

function BK_Debug_setState(state)
{
	//
	// force setting "_state" to true of false
	// ensure that debug window won't open in a "false" production state
	//
	
	switch(state)
	{
		case true:
		case false:
			this._state = state;
			break;
			
		default:
			this._state = false;
			break;
	}
}

function BK_Debug_functionName(f)
{
	// receives arguments.callee.toString() from caller
	// implicitly placed "f" as passed value from caller
	// to maintain futur compatibility
	// because "caller" is a deprecated arguments property in javascript
	
	var temp	= f.substr('function '.length);
		temp	= temp.substr(0, temp.indexOf('('));
												
	this.append(temp + "()");
}

//
// alter prototype of Class
//
BK_Debug.prototype.append		= BK_Debug_append;
BK_Debug.prototype.setState		= BK_Debug_setState;
BK_Debug.prototype.functionName	= BK_Debug_functionName;

$BK_Debug = new BK_Debug();

if(typeof($BK) != "undefined")
{
	//
	// register object into registered objects list of $BK framework
	//
	$BK.registerObject($BK_Debug);
}
