var dbug = new DBugger();

function DBugger() {
	this.que = new Array();
	this.started = false;
	this.divid = 'wexdebug';
	this.width = 400;
	this.height = 200;
	this.fontSize = 8;
	this.limit = 10;
	this.div = null;
	this.add = function (str,level) {
		// if the str has a <pre> in it. Then put the pre in a collapseable div with the first line as the link (2008.11.24)
		var dboq = DBQO(str,level);
		if(str.match("<pre>")) {
			var tmp = str.split("<pre>");
			var nstr = '<a href="javascript:hideUnhide(\'pre'+this.que.length+'\');" style="color:'+dboq.color+'">'+tmp[0]+'...</a>';
			tmp.unshift();
			nstr += '<div id="pre'+this.que.length+'" style="display:none;">'+tmp.join("<pre>")+'</div>';
			dboq.src = nstr;
		} else if (str.match("\n")) { // now split on \n if it has multiple lines
			var tmp = str.split("\n");
			var nstr = '<a href="javascript:hideUnhide(\'pre'+this.que.length+'\');" style="color:'+dboq.color+'">'+tmp[0]+'...</a>';
			tmp.unshift();
			nstr += '<div id="pre'+this.que.length+'" style="display:none;">'+tmp.join("\n")+'</div>';
			dboq.src = nstr;
		} else if (str.length>100){
			var nstr = '<a href="javascript:hideUnhide(\'pre'+this.que.length+'\');" style="color:'+dboq.color+'">'+str.substr(0,100)+'...</a>';
			tmp.unshift();
			nstr += '<div id="pre'+this.que.length+'" style="display:none;">'+str.substr(100,str.length)+'</div>';
			dboq.src = nstr;
		}
		this.que.unshift(dboq);
		this.write();
	}
	this.write = function () {
		var div = document.getElementById(this.divid);
		if(div) {
			var showing = this.que.length<this.limit ? this.que.length : this.limit;
			if(!this.started) {
				div.innerHTML = '<div style="color:#ffffff;font-size:'+this.fontSize+'pt;font-famliy:arial;background-color:#990000;width:100%;">'+
										 '<center>WEX DEBUGGER (showing '+
											 '<input type="text" size="3" maxlength="3" value="'+this.limit+'" style="font-size:'+this.fontSize+'pt;font-family:arial;" onblur="dbug.setLimit(this.value);"> of <span id="wexdebugger_length">'+this.que.length+'</span>)<br> '+
											 ' width:<input type="text" size="4" maxlength="4" value="'+this.width+'" style="font-size:'+this.fontSize+'pt;font-family:arial;" onblur="dbug.setWidth(this.value);">'+
											 ' height:<input type="text" size="4" maxlength="4" value="'+this.height+'" style="font-size:'+this.fontSize+'pt;font-family:arial;" onblur="dbug.setHeight(this.value);">'+
											 ' fontSize:<input type="text" size="4" maxlength="4" value="'+this.fontSize+'" style="font-size:'+this.fontSize+'pt;font-family:arial;" onblur="dbug.setFontSize(this.value);">'+
										 '</center>'+
										 '<div id="wexdebugger_content" style="background-color:#000000;"></div>'+
									 '</div>';
				
				div.style.border = '1px solid #000';
				div.style.backgroundColor = '#000000';
				div.style.display = 'block';
				div.style.fontFamily = 'courier new';
				div.style.fontSize = this.fontSize+'pt';
				div.style.width = this.width+'px';
				div.style.height = this.height+'px';
				div.style.overflow = 'auto';
				div.align='left';
				this.started = true;
			}
			var content = document.getElementById('wexdebugger_content');
			content.innerHTML = '';
			for(var i=0; i < this.que.length; ++i) {
				if(i<this.limit) {
					content.innerHTML += '<div style="color:'+this.que[i].color+';"><span style="background-color:#999999;color:#ffffff;width:20px;text-align:right;">'+i+'</span><b>'+this.que[i].level+':</b> '+this.que[i].src+'</div><hr style="font-size:1pt;">';
				} else {
					break;
				}
			}
			document.getElementById('wexdebugger_length').innerHTML = this.que.length;
		} else {
			//alert('unable to find '+this.divid);
		}
	}
	this.setLimit = function (n) {
		this.limit = Math.abs(n);
		this.write();
	}
	this.setWidth = function (n) {
		this.width = n;
		document.getElementById(this.divid).style.width = this.width+'px';
	}
	this.setHeight = function (n) {
		this.height = n;
		document.getElementById(this.divid).style.height = this.height+'px';
	}
	this.setFontSize = function (n) {
		this.fontSize = n;
		document.getElementById(this.divid).style.fontSize = this.fontSize+'pt';
	}
}

function DBQO (str, level) {
	switch(level) {
		case 1: return new Object({'src':str,'level':'Important','color':'#ffff00'});
		case 2: return new Object({'src':str,'level':'Warning','color':'#ff9900'});
		case 3: return new Object({'src':str,'level':'Error','color':'#ff0000'});
		default: return new Object({'src':str,'level':'Comment','color':'#009900'});
	}
}