
var currpath = new Array();
var showNull = true;
var updateHandler;

function setUpdateHandler(upPage)
{
	updateHandler = upPage;
}

function toggleNull()
{
	showNull = !showNull;
	updateHandler();
}

function showPath()
{
	if( currpath.length == 0 ) return "&lt;root&gt;";
	return "["+typeof getCurrObj()+"] " +currpath.length+": <b>" +currpath.join(" . ")+"</b>";
}

function getPath(k)
{
	var r = "this";
	if( k==-1 ) k = currpath.length;
	for( var i=0; i<k; i++ )
	{
		r += "[\"" + currpath[i] + "\"]";
	}
	return r;
}

function goUp()
{
	if( currpath.length > 0 ) currpath.length--;
	updateHandler();
	return showPath();
}

function pathAdd(s)
{
	currpath[currpath.length] = s;
	return showPath();
}

function sPathAdd(t)
{
        pathAdd(t.innerHTML);
	updateHandler();
        return false;
}


function totext(s)
{
	var r="";
	for( var i=0; i<s.length; i++ )
	{
		if( s.charAt(i) == "<" ) r+= "&lt;";
		else if( s.charAt(i) == ">" ) r+= "&gt;";
		else if( s.charAt(i) == "&" ) r+= "&amp;";
		else r+=s.charAt(i);
	}
	return r;
}

function getCurrObj()
{
	var s = getPath(-1);

	{
		// check existence
		for( var  i=0; i<currpath.length; i++ )
		{
			var p = getPath(i);
			if( eval(p) == null )
				return p+" UNDEFINED";
		}
	}

	return eval(s);
}

function showObjs()
{
	var r="";

	var o = getCurrObj();

	r+="<b><a href='#' onclick='goUp(); return false;'>UP</a></b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ";

	r+="show <b><a href='#' onclick='toggleNull(); return false;'>null</a> = </b>"+showNull+"<hr> ";

	if( o == null )
		return r+=" UNDEFINED";

	var to = typeof o;


	if( to == "object" )
	{     
	  r+="{";
	  try{
	  for( i in o )
	  {
		try{
		 if( typeof o[i] == "object" )
		 {
			if( o[i] != null )
				r+="<b><a href='#' onclick='return sPathAdd(this);'>"+i+"</a></b> ";
			else if( showNull )
				r+="<i>"+i+"</i> ";
		 }
		}catch(e){} // this is to prevent accessing not implemented objects
	  }

	  r += "}<br><br>{";
	  for( i in o )
	  {
		try{
		var inf = typeof o[i];
		}catch(e){ continue; }

		if( inf == "object" ) continue;
		if( inf == "string" ) inf = "["+o[i].length+"]";
		if( inf == "number" ) inf = "="+o[i];
		if( inf == "boolean" ) inf = "="+o[i];
		if( inf == "function" ) inf = "()";
		if( inf == "undefined") 
		{
			if( showNull )	r += " <i>"+i + "</i>";
		}
		else
			r += " <a href='#' onclick='return sPathAdd(this);'>"+i +"</a>"+ inf+" ";
	  }
	  }catch(e){ r+= "This object does not support propery iteration"; }
	  r+="}";
	}
	else
	{
	  if( to == "string" || to=="function" ) 
		r += "<pre>"+ totext(String(o)) +"</pre>";
	  else if( to == "number" ) r += o;
	  else if( to == "boolean" ) r += o;
	  else
			r += "<pre>"+o +"</pre>";
	}


	return r;
}

function setObj(o,v)
{
	var c = getCurrObj();
	if( typeof c == "object" )
	{
		if( o=="" ) return;
		try{
			c[o] = v;
		}catch(e){ alert(e +" This object/property is not settable"); }
		return
	}

	if( o!="" && o!=currpath[currpath.length-1] )
	{
	  var ask = confirm("This is not an object, and different name is specified. Ignore name?");
	  if( !ask ) return;
	}

	eval(getPath(currpath.length-1))[currpath[currpath.length-1]] = v;
}

function execText(o)
{
	var c = getCurrObj();
	c.eval = eval;
	c.eval(c[o]);
}

