/******************************************************************************

	Copyright (C) 2006-2007  John Ha

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

******************************************************************************/

if (!khanh) var khanh = {};
if (!khanh.debug) {
	khanh.debug = {
		buffer: [],
	
		print: function(text, dump) {
			if (text != null) {
				if (dump) text = text.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n/g, '<br />');
				this.buffer.push(text + '');
			}
			if (document.body) {
				if (!this.box) {
					this.box = document.createElement('div');
					this.box.style.cssText = 'background:#fff;border: 5px solid #000';
					this.box.innerHTML = '<h2 style="text-align:center;color:#fff;background:#000;margin:0;padding:0 0 5px 0;">Debugging Output</h2>'
					+ '<div style="padding:5px;"></div>';
					document.body.appendChild(this.box);
					this.output = this.box.lastChild;
				}
				for (var i = 0; i < this.buffer.length; i++) {
					this.output.innerHTML += this.buffer[i] + '<br />\n';
				}
				this.buffer = [];
			} else if(!this.flush) {
				this.flush = function() { khanh.debug.print(); };
				khanh.event.add(window, 'domload', this.flush);
			}
		}
	};
}

function trace(text) { khanh.debug.print(text); };
function dump(text) { khanh.debug.print(text, 1); };
