var divScrollerArgs = null;

function SetupTreeViews(suppressPostback)
{
	// override native Select method
	RadTreeNode.prototype.NSelect = RadTreeNode.prototype.Select;
	RadTreeNode.prototype.Select = function()
	{
		this.TreeView.UnSelectAllNodes();
		try { this.Expand(); } catch(e) { /*ignore*/}
		this.NodeElmt.className = "selectedNode";
		if( this.WrapElmt )
			this.WrapElmt.className="selectedNodeChildren";
		scheduleScrollTop(this);	
		this.NSelect();
	}
	
	// override native UnSelect method
	RadTreeNode.prototype.NUnSelect = RadTreeNode.prototype.UnSelect;
	RadTreeNode.prototype.UnSelect = function()
	{
		// if(this.Parent) this.Collapse();
		this.NodeElmt.className = '';
		if( this.WrapElmt )
			this.WrapElmt.className = '';
		this.NUnSelect();
	}

	var i, j;
	for ( i in treeViewIds)
	{
		var tv = treeViewIds[i];
		var nodes = tv.AllNodes;
		
		tv.SuppressPostback = suppressPostback;
		
		for (j=0; j<nodes.length; j++)
		{
			var node = nodes[j];
			
			node.NodeElmt = document.getElementById(node.id);
			node.WrapElmt = document.getElementById("G" + node.id);

			if (node.TextElmt)
			{
				node.TextElmt.onclick = function(e)
				{
					var p = this.parentElement;
					if(!p) p = this.parentNode;
					var nodeId = p.id;
					var treeId = nodeId.substring(0, nodeId.indexOf('_'));
					var tree = eval(treeId);
					var node = tree.FindNode(nodeId);
					node.Select();
					treeId = tree.UniqueId;
					treeId = treeId.replace(":","$");
					if (!e)	e = event;
					e.cancelBubble = true;
					
					if ( !suppressPostback ) { __doPostBack(treeId, nodeId) } ;
				};
			}
			if (node.Selected)
			{
				node.Select();
				scheduleScrollTop(node);
			}
		}
	}
}

function scheduleScrollTop(node)
{
	var parent = document.getElementById(node.TreeView.Container);
	var child = (node.WrapElmt) ? node.WrapElmt : node.NodeElmt;
	var offsetAdj = (node.WrapElmt) ? node.NodeElmt : null;
	var args = {parent:parent, child:child, offsetAdj:offsetAdj};
	
	divScrollerArgs = args;
	
	//need to wait for some other things to happen before we can execute this.
	window.setTimeout("scrollDiv();", 50);
}


function scrollDiv()
{
	var args = divScrollerArgs;
	if (!args) return;
	
	var p = args.parent;
	var c = args.child;
	var adj = 0;
	
	if (args.offsetAdj) adj = args.offsetAdj.offsetHeight;
	
	var pTop = getAbsoluteOffset(p);
	var cTop = getAbsoluteOffset(c) - adj;
	
	var pHeight = p.offsetHeight;
	var cHeight = c.offsetHeight + adj;

	var pBottom = pHeight + pTop;
	var cBottom = cHeight + cTop;
	
	if (cBottom > pBottom)
	{
		//allow for horizontal scrollbar if one exists
		p.scrollTop = 20 + (cBottom - pBottom);
		
		if (cHeight > pHeight)
			p.scrollTop = cTop - pTop;
	}
}



       var MAX_DUMP_DEPTH = 10;

      

       function dumpObj(obj, name, indent, depth) {

              if (depth > MAX_DUMP_DEPTH) {

                     return indent + name + ": <Maximum Depth Reached>\n";

              }

              if (typeof obj == "object") {

                     var child = null;

                     var output = indent + name + "\n";

                     indent += "\t";

                     for (var item in obj)

                     {

                           try {

                                  child = obj[item];

                           } catch (e) {

                                  child = "<Unable to Evaluate>";

                           }

                           if (typeof child == "object") {

                                  output += dumpObj(child, item, indent, depth + 1);

                           } else {

                                  output += indent + item + ": " + child + "\n";

                           }

                     }

                     return output;

              } else {

                     return obj;

              }

       }

      
