function sableException(message)
{
	this.message	= message;
	this.name		= "SableException";
}

function wDebugWindowFocusNextLevel()
{
	if(!window.wDebugWindowFocusDepth)
		window.wDebugWindowFocusDepth = 1000000;
	else	
		window.wDebugWindowFocusDepth ++;
	return window.wDebugWindowFocusDepth;
}

function wDebugWindowFocus(id)
{
	var l=wDebugWindowFocusNextLevel();
	document.getElementById(id).style.zIndex = l;
}

function wDebugWindow(m,t)
{
	if(!window.wDebugWindowCounter)
		window.wDebugWindowCounter = 1;
	else	
		window.wDebugWindowCounter ++;

	var d = document.createElement("DIV");
	d.style.position = "absolute";
	d.style.left = "10px";
	d.style.top = "10px";
	d.style.zIndex = wDebugWindowFocusNextLevel();
	d.id = "debugWindow_"+wDebugWindowCounter;
	d.onmousedown = function(){wDebugWindowFocus(this.id)};

	var html ="";
	html += "<table cellpadding='0' cellspacing='4' style='background-color:rgb(170,170,170);'>";
		html += "<tr style='cursor:move'>";
			html += "<td>";
				html += "<table cellpadding='0' cellspacing='0' style='width:100%'><tr><td style='width:100%;font-size:11px;color:rgb(50,50,50);'>"+t+"</td><td style='font-family:Arial;font-size:12px;color:rgb(50,50,50);font-weight:bold;cursor:pointer' align='center'><div ontouchend='wRemoveElement(\""+d.id+"\")' onclick='wRemoveElement(\""+d.id+"\")' align='center' style='width:20px'>X</div></td></tr></table>";
			html += "</td>";
		html += "</tr>";
		html += "<tr>";
			html += "<td>";
				html += "<table cellpadding='0' cellspacing='0' style='width:100%;background-color:rgb(250,250,250);'><tr><td style='font-size:12px;color:rgb(50,50,50);padding:5px'>"+m+"</td></tr></table>";
			html += "</td>";
		html += "</tr>";
	html += "</table>";
	
	d.innerHTML = html;
	document.body.appendChild(d);
}

function wDebug(o,l,t)
{
	if(!t)t="Debug Message";
	if(!l)l=2;
	var h=wDebugHtml(o,l,"");
	wDebugWindow(h, t);
};

function wDebugHtml(o,l,t)
{
	var r="";
	if(typeof(o)=="object")
	{
		if(!l)l=2;
		if(!t)t="";
		for(var i in o)
		{
			if(l>1 && typeof(o[i])=="object")
				r+= t+i+"= [STRUCT]<br>"+wDebugHtml(o[i],l-1,t+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;")+"<br/>";
			else
				r+= t+i+"="+(o[i]+"").replace(/\n/g,"<br/>"+t+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;")+"<br/>";
		}
	}
	else r = o+"";
	return r;
};

window.onerror = function(event, where, line, num){wDebug("Error: "+event+"<br/>File: "+where+"<br/>Line: "+line,2,"Javascript Error"); return false};

function wRemoveElement(o)
{
	if(typeof(o)=="string")	o = document.getElementById(o);
	if(o) o.parentNode.removeChild(o);
}
