// vkDebug.js

function	vkDebugClass()
{
	this.container = null;
	this.log = null;
	this.buttons = null;
	this.enabled = false;
	this.errors = false;
}

vkDebugClass.prototype.enable = function()	// (enable, errors)
{
	if(	arguments.length > 0 &&
		!arguments[0])
		this.enabled = false;
	else
	{
		this.enabled = true;

		if(	arguments.length > 1 &&
			arguments[1] )
		{
			if(!this.errors)
			{
				//if(window.onerror)
				{
					//alert('Setting');
					//window.onerror = function(msg, url, line) { return vkDebug._showError(event, msg, url, line); };
//					window.onError = function() { return vkDebug._showError('test', 'test', 'test');  };
				}
				
				/*
				if(window.addEventListener)
					window.addEventListener('error', function(msg, url, line) { vkDebug._showError(msg, url, line) }, true);
				*/

				/*
				Error.prototype.Error = function()
				{
					alert('Error handling here');
				}
				*/

				/*
				alert('Enabling errors');
				if(window.addEventListener)
					document.addEventListener('error', function(msg, url, line) { vkDebug._showError(msg, url, line) }, false);
				else if(window.attachEvent)
					window.attachEvent('onerror', function(msg, url, line) { vkDebug._showError(msg, url, line) });
				*/
				this.errors = true;
			}
		}
	}
}

vkDebugClass.prototype.text = function(string)
{
	if(this.enabled)
		this._write(vkDom.nl2br(vkDom.html(string))+'<br />');
}


vkDebugClass.prototype.html = function(string)
{
	if(this.enabled)
		this._write(string+'<br />');
}


vkDebugClass.prototype.domtree = function(element, skiptext)
{
	if(this.enabled)
		this._domtree(element, skiptext, 0);
}

// PRIVATE



vkDebugClass.prototype._domtree = function(node, skiptext, depth)
{
	var	i, str;

	str = '+ ';

	switch(node.nodeType)
	{
		case 1:	// ELEMENT_NODE
			str += 'ELEMENT_NODE [1] ['+node.tagName+']';
			if(node.id)
				str += ' #'+node.id;
			break;

		case 2:	// ATTRIBUTE_NODE
			str = null;
			break;

		case 3:	// TEXT_NODE
			if(!skiptext)
				str += 'TEXT_NODE [3]';
			else
				str = null;
			break;

		case 4:	// CDATA_SECTION_NODE
			if(!skiptext)
				str += 'CDATA_SECTION_NODE [4]';
			else
				str = null;
			break;

		case 5:	// ENTITY_REFERENCE_NODE
			str += 'ENTITY_REFERENCE_NODE [5]';
			break;

		case 6:	// ENTITY_NODE
			str += 'ENTITY_NODE [6]';
			break;

		case 7:	// PROCESSING_INSTRUCTION_NODE
			str += 'PROCESSING_INSTRUCTION_NODE [7]';
			break;

		case 8:	// COMMENT_NODE
			if(!skiptext)
				str += 'COMMENT_NODE [8]';
			else
				str = null;
			break;

		case 9:	// DOCUMENT_NODE
			str += 'DOCUMENT_NODE [9]';
			break;

		case 10:// DOCUMENT_TYPE_NODE
			str += 'DOCUMENT_TYPE_NODE [10]';
			break;
		
		case 11:// DOCUMENT_FRAGMENT_NODE
			str += 'DOCUMENT_FRAGMENT_NODE [11]';
			break;

		case 12:// NOTATION_NODE
			str += 'NOTATION_NODE [12]';
			break;

		default:
			str += 'UNKNOWN_TYPE ['+node.nodeType+']';
	}

/*
	NodeType 	Named Constant
	1			ELEMENT_NODE
	2 			ATTRIBUTE_NODE
	3 			TEXT_NODE
	4 			CDATA_SECTION_NODE
	5 			ENTITY_REFERENCE_NODE
	6 			ENTITY_NODE
	7 			PROCESSING_INSTRUCTION_NODE
	8 			COMMENT_NODE
	9 			DOCUMENT_NODE
	10 			DOCUMENT_TYPE_NODE
	11 			DOCUMENT_FRAGMENT_NODE
	12 			NOTATION_NODE
*/


	if(str != null)
	{
		var	spacer = '';

		for(i = 0; i < depth*2; i++)
			spacer += '&nbsp;';		// unfortunately, IE ignores "white-space: pre"

		this._write(spacer+vkDom.html(str)+'<br />');	
	}

	if(node.nodeType == 1 && node.tagName == 'DIV' && node.id == 'vkDebug')
		return;
	
	if(node.hasChildNodes())
	{
		depth++;
		for(i = 0; i < node.childNodes.length; i++)
			this._domtree(node.childNodes[i], skiptext, depth)
	}
}



vkDebugClass.prototype._write = function(string)
{
	if(!this.log)
	{
		// Get last position and dimensions from cookie
		this._getParams();

		this.container = document.createElement('div');
		this.container.id = 'vkDebug';

		this.log = document.createElement('div');
		this.log.className = 'log';
		this.log.style.width = this.width+'px';
		this.log.style.height = this.height+'px';

		this.container.appendChild(this.log);

		this.buttons = document.createElement('div');
		this.buttons.className = 'buttons';
		this.buttons.style.width = this.width+'px';

		this.buttons.innerHTML = 
			'<button onclick="return vkDebug._hide()">Hide</button>'+
			'<button onclick="return vkDebug._setpos()">Pos</button>'+
			'<button onclick="return vkDebug._resize(true, false)">W -</button>'+
			'<button onclick="return vkDebug._resize(true, true)">W +</button>'+
			'<button onclick="return vkDebug._resize(false, false)">H -</button>'+
			'<button onclick="return vkDebug._resize(false, true)">H +</button>'+
			'<button onclick="return vkDebug._clear()">Clear</button>&nbsp;';

		this.container.appendChild(this.buttons);

		document.body.appendChild(this.container);

		this._updatePosition();

		if(window.addEventListener)
		{
			window.addEventListener('resize', function() { vkDebug._updatePosition() }, false);
			window.addEventListener('scroll', function() { vkDebug._updatePosition() }, false);
		}
		else if(window.attachEvent)
		{
			window.attachEvent('onresize', function() { vkDebug._updatePosition() });
			window.attachEvent('onscroll', function() { vkDebug._updatePosition() });
		}
	}

	this.container.style.display = 'block';

	this.log.innerHTML += string;
	
	this.log.scrollTop = this.log.scrollHeight;
}

vkDebugClass.prototype._getParams = function()
{
	var	cookie, parts;

	this.width = null;
	this.height = null;
	this.position = null;

	cookie = vkDom.getCookie('vkDebug');

	if(cookie)
	{
		parts = cookie.split('|');
		if(parts.length == 3)
		{
			this.width = parseInt(parts[0]);
			this.height = parseInt(parts[1]);
			this.position = parseInt(parts[2]);
		}
	}

	if(this.width == null)
	{
		this.width = 400;
		this.height = 50;
		this.position = 1;
	}

	this._normalizeParams();
}

vkDebugClass.prototype._updateParams = function()
{
	vkDom.setCookie('vkDebug', this.width+'|'+this.height+'|'+this.position, 7*24*60*60);
}

vkDebugClass.prototype._normalizeParams = function()
{
	if(this.width < 350)
		this.width = 350;
	else if(this.width > 800)
		this.width = 800;

	if(this.height < 100)
		this.height = 100;
	else if(this.height > 800)
		this.height = 800;

	if(this.position < 1 || this.position > 4)
		this.position = 1;
}

vkDebugClass.prototype._updatePosition = function()
{
	var	x, y;

	switch(this.position)
	{
		case 1:	// LEFT/TOP
			x = 0;
			y = 0;
			break;

		case 2:	// RIGHT/TOP
			x = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft) + document.documentElement.clientWidth-this.container.clientWidth;
			y = 0;
			break;

		case 3:	// RIGHT/BOTTOM
			x = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft) + document.documentElement.clientWidth-this.container.clientWidth;
			y = Math.max(document.documentElement.scrollTop, document.body.scrollTop) + document.documentElement.clientHeight-this.container.clientHeight;
			break;

		case 4:	// LEFT/BOTTOM
			x = 0;
			y = Math.max(document.documentElement.scrollTop, document.body.scrollTop) + document.documentElement.clientHeight-this.container.clientHeight;
			break;

	}

	this.container.style.left = x+'px';
	this.container.style.top = y+'px';
}

// Callbacks

vkDebugClass.prototype._clear = function()
{
	this.log.innerHTML = '';
	return false;
}

vkDebugClass.prototype._resize = function(width, more)
{
	if(width)
	{
		if(more)
			this.width += 50;
		else
			this.width -= 50;
	}
	else
	{
		if(more)
			this.height += 50;
		else
			this.height -= 50;
	}

	this._normalizeParams();
	this._updateParams();

	this.log.style.width = this.width+'px';
	this.log.style.height = this.height+'px';

	this.buttons.style.width = this.width+'px';
	
	this._updatePosition();
	return false;
}

vkDebugClass.prototype._hide = function()
{
	this.container.style.display = 'none';
}

vkDebugClass.prototype._setpos = function()
{
	this.position++;
	this._normalizeParams();
	this._updateParams();
	
	this._updatePosition();
	return false;
}


vkDebugClass.prototype._showError = function(ev, msg, url, line)
{
	vkDebug.html('<span style="color: white; background-color: red;">*** JAVASCRIPT ERROR ***</span>');
	this.text('FILE: '+url);
	this.text('LINE: '+line);
	this.text('MESSAGE: '+msg);
	return true;
}


var	vkDebug = new vkDebugClass();
