/////////////////////////////////////////////////////
//	NDiagnosticsStaticClass constants
//
var MemberFilter_All = 0;
var MemberFilter_Properties = 1;
var MemberFilter_Methods = 2;

/////////////////////////////////////////////////////
//	NDiagnosticsStaticClass object
//
function NDiagnosticsStaticClass()
{
	//	Operations
	NDiagnosticsStaticClass.prototype.Trace = function(text)
	{
		if(this.creatingWindow)
			return;
		if(this.debugWindow == null || this.debugWindow.closed != false)
		{
			this.creatingWindow = true;
			this.debugWindow = this.createDebugWindow("Trace");
			this.debugWindow.document.write('<h4>Trace</h4>');
			this.creatingWindow = false;
		}
		this.debugWindow.document.write(String(text) + "<br />");
	}
	
	NDiagnosticsStaticClass.prototype.ListMembers = function(title, obj, memberFilter)
	{
		try
		{
			if(new String(memberFilter) == "undefined")
				memberFilter = MemberFilter_All;
			
			if(String(typeof(obj)).toLowerCase() == String(typeof("")).toLowerCase())
				obj = eval(obj);
			
			var previewWindow = this.createDebugWindow(title);

			previewWindow.document.write("<a name='top'></a>");
			previewWindow.document.write('<h4>' + title + " - " + obj + '</h4>');
			
			var members = new Array();
			
			var prop;
			for(prop in obj)
			{
				try
				{
					var regex = /</gi;
					var propertyValue = obj[prop];
					var propertyStringValue = new String(propertyValue).replace(regex,"&lt;");
					if(propertyValue == null)
						propertyValue = "null";
					var propertyType = typeof(propertyValue);
					
					if(	memberFilter == MemberFilter_Properties && 
						propertyType.toLowerCase() == "function")
						continue;
						
					var prefix = prop.slice(0, 2).toLowerCase();
		
					if(	memberFilter == MemberFilter_Methods &&
						propertyType.toLowerCase() != "function")
						{
							if(propertyType.toLowerCase() != "string")
								continue;
							if(prefix != "on")
								continue;
						}
					
					var member = new Object();
					member.name = new String(prop).toString();
		
					if(prefix == "on")
						member.type = "delegate";
					else
						member.type = new String(propertyType).toString();
					member.value = propertyStringValue.toString();
					members.push(member);
				}
				catch(ex)
				{
					previewWindow.document.write(prop + " caused exception " + ex.message + "<p />");
				}
			}
		
			members.sort(this.membersArrayComparer);
			previewWindow.document.write("<table border='1' cellspacing='0' cellpadding='5' width='100%'>");
			var currentType = "";
			var typeCounter = 0;
			for(var i = 0; i < members.length; i ++)
			{
				var member = members[i];
				if(currentType == member.type)
					continue;
				
				var bgColor = (typeCounter%2 == 0)?"#ffffff":"#ffffee";
	
				currentType = member.type;
				previewWindow.document.write("<tr>");
				previewWindow.document.write("	<td width='100%' style='background:" + bgColor + ";'>");
				previewWindow.document.write(		"<a href='#" + currentType + "'>" + currentType + "</a>");
				previewWindow.document.write("	</td>");
				previewWindow.document.write("</tr>");
				typeCounter ++;
			}
			previewWindow.document.write("</table><p>&nbsp;</p>");
	
			previewWindow.document.write("<table border='1' cellspacing='0' cellpadding='5' width='100%'>");
			for(var i = 0; i < members.length; i ++)
			{
				var member = members[i];
				if(currentType != member.type)
				{
					currentType = member.type;
					previewWindow.document.write("<tr>");
					previewWindow.document.write("	<td colspan='3' style='background:#ccddee;'>");
					previewWindow.document.write("		<a name='" + currentType + "'></a><h3>" + currentType + " | <a href='#top'>go top</a></h3>");
					previewWindow.document.write("	</td>");
					previewWindow.document.write("</tr>");
				}
				
				var bgColor = (i%2 == 0)?"#ffffff":"#eeeeff";
				
				var clickHandlerTitle = "\"" + title + "." + member.name + "\"";
				var clickHandlerObject = "window.document.selectedObject." + member.name;
				var clickHandlerFilter = memberFilter;
				var clickHandlerText = "NDiagnostics.ListMembers(" + clickHandlerTitle + ", " + clickHandlerObject + ", " + clickHandlerFilter + ")";
				
				previewWindow.document.write("<tr>");
				previewWindow.document.write("	<td width='15%' style='background:" + bgColor + ";'>");
				previewWindow.document.write(		currentType);
				previewWindow.document.write("	</td>");
				previewWindow.document.write("	<td width='15%' style='background:" + bgColor + ";'>");
				previewWindow.document.write("		<a href='javascript:var i = 0;' onClick='" + clickHandlerText + "'>" + member.name + "</a>");
				previewWindow.document.write("	</td>");
				previewWindow.document.write("	<td width='70%' style='background:" + bgColor + ";'>&nbsp;");
				previewWindow.document.write(		member.value);
				previewWindow.document.write("	</td>");
				previewWindow.document.write("</tr>");
			}
			
			previewWindow.document.write("</table>");
			
			this.finilizeDebugWindow(previewWindow);
			previewWindow.document.selectedObject = obj;
		}
		catch(ex)
		{
		}
	}
	
	NDiagnosticsStaticClass.prototype.AlertProperties = function(ob)
	{
		if((new String(typeof(ob))).toLowerCase() == "string")
		{
			alert("string: " + ob);
			return;
		}
		
		var output = new Array();
		var linesCount = 0;
		var currentBlock = new String();
		for(prop in ob)
		{
			if(ob[prop] == null || String(ob[prop]) == "" || String(ob[prop]).indexOf(["native code"]) != -1 || prop == "innerHTML"  || prop == "outerHTML")
				continue;
			
			if(linesCount > 25)
			{
				output[output.length] = currentBlock;
				currentBlock = new String();
				linesCount = 0;
			}
			currentBlock += prop + " =" + ob[prop] + "\n";
			linesCount ++;
		}
		output[output.length] = currentBlock;
		
		for(block in output)
			alert(output[block]);
	}

	NDiagnosticsStaticClass.prototype.RemoveOffsetDivs = function()
	{
		for(var i = 0; i < 100; i++)
		{
			var div1Id = "zDiv" + i;
			var div1 = document.getElementById(div1Id);
			if(!NReflection.IsInstance(div1))
				break;
			var div2Id = "xDiv" + i;
			var div2 = document.getElementById(div2Id);

			document.body.removeChild(div1);
			document.body.removeChild(div2);
		}
	}

	NDiagnosticsStaticClass.prototype.CreateOffsetDiv = function(number, color, length, coordinate, horizontal)
	{
		var div = document.createElement("div");
		div.id = "zDiv" + number;
		div.style.position = "absolute";
		if(!horizontal)
		{
			div.style.top = NBrowserCaps.ToPixelLength(0);
			div.style.left = NBrowserCaps.ToPixelLength(30 * number);
			div.style.width = NBrowserCaps.ToPixelLength(30);
			div.style.height = NBrowserCaps.ToPixelLength(length);
		}
		else
		{
			div.style.top = NBrowserCaps.ToPixelLength(30 * number);
			div.style.left = NBrowserCaps.ToPixelLength(0);
			div.style.width = NBrowserCaps.ToPixelLength(length);
			div.style.height = NBrowserCaps.ToPixelLength(30);
		}
		div.style.background = color;
		document.body.appendChild(div);

		div = document.createElement("div");
		div.id = "xDiv" + number;
		div.style.position = "absolute";
		if(!horizontal)
		{
			div.style.top = NBrowserCaps.ToPixelLength(coordinate);
			div.style.left = NBrowserCaps.ToPixelLength(30 * number);
			div.style.width = NBrowserCaps.ToPixelLength(30);
			div.style.height = NBrowserCaps.ToPixelLength(30);
		}
		else
		{
			div.style.top = NBrowserCaps.ToPixelLength(30 * number);
			div.style.left = NBrowserCaps.ToPixelLength(coordinate);
			div.style.width = NBrowserCaps.ToPixelLength(30);
			div.style.height = NBrowserCaps.ToPixelLength(30);
		}
		div.style.background = color;
		div.style.border = "2px solid black";
		document.body.appendChild(div);
	}

    NDiagnosticsStaticClass.prototype.DisplayTraceElement = function(anchorElement)
    {
		if(this.traceElement == null)
		{
			this.traceElement = this.CreateTraceElement(anchorElement);
			return;
		}
		
		var elementOffset = NBrowserCaps.GetOffset(anchorElement);
		this.traceElement.style.left = NBrowserCaps.ToPixelLength(elementOffset.offsetLeft);
		this.traceElement.style.top = NBrowserCaps.ToPixelLength(elementOffset.offsetTop);
		this.traceElement.style.width = NBrowserCaps.ToPixelLength(anchorElement.offsetWidth);
		this.traceElement.style.height = NBrowserCaps.ToPixelLength(anchorElement.offsetHeight);
    }
    
    NDiagnosticsStaticClass.prototype.CreateTraceElement = function(anchorElement)
    {
		var elementOffset = NBrowserCaps.GetOffset(anchorElement);
		
		var div11 = document.createElement("div");
		div11.style.position = "absolute";
		div11.style.left = NBrowserCaps.ToPixelLength(elementOffset.offsetLeft);
		div11.style.top = NBrowserCaps.ToPixelLength(elementOffset.offsetTop);
		div11.style.width = NBrowserCaps.ToPixelLength(anchorElement.offsetWidth);
		div11.style.height = NBrowserCaps.ToPixelLength(anchorElement.offsetHeight);
		div11.style.border = "solid 3px red";
		document.body.appendChild(div11);
		return div11;
    }
			    
	//	Tools
	NDiagnosticsStaticClass.prototype.membersArrayComparer = function(left, right)
	{
		if(left.type != right.type)
			return (left.type < right.type)?-1:1;
		
		if(left.name != right.name)
			return (left.name < right.name)?-1:1;
			
		return 0;
	}

	NDiagnosticsStaticClass.prototype.createDebugWindow = function(title)
	{
		var previewWindow = window.open('','_blank','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,copyhistory=0,top=0,left=0,width=1000,height=720');
		
		previewWindow.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
		previewWindow.document.write('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>');

		previewWindow.document.write('<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=Utf-8" />');
		previewWindow.document.write('<meta http-equiv="Content-Language" content="en" /> ');
		previewWindow.document.write('<meta http-equiv="Content-Style-Type" content="text/css" />');
		previewWindow.document.write('<meta http-equiv="Content-Script-Type" content="javascript1.2" />');
	
		previewWindow.document.write("<script type='text/javascript' src='javascript/NDiagnostics.js'></script>");

		previewWindow.document.write("<title>" + title + "</title>");

		previewWindow.document.write("</head><body>");
		
		return previewWindow;
	}

	NDiagnosticsStaticClass.prototype.finilizeDebugWindow = function(previewWindow)
	{
		previewWindow.document.write("<h4>done</h4>");
	
		previewWindow.document.write("</body></html>");
		previewWindow.document.close();
	}
	
	//	fields
	this.length += 4;
	this.debugWindow = null;
	this.creatingWindow = false;
	this.traceElement = null;
}
//	Static Instance
var NDiagnostics = new NDiagnosticsStaticClass();
