var __skip_line = '-----------------------------------------------------------------------';

function debugConsole()
{
	__global_debugger = this;
// create console
	this.consoleHolder = document.createElement('DIV');
	this.consoleHolder.className = 'debugConsole';
	this.consoleHolder.id = '__debug_console_holder';
	this.consoleHolder.style.height = 0;
	document.body.appendChild(this.consoleHolder);
	this.timer = null;
	
	this.consoleBody = document.createElement('DIV');
	this.consoleBody.className = 'debugConsoleBody';
	this.consoleBody.id = '__debug_console_body';
	this.consoleHolder.appendChild(this.consoleBody);
	this.msg = function (str,sender)
	{
		if (typeof(sender) != 'undefined')
		{
			var strng = document.createElement('strong');
			strng.appendChild(document.createTextNode(sender + ': '));
			$('__debug_console_body').appendChild(strng);
		}
		$('__debug_console_body').appendChild(document.createTextNode(str));
		$('__debug_console_body').appendChild(document.createElement('BR'));					
		if (__global_debugger.console_height == 300)
			$('__debug_console_body').scrollTop = 100000;
	}
	this.msg('Created console instance.', 'Debug Console');	
	this.cmdReaction = function (event) {
 		key = event.keyCode ? event.keyCode : event.charCode;
		if (event.ctrlKey && key == 13)
		{
			$('__debug_console_body').appendChild(document.createTextNode('# ' + $('__debug_console_cmd_line').value));
			$('__debug_console_body').appendChild(document.createElement('BR'));
			try {
				var cmdResult = eval($('__debug_console_cmd_line').value);
			}
			catch (err)
			{
				if (document.all)
					cmdResult = 'Error: ' + err.description;
				else
					cmdResult = err;
			}
			$('__debug_console_cmd_line').value = '';
			$('__debug_console_body').appendChild(document.createTextNode(cmdResult));
			$('__debug_console_body').appendChild(document.createElement('BR'));
			if (__global_debugger.console_height == 300)
				$('__debug_console_body').scrollTop = 100000;
		}
	}
	this.consoleCmd = document.createElement('input');
	this.consoleCmd.type = 'text';
	this.consoleCmd.className = 'consoleCmd';
	this.consoleCmd.id = '__debug_console_cmd_line';
	this.consoleHolder.appendChild(this.consoleCmd);
	if (document.addEventListener)
		this.consoleCmd.addEventListener('keypress', this.cmdReaction,false);
	else
		this.consoleCmd.attachEvent("onkeydown", this.cmdReaction);
	this.msg('Created console command line.', 'Debug Console');
// events 
	this.onKeyPress = function (event)
	{
 		key = event.keyCode ? event.keyCode : event.charCode;
    	if (event.ctrlKey && (key == 96 || key == 192))
    		if (__global_debugger.timer == null)
    			__global_debugger.time = setTimeout(__global_debugger.moveConsole, 20);	
	}
	
	if(document.addEventListener)
		document.addEventListener('keypress', this.onKeyPress,false);
	else
		document.attachEvent("onkeydown", this.onKeyPress);
	this.direction = +10;
	this.console_height = 0;
	this.debug = function (msg,var_name,nobr,__parent_node,_marg)
	{
		if (__parent_node == undefined)
			__parent_node = $('__debug_console_body');
		if (_marg == undefined)
			_marg = 15;
		if (nobr == undefined)
			nobr = false;
		if (var_name != null && var_name != undefined)
			__parent_node.appendChild(document.createTextNode(var_name + ' = '));
		if (typeof(msg) == 'object')
		{
			__parent_node.appendChild(document.createTextNode('object {'));
			__parent_node.appendChild(document.createElement('BR'));
			for (k in msg)
			{
				var span_tab = document.createElement('span');
				span_tab.appendChild(document.createTextNode(' '));
				span_tab.appendChild(document.createTextNode("    " + k+' : '));
				debug(msg[k],null,true,span_tab,_marg+15);
				span_tab.appendChild(document.createTextNode(",\n"));
				span_tab.style.marginLeft = _marg + 'px';
				__parent_node.appendChild(document.createTextNode(" "));
				__parent_node.appendChild(span_tab);
				__parent_node.appendChild(document.createElement('BR'));
			}
			var span_tab = document.createElement('span');
			span_tab.appendChild(document.createTextNode("}"));
			span_tab.style.marginLeft = (_marg -15) + 'px';
			__parent_node.appendChild(span_tab);
			if (!nobr)
				__parent_node.appendChild(document.createElement('BR'));

		} 
		else if (typeof(msg) == 'array')
		{
			__parent_node.appendChild(document.createTextNode("array { "));		
			for (i = 0; i < msg.length; i++)
				__parent_node.appendChild(document.createTextNode(typeof(msg[i]) == undefined ? 'undefined, ': msg[i]));
			if (!nobr)
				__parent_node.appendChild(document.createElement('BR'));				
		} 
		else if (typeof(msg) == 'undefined')
		{
			__parent_node.appendChild(document.createTextNode("undefined"));
			if (!nobr)
				__parent_node.appendChild(document.createElement('BR'));					
		} 
		else
		{
			__parent_node.appendChild(document.createTextNode(msg));
			if (!nobr)
				__parent_node.appendChild(document.createElement('BR'));			
		}
		if (__global_debugger.console_height == 300)
			$('__debug_console_body').scrollTop = 100000;
	}
	this.moveConsole = function()
	{
		 __global_debugger.console_height =  __global_debugger.console_height + __global_debugger.direction;
		//alert(this.console_height);
		//clearTimeout(__global_debugger.timer);
		//return;		
		$('__debug_console_holder').style.height =  __global_debugger.console_height + 'px';
		if ( __global_debugger.console_height == 0)
		{
			clearTimeout(__global_debugger.timer);
			__global_debugger.direction = 10;
			__global_debugger.timer = null;
		} else if ( __global_debugger.console_height == 300)
		{
			clearTimeout(__global_debugger.timer);
			__global_debugger.direction = -10;
			__global_debugger.timer = null;
			$('__debug_console_cmd_line').focus();
			$('__debug_console_body').scrollTop = 100000;
		} else
			this.time = setTimeout(__global_debugger.moveConsole, 20);
	}
	this.msg('Debug console was successfuly initialized.');
	this.msg(__skip_line);
}

function _alert(str,sender)
{
	if (typeof(__global_debugger) != 'undefined')
		__global_debugger.msg(str,sender);
}

function debug(msg,var_name,nobr,__parent_node,_marg)
{
	if (typeof(__global_debugger) != 'undefined')
		__global_debugger.debug(msg,var_name,nobr,__parent_node,_marg);
}