/* JavaScript by Matti "Lumpio-" Virkkunen and Peter "Munter" MÃ¼ller
 *
 * This script is licensed under a Creative Commons Attribution-ShareAlike 2.5 License
 * More information about the license is available at:
 * http://creativecommons.org/licenses/by-sa/2.5/
 */

String.prototype.repeat = function(n) {
	var r = "", i;
	for (i = 0; i < n; i++) {
		r += this;
	}
	return r;
};

var Debug = function(title, pos) {
	this.entries = [];
	this.curItem = null;
	this.title = title || null;
	this.pos = pos || "ne";
	
	this.open();

	if (!Debug.firstInstance) {
		Debug.firstInstance = this;
	}
	
	if (Debug.buffer.length > 0) {
		var i;
		for (i = 0; i < Debug.buffer.length; i++) {
			var c = Debug.buffer[i];
			(c.line ? this.writeln : this.write)(c.str, c.color);
		}
	}
}

Debug.styleInserted = false;
Debug.indent = "    ";
document.write("\
<style type=\"text/css\">\
.debugger {\
	position: absolute;\
	border: 1px solid #000;\
	padding: 3px;\
	background-color: #EEE;\
	width: 250px;\
	-moz-border-radius: 4px;\
	font-family: sans-serif;\
	font-size: 0.8em;\
	z-index: 20000;\
}\
.debugger.nw {\
	top: 10px;\
	left: 10px;\
}\
.debugger.ne {\
	top: 10px;\
	right: 10px;\
}\
.debugger.sw {\
	bottom: 10px;\
	left: 10px;\
}\
.debugger.se {\
	bottom: 10px;\
	right: 10px;\
}\
.debugger.fold {\
	opacity: 0.3;\
}\
.debugger.fold:hover {\
	opacity: 1.0;\
}\
.debugger h2 {\
	font-size: 0.9em;\
	margin: 2px 0;\
}\
.debugger .toolbox {\
	margin: 2px 0;\
}\
.debugger .toolbox a {\
	display: block;\
	float: right;\
	margin-right: 1px;\
	width: 13px;\
	height: 13px;\
	color: #000;\
	text-decoration: none;\
	background-image: url(data:image/gif;base64,R0lGODlhRQAaAKIAAJkAAO7u7t3d3czMzLu7u5mZmWZmZv///yH5BAEAAAcALAAAAABFABoAAAP/OLrc/jDKyYK9OOt9B++f5YVKaJJZkY2fp2pqWcxz8F50bd25iKm3gGsT26VEw9/x+OIJcVCfbclyIqfKrLIJHXGx1egTGpQtcTpjk+bjcpNAI8uIMWvpaLYzjPfSX3NxHUlieHiCU35ZfFhjjTcyPTlAkmRdjUqBmIpRJSefKyZzmE9BXaOgJ6ihKCQUr7CxsQS0tba3uLm6u7y9tgLAwcLDxMXBBMbHycDIy7TL0M7DBsPNycgAxdnPBt3dAtTC3t/A4eDezOLgwsjm6s3ZwvHc0/X14ebU1vnK6+oC1uIJEEjvXzB35fzxa4YvIcCDENMFyyYQYDt7B8klxIfuVmHDhhfXgRxW0aK/iCjLjTupD6U5hg7DWRtI81jIlCcdioQI898+lAEn2jyHbhw1oyyT3nynzh08ksxmRpsq8RrGdCWBbZNKNRrXatC+svNFtqxZswkAADs%3D);\
	-moz-outline: none;\
}\
.debugger .toolbox a.clear {\
	width: 33px;\
	background-position: 0 0;\
}\
.debugger .toolbox a.clear:hover {\
	background-position: 0 -13px;\
}\
.debugger .toolbox a.fold {\
	background-position: -32px 0;\
}\
.debugger .toolbox a.fold:hover {\
	background-position: -32px -13px;\
}\
.debugger.fold .toolbox a.fold {\
	background-position: -44px 0;\
}\
.debugger.fold .toolbox a.fold:hover {\
	background-position: -44px -13px;\
}\
.debugger.se .toolbox a.fold, .debugger.sw .toolbox a.fold {\
	background-position: -44px 0;\
}\
.debugger.se .toolbox a.fold:hover, .debugger.sw .toolbox a.fold {\
	background-position: -44px -13px;\
}\
.debugger.fold.se .toolbox a.fold, .debugger.sw .toolbox a.fold {\
	background-position: -32px 0;\
}\
.debugger.fold.se .toolbox a.fold:hover, .debugger.sw .toolbox a.fold {\
	background-position: -32px -13px;\
}\
.debugger .toolbox a.close {\
	background-position: -56px 0;\
}\
.debugger .toolbox a.close:hover {\
	background-position: -56px -13px;\
}\
.debugger.ie .toolbox a {\
	font-size: 0.7em;\
	background-color: #EEE;\
	border: 1px solid #666;\
	text-align: center;\
}\
.debugger.ie .toolbox a:hover {\
	background-color: #BBB;\
}\
.debugger ul {\
	list-style: none;\
	padding: 2px;\
	height: 300px;\
	margin: 0;\
	overflow: auto;\
	background-color: #FFF;\
	border: 1px solid #AAA;\
	font-family: monospace;\
}\
.debugger ul li {\
	white-space: pre;\
	padding: 2px;\
	border-top: 1px solid #CCC;\
}\
.debugger.fold ul {\
	display: none;\
}\
</style>\
");
Debug.isIE = navigator.userAgent.toLowerCase().indexOf("msie") != -1;

Debug.prototype = {
	open: function() {
		var e;
		
		if (!this.el) {
			this.el = document.createElement("div");
			this.el.className = "debugger " + this.pos + (Debug.isIE ? " ie" : "");
			
			this.toolbox = document.createElement("div");
			this.toolbox.className = "toolbox";
			var obj = this;
			
			(function(c, t, f, ietext) {
				var e = document.createElement("a");
				e.obj = obj;
				e.className = c;
				e.title = t;
				e.onclick = f;
				e.href = "#";
				if (Debug.isIE) {
					e.appendChild(document.createTextNode(ietext));
				}
				obj.toolbox.appendChild(e);
				obj["btn" + c] = e;
				return arguments.callee;
			})
			("close", "Close", this.close, "X")
			("fold", "Fold/unfold", this.toggle, "_")
			("clear", "Clear log", this.clear, "CLEAR");

			this.el.appendChild(this.toolbox);
			
			e = document.createElement("h2");
			e.appendChild(document.createTextNode(this.title || "Debug log"));
			this.el.appendChild(e);
	
			this.list = document.createElement("ul");
			if (this.pos == "se" || this.pos == "sw") {
				this.el.insertBefore(this.list, this.el.firstChild);
			} else {
				this.el.appendChild(this.list);
			}
	
			document.body.appendChild(this.el);
		}
	},

	clear: function() {
		obj = this.obj || this;
		obj.entries = [];
		obj.curItem = null;
		var l = document.createElement("ul");
		obj.list.parentNode.replaceChild(l, obj.list);
		obj.list = l;
		return false;
	},

	close: function() {
		obj = this.obj || this;
		obj.clear();
		obj.el.parentNode.removeChild(obj.el);
		obj.el = null;
		return false;
	},

	fold: function() {
		this.el.className = "debugger " + this.pos + " fold" + (Debug.isIE ? " ie" : "");
	},

	unfold: function() {
		this.el.className = "debugger " + this.pos + (Debug.isIE ? " ie" : "");
	},

	toggle: function() {
		obj = this.obj || this;
		(obj.el.className.indexOf("fold") == -1) ? obj.fold() : obj.unfold();
		return false;
	},

	setWidth: function(w) {
		this.el.style.width = w + "px";
	},

	setHeight: function(h) {
		this.el.style.height = h + "px";
//		window.status += this.toolbox.offsetHeight;
//		window.status += this.toolbox.firstChild.offsetHeight;
		this.list.style.height = (this.el.offsetHeight - this.toolbox.firstChild.offsetHeight - 16) + "px";
	},

	write: function(s, c) {
		if (!this.curItem) {
			this.writeln(s, c);
			this.curItem = this.entries[this.entries.length - 1];
		} else {
			this.curItem.firstChild.nodeValue += s;
		}
	},

	writeln: function(s, c) {
		var e;

		e = document.createElement("li");
		e.appendChild(document.createTextNode(s));
		if (typeof c != "undefined") {
			e.style.color = c;
		}
		
		this.curItem = null;
		this.entries.push(e)
		this.list.insertBefore(e, this.list.firstChild);
	},

	print_r: function(o, d) {
		var i, r;
		if (typeof d == "undefined") {
			d = 0;
		}
		if (typeof o == "undefined") {
			return "undefined";
		} else if (typeof o == "string") {
			return "\"" + o + "\"";	
		} else if (o === null) {
			return "null";
		} else if (o instanceof Array) {
			r = "[\n";
			for (i = 0; i < o.length; i++) {
				if (typeof(o[i]) == "function") {
					continue;
				}
				if (r != "[\n") {
					r += ",\n";
				}
				r += Debug.indent.repeat(d + 1) + this.print_r(o[i], d + 1);
			}
			return r + "\n" + Debug.indent.repeat(d) + "]";
		} else if (typeof o == "object") {
			r = "{\n";
			for (i in o) {
				if (typeof(o[i]) == "function") {
					continue;
				}
				if (r != "{\n") {
					r += ",\n";
				}
				r += Debug.indent.repeat(d + 1) + i + ": " + this.print_r(o[i], d + 1);
			}
			if (r == "{\n") {
				return "{ }";
			} else {
				return r + "\n" + Debug.indent.repeat(d) + "}";
			}
		} else {
			return String(o);
		}
	}
};

Debug.print_r = Debug.prototype.print_r;

Debug.buffer = [];

Debug.writeln = function(s, c) {
	if (Debug.firstInstance) {
		Debug.firstInstance.writeln(s, c);
	} else {
		Debug.buffer.push({line: true, str: s, color: c});
	}
};
Debug.write = function(s, c) {
	if (Debug.firstInstance) {
		Debug.firstInstance.write(s, c);
	} else {
		Debug.buffer.push({line: false, str: s, color: c});
	}
};

