﻿function ManageContentForm (varName, ajaxFunction, viewNodeUrl, currentListKey) 
{
	this.VariableName = varName;
	this.AjaxFunction = ajaxFunction;
	this.ViewNodeUrl = viewNodeUrl;
	this._currentNode = null;
	this._currentListKey = currentListKey;
	
	this.AddChildTreeNodes = function(node)
	{
	    this._currentNode = node;
	    this._currentNode._originalHtml = this._currentNode.Html;
	    this._currentNode.Html = "(Loading...)";
	    this._currentNode.Refresh();
        this.AjaxFunction("GetSubNodes", node.Value, new Function('result', 'window.' + this.VariableName + '.AddSubNodesComplete(result);'));
	}
	
	this.AddSubNodesComplete = function(result)
	{
	    var node = this._currentNode;
	    if (result)
		{
		    if (node)
		    {
		        var nodes = eval(result);
		        for (var i = 0; i < nodes.length; i++)
		        {
		            node.AddNode(new Telligent_TreeNode(nodes[i][1], nodes[i][0]));
		        }
		        
		        node.Html = node._originalHtml;
		        node.Refresh();
		        node.SetExpanded(true);
		    }
		}
		else
		{
			alert('An error occured while loading sub-nodes for the selected tree node');
			if (node)
			{
			    node.Html = node._originalHtml;
		        node.Refresh();
			}
		}
	}
	
	this.TreeNodeClick = function(node)
	{
	    if (this._currentListKey != node.Value)
	        window.location = this.ViewNodeUrl.replace('{0}', node.Value); // Do not URI encode the node value, since it is not inserted into the path.
	}
}


if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();