/**
 * Mimicks the Firebug console object for non-firebug browsers.
 * TODO
 * 		- Add debugging flag and only output debug messages when set to true
 * 		- Add draggability
 * 		- Add cookie to remember position
 */
dbug = {
	count:		0,
	enabled:	true,
	window:		false,
	useFirebug: false,
	messages:	[],
	customEventLogging: false,
	init: function() {
	},
	init: function() { dbug.createConsole() },
	createConsole: function() {
		this.console = document.body.insertBefore(
			$dom.create('div',{id:'dbugConsole'}),document.body.firstChild);
		this.title =	this.console.appendChild($dom.create('h5',{id:'dbugTitle'},'Debugger'));
		this.list =		this.console.appendChild($dom.create('ol',{id:'dbugList'}));
		this.form =		this.console.appendChild($dom.create('form',{id:'dbugForm'}));
		this.input =	this.form.appendChild($dom.create('input',{id:'dbugInput'}));
		this.submit =	this.form.appendChild($dom.create('input',{type:'input',type:'submit',id:'dbugSubmit',value:'GO!'}));
		this.console.style.display = 'none';
		this.setStyles();
		this.bindUIEvents();
	},
	addMessage: function(message, level) {
		if (typeof this.console == 'undefined') {
			this.messages.push([message,level]);
			this.interval = setInterval('dbug.printQueue()',50);
			return;
		}
		this.printMessage(message,level);
	},
	printQueue: function() {
		if (typeof this.console == 'undefined') { return; }
		clearInterval(this.interval);
		while (this.messages.length > 0) {
			var m = this.messages.shift();
			this.printMessage(m[0],m[1]);
		}
	},
	printMessage: function(message, level) {
		if (this.console.style.display != 'block') {
			this.console.style.display = 'block'
		}
		var o = this.list.appendChild($dom.create('li',{className:level}));
		o.innerHTML = message;
		this.list.scrollTop = this.list.scrollHeight;
	},
	showConsole: function() {
		this.addMessage('Turning Console On','info');
	},
	setStyles: function() {
		// will dynamically append stylesheet here
	},
	bindUIEvents: function() {
		this.form.onsubmit = function() { return false; }
		PopJavaScriptFramework.v1B1.event.add(this.submit,'click',this.eval)
	},
	eval: function() {
		o = eval(dbug.input.value);
		dbug.input.value = '';
		dbug.addMessage(o,'log');
	},
	build: function(type, args) {
		var o = '';
		for (var i=0; i<args.length; i++) {
			o += args[i] + '<br />';
		}
		this.addMessage(this.count.toString() + '. ' + o, type);
		this.count++;
	},
	log:	function() { dbug.build('log', arguments); },
	debug:	function() { dbug.build('debug', arguments); },
	info:	function() { dbug.build('info', arguments); },
	error:	function() { dbug.build('error', arguments); },
	warn:	function() { dbug.build('warn', arguments); }
}
if (typeof console != "undefined") { // both safari and firebug have [console] objects
	if (typeof console.debug != "undefined") { // only firebug has the console.debug object
		dbug.log = console.log;
		dbug.debug = console.debug;
		dbug.info = console.info;
		dbug.error = console.error;
		dbug.warn = console.warn;
		dbug.useFirebug = true;
	}
}
if (dbug.useFirebug == false) {
	PopJavaScriptFramework.v1B1.event.add(window,'load',dbug.init);
}
/*
window.onerror = function(msg,url,line) {
	dbug.error('Error on Line: ' + line,msg);
	return true;
}
*/