<?xml version="1.0" encoding="UTF-8"?>
<scopeviewer>
<![CDATA[

/* SCOPE IT */

	var aScoped = [];
	var aScopedMap = [];
	var aScopedHash = [];
	var iDepth = 0;
	var iCurrentDepth = 0;
	var aDepth = [];
	var iScopeMapCount = 0;
	var scope_to_text = 0;
	var iDrawOffsetTop = 50;
	var colors = ["#A0A0A0","#FFAAAA","#AAFFAA","#AAAAFF","#FFCCCC","#CCFFCC","#CCCCFF","#FFEEEE","#EEFFEE","#FFEEEE","#EFEFEF"];
	var color_len = colors.length;
	function NextColor(i){
		if(i > (color_len - 1)){
			var d = i / color_len;
			var c = parseInt((d - parseInt(d)) * color_len);
			return colors[c];
		}
		else{
			return colors[i];
		}
	}
	
	function ScopeText(){
		aScoped = [];
		aScopedMap = [];
		aScopedHash = [];
		iScopeMapCount = 0;
		var raw_data = temp_text;
		
		if(raw_data.length == 0){
			alert('invalid data');
			return;
		}		
		var a = raw_data.split("~");

		//alert(a.length);
		//var a = demo_data.split("~");
		var a_len = a.length;
		for(var i = 0; i < a_len; i++){
			var v;
			aScoped[i] = eval("v = " + a[i]);
		}
		DrawScope();	
	}
	function MoveFirst(){
		iCurrentDepth = 0;
		ShowDepths();
	}
	function MovePrev(){
		iCurrentDepth--;
		if(iCurrentDepth < 0) iCurrentDepth = 0;
		ShowDepths();
	}
	function MoveNext(){
		iCurrentDepth++;
		if(iCurrentDepth > iDepth) iCurrentDepth = iDepth;
		ShowDepths();
	}
	function MoveLast(){
		iCurrentDepth = iDepth;
		ShowDepths();
	}
	function MoveTo(){
		iCurrentDepth = parseInt(document.getElementById("oDepthLevel").value);
		if(iCurrentDepth < 0) iCurrentDepth = 0;
		else if(iCurrentDepth > iDepth) iCurrentDepth = iDepth;
		ShowDepths();
	}
	function FindClass(){
		var sName = document.getElementById("oDepthLevel").value;
		iCurrentDepth = 0;
		ShowDepths("c",sName);
	}

	function FindId(){
		var sId = document.getElementById("oDepthLevel").value;
		iCurrentDepth = 0;
		ShowDepths("i",sId);
	}
	
	function ShowDepths(n,v){
		var l = aDepth.length;
		document.getElementById("oDepthLevel").value = iCurrentDepth;
		var b = 0;
		if(n && v) b = 1;
		for(var i = 0; i < l; i++){
			var a = aDepth[i];
			if(typeof a != "object") continue;
			for(var t = 0; t < a.length; t++){
				var o = a[t];
				var d = parseInt(o.getAttribute("scope-index"));
				var p = aScoped[d];
				if(i <= iCurrentDepth || (b && p[n]==v)){
					a[t].style.display = "block";
				}
				else{
					a[t].style.display = "none";
				}
			}
		}
	}



	function DrawControls(){
		var oP = document.createElement("div");
		oP.style.cssText = "border: 1px solid #000000;background-color: #CFCFCF;";
		var oI = document.createElement("input");
		oI.setAttribute("type","button");
		oI.setAttribute("value","<<");
		oI.onclick = MoveFirst;
		oP.appendChild(oI);

		oI = document.createElement("input");
		oI.setAttribute("type","button");
		oI.setAttribute("value","<");
		oI.onclick = MovePrev;
		oP.appendChild(oI);
		
		oI = document.createElement("input");
		oI.setAttribute("type","text");
		oI.setAttribute("id","oDepthLevel");
		oI.style.cssText = "width: 50px;padding-left: 3px;border:1px solid #000000;";
		oP.appendChild(oI);
		
		oI = document.createElement("input");
		oI.setAttribute("type","button");
		oI.setAttribute("value","!");
		oI.onclick = MoveTo;
		oP.appendChild(oI);

		oI = document.createElement("input");
		oI.setAttribute("type","button");
		oI.setAttribute("value","Id");
		oI.onclick = FindId;
		oP.appendChild(oI);

		oI = document.createElement("input");
		oI.setAttribute("type","button");
		oI.setAttribute("value","Class");
		oI.onclick = FindClass;
		oP.appendChild(oI);

		oI = document.createElement("input");
		oI.setAttribute("type","button");
		oI.setAttribute("value",">");
		oI.onclick = MoveNext;
		oP.appendChild(oI);
		
		oI = document.createElement("input");
		oI.setAttribute("type","button");
		oI.setAttribute("value",">>");
		oI.onclick = MoveLast;
		oP.appendChild(oI);
		
		document.body.appendChild(oP);
	}
	function DrawScope(){
		document.body.innerHTML = "";
		aScopedMap = [];
		aScopedHash = [];
		iDepth = 0;
		aDepth = [];
		iScopeMapCount = 0;
		DrawControls();
		
		/* fix up the hash */
		for(var i = 0; i < aScoped.length; i++){
			var o = aScoped[i];

			var n = o.n;
			
			if(typeof n == "string"){
				var ind = iScopeMapCount++;
				aScopedHash[ind] = n;
			}
			else{
				if(typeof aScopedHash[n] == "string"){
					n = aScopedHash[n];
				}
				else{
					alert('woops: ' + n + " from " + aScopedHash.length);
				}
			}

			var p = document.createElement("div");
			document.body.appendChild(p);
			p.style.position = "absolute";
			if(o.d == 0) p.style.display = "none";
			if(o.w <= 0 || o.h <= 0) continue;
			p.style.zIndex = o.p;
			p.style.left = o.l + "px";
			p.style.width = o.w + "px";
			p.style.top = (iDrawOffsetTop + o.t) + "px";
			p.style.height = o.h + "px";
			p.style.border = "1px solid #000000";
			p.style.font = "8pt Arial";
			
			p.setAttribute("scope-index",i);
			if(o.p > iDepth) iDepth = o.p;
			if(typeof aDepth[o.p] != "object" || aDepth[o.p] == null) aDepth[o.p] = [];
			var aD = aDepth[o.p];
			aD[aD.length] = p;
			
			var node_title =  "Name=" + n + " / ID=" + o.i + " / CLASS=" + o.c;
			p.setAttribute("title",node_title);

		
			p.appendChild(document.createTextNode((o.y ? o.y : n) + (o.i ? " " + o.i : "")));
			//if(o.p > (colors.length - 1)) document.title = o.p;
			p.style.backgroundColor = NextColor(o.p);//"#EFEFEF";
		}
		iCurrentDepth = iDepth;
		document.getElementById("oDepthLevel").value = iCurrentDepth;
	}

]]>
</scopeviewer>
