// JAM window Based Debugging tools

function JAM_D_AnalyzeVar(Var, Target, VarLabel)
{
	// the drill down event handler
	function DrillDown(e)
	{
		var targ = JAM_EventTarget(e);
		
		var Property = targ.id;
		temp = new JAM_D_AnalyzeDialog(Var[Property], VarLabel + " -> " + Property);
	}
	
	// create document fragment to build this list in
	var Frag = document.createDocumentFragment();
	
	// get the type of the Variable
	var Type = typeof(Var);
	Frag.appendChild( document.createTextNode("Variable Value is \"" + Var + "\"<br />") );
	
	if (Type == "object")
		{
		var PropertyList = new Array();
		
		for (var Property in Var)
			{
			PropertyList[PropertyList.length] = Property;
			}
			
		PropertyList.sort();
		
		for (i=0; i<PropertyList.length; ++i)
			{
			// create a div for this row
			var RowDiv = document.createElement("div");
			Frag.appendChild(RowDiv);
			var Output = "";
				
			Property = PropertyList[i];
				
			try	{
				var Prop = Var[Property];
				var SProp = ""; SProp += Prop;
				var PType = typeof(Prop);
				
				if (PType == "object")
					{
					//OnClick = "Drilldown(Property);";
					ObjectPrototype = "" + Prop;
					ObjectPrototype = ObjectPrototype.substr(ObjectPrototype.indexOf(" "), -1);
					RowDiv.innerHTML = Property + " (object) " + Prop;
					RowDiv.style.color = "blue";
					if (Prop != null)
						{
						RowDiv.id = Property;
						RowDiv.style.cursor = "pointer";
						bindEvent(RowDiv, "click", DrillDown);
						RowDiv.style.textDecoration = "underline";
						}
					}
				else if (PType == "function")
					{
					Text = SProp.substr(0, SProp.indexOf("{"));
					FnName = Text.substr(9, Text.length - 12);
					RowDiv.innerHTML = FnName + " (function)";
					RowDiv.style.color = "green";
					}
				else
					{
					RowDiv.innerHTML = Property + " (" + PType + ") [" + Prop + "]";
					RowDiv.style.color = "red";
					}
				}
			catch (e)
				{
				RowDiv.innerHTML = Property + " (failed to get details)<br />";
				}
			}
		}
		
	Target.appendChild(Frag);
}

//----------------------------------------------------------------------
// JAM Debug Analyse dialog
//----------------------------------------------------------------------

function JAM_D_AnalyzeDialog(Var, VarLabel, zIndex)
{
	if (typeof(zIndex) != "number")
		zIndex = 65000;
	
	var self = this;
	
	this.Window = new JAM_Window("", 600, 500, "center", false, true, zIndex);
	
	// add background to debug window
	this.Window.Window.style.background = "url(inc/debug_bg.jpg)";
	this.Window.Window.style.backgroundColor = "white";
	this.Window.Window.style.backgroundRepeat = "no-repeat";
	this.Window.Window.style.backgroundAlign = "top left";
	
	// create a div for the variable label
	this.LabelDiv = document.createElement("div");
	this.LabelDiv.style.fontSize = "small";
	this.LabelDiv.style.border = "1px black solid";
	this.LabelDiv.style.overflow = "hidden";
	PlaceElement(this.LabelDiv, 100, 25, 300, 20, 31);
	this.Window.Window.appendChild(this.LabelDiv);
	
	// create update btn
	this.UpdateBtn = new JAM_Button("Update");
	PlaceElement(this.UpdateBtn.Element, 420, 25, 80, 20, 31);
	this.UpdateBtn.OnClick = function()
	{
		self.ListDiv.innerHTML = "";
		JAM_D_AnalyzeVar(Var, self.ListDiv);
	}
	this.Window.Window.appendChild(this.UpdateBtn.Element);
	
	if (VarLabel)
		this.LabelDiv.innerHTML = VarLabel + " (" + typeof(Var) + ") " + Var;
	
	// create a scrolling div for the analisis list
	this.ListDiv = document.createElement("div");
	this.ListDiv.style.textAlign ="left";
	this.ListDiv.style.fontSize = "small";
	this.ListDiv.style.overflow = "auto";
	this.ListDiv.style.border = "1px black solid";
	PlaceElement(this.ListDiv, 100, 50, 400, 400, 31);
	this.ListDiv.appendChild( document.createTextNode(this.Message) );
	this.Window.Window.appendChild(this.ListDiv);
	
	JAM_D_AnalyzeVar(Var, this.ListDiv, VarLabel);

	// Add OK button
	this.OkBtn = new JAM_Button("OK");
	PlaceElement(this.OkBtn.Element, 260, 465, 80, 20, 31);
	this.OkBtn.OnClick = function()
	{	self.Window.Remove();	};
	this.Window.Window.appendChild(this.OkBtn.Element);
}