//$Id: Debug.js,v 1.5.2.1 2006/02/09 22:16:26 spyle Exp $
/*
DOCUMENTATION
Include this file: <script language="javascript" type="text/javascript" src="Debug.js"></script>
*/

//Class that creates a popup and writes debug messages to it.
function Debug(enable)
{
	var enabled = enable?true:false;
	var win = null;
	
	//Enables the output messages
	this.enable = function()
	{
		enabled = true;
	}
	
	this.disable = function()
	{
		enabled = false;
	}
	
	this.output = function(message)
	{	
		if(!enabled)
			return;
		
		if(null == win || win.closed) //no window, create one
		{
			win = true; //set true to stop a plethora of other windows being created
			win = window.open('','js_debug_window','height=300,width=500,resizable=yes,status=yes,scrollbars=yes');
		}
					
		//sleep to allow the window to be created before writing to it
		stall(2000);
		
		win.document.write('['+get_now_timestamp()+'] '+message+'<br />');		
	}

	
	this.outputObject = function(object,object_name,depth)
	{
		var s = '';
		var s = '<pre><b>'+object_name+' is type '+typeof(object)+'</b>\n';
		
		s += getObjectString(object,object_name,depth,'');
		
		this.output(s+'</pre>');
	}
}

//Outputs the properties of the object to the debug window.
function getObjectString(object,object_name,depth,indent)
{
	alert(1);
	if(depth == 0)
		return;
		
	var s = '';
		
	for(attribute in object)
	{
		try
		{
			s += indent+object_name+'['+attribute+'] = ';

//alert(object[attribute]);			
			switch(typeof(object[attribute]))
			{
				case 'undefined':
					break;
				case 'function':
					//if('addEventListener'==attribute){alert(object[attribute]);}
					s += '[function]';
					break;
				case 'string':
//						if('innerHTML' == attribute)
//						{
//							s += '[HTML]'	
//							break;
//						}
					s += object[attribute].substr(0,100);
					break;
				case 'object':
					//alert(typeof(object[attribute]));
					s += '\n'+getObjectString(object[attribute],attribute,depth-1,indent+'\t');
				case 'number':
				default:
					//alert(object[attribute]);
					s += object[attribute];
					break;
			}
					
			s += '\n';
		}
		catch(e){;}
	}
	
	return s;
}


//Returns a string with the current date and time
function get_now_timestamp()
{
	//build timestamp
	now = new Date();
	month = now.getMonth()+1;
	
	return now.getFullYear()+'-'+
		month+'-'+
		now.getDate()+' '+
		now.getHours()+':'+
		now.getMinutes()+':'+
		now.getSeconds();
}

//Wrapper function for setTimeout so that you don't have to provide a function to call.
function stall(time_in_ms)
{
	if(0 == time_in_ms) //done
		return;
	
	setTimeout('stall(0)',time_in_ms)
}

//MAIN CODE
var debug = new Debug(false);
