/*
	Debug Utils v0.1
	
	Provides a debug window for displaying traces
	and timers for performance testing.
	
	Main methods are made available globally for ease of use.
*/
if(typeof(TL) == "undefined") var TL = new Object();


TL.Debug = function(){
	return{
		openWindow: function(){
			var w = window.open(null,null,"toolbar=no,width=500,height=250,directories=no,scrollbars=yes");
			w.document.write("<html><title>Debug</title><body></body></html>");
			w.onbeforeunload = w.document.body.onbeforeunload = function(){
				TL.Debug.debugWindow = null;
			}
			
			return w;
		},
		trace: function($str){
			
			if(!TL.Debug.debugWindow){
				TL.Debug.debugWindow = TL.Debug.openWindow();
			}
			
			var w = TL.Debug.debugWindow;
			/*w.focus();*/
		
			var p = w.document.createElement("p");
			p.appendChild(w.document.createTextNode($str));
			w.document.body.appendChild(p);
			w.scroll(0,w.document.body.offsetHeight);
		},
		setTimer: function($id){
			if(!TL.Debug.timers){
				TL.Debug.timers = new Object();
			}
			TL.Debug.timers[$id] = {startTime:new Date().getTime()};
			return null;
		},
		timer: function($id){
			TL.Debug.trace($id+" :: "+(new Date().getTime() - TL.Debug.timers[$id].startTime));
			return null;
		}
	}
}();

window.trace = TL.Debug.trace;
window.setTimer = TL.Debug.setTimer;
window.timer = TL.Debug.timer;
