nmdgf.widgets.Tree = function(config) {
	this.container = config.container;
	this.name = config.name;
	this.id = config.id;
	nmdgf.registerWidget(this);
	this.viewURL = config.viewURL;
	this.addURL = config.addURL;
	this.deleteURL = config.deleteURL;
	this.moveUpURL = config.moveUpURL;
	this.moveDownURL = config.moveDownURL;
};

nmdgf.widgets.Tree.prototype = {
	
	id : null,
	
	name : null,
	
	treeview : null,
	
	container : null,
	
	data : null,
	
	viewURL : null,
	
	addURL : null,
	
	deleteURL : null,
	
	moveUpURL : null,
	
	moveDownURL : null,
	
	initialize : function() {
		this.treeview = new YAHOO.widget.DialogTreeView(this.container, {
			label: this.name,
			type: YAHOO.widget.DialogTreeNode,
			expanded: true
		});
		
		var root = this.treeview.getNodeByIndex(1);
		
		for(var idx in this.data) {
			this.addNode(this.data[idx], root);
		}
		
		this.treeview.render();
		
		this.treeview.subscribe('addClicked', this.addClicked, this, true);
		this.treeview.subscribe('deleteClicked', this.deleteClicked, this, true);
		this.treeview.subscribe('upClicked', this.upClicked, this, true);
		this.treeview.subscribe('downClicked', this.downClicked, this, true);
		this.treeview.subscribe('dialogSuccess', this.dialogSuccess, this, true);
	},
	
	addClicked : function(node) {
		var newNode = new YAHOO.widget.DialogTreeNode({
            type: YAHOO.widget.DialogTreeNode,
        	label: "New Record",
            expanded: true,
            dialogURL:this.addURL+'/checklist.id/'+this.id+'/parentItem.id/'+this.treeview.dialogNode.recordId,
        });
        newNode.appendTo(node);
        node.refresh();
        newNode.focus();
        this.treeview.nodeFocusChanged({node:newNode});
	},

	deleteClicked : function(node) {
		if(confirm("Are you sure you want to delete this item and all of it's children?")) {
			YAHOO.util.Connect.asyncRequest('GET', this.deleteURL+'/item.id/'+this.treeview.dialogNode.recordId, {
				success: function(r) {
					var result = nmdgf.parseJSON(r.responseText);
					if(result.success == true) {
						var parent = this.treeview.dialogNode.parent;
						this.treeview.dialog.hide();
						this.treeview.removeNode(this.treeview.dialogNode, true);
					}else {
						alert('There was a problem deleting this item');
					}
				}, scope: this
			});
		}
	},
	
	upClicked : function(node) {
		var previousSibling = node.previousSibling;
		if(previousSibling != null) {
			YAHOO.util.Connect.asyncRequest('GET', this.moveUpURL+'/item.id/'+this.treeview.dialogNode.recordId, {
				success: function(r) {
					var result = nmdgf.parseJSON(r.responseText);
					if(result.success == true) {
						this.treeview.dialog.hide();
						this.treeview.popNode(node);
						node.insertBefore(previousSibling);
						previousSibling.parent.refresh();
						node.selected();
					}else {
						alert('There was a problem moving this item');
					}
				}, scope: this
			});
		}
	},

	downClicked : function(node) {
		var nextSibling = node.nextSibling;
		if(nextSibling != null) {
			YAHOO.util.Connect.asyncRequest('GET', this.moveDownURL+'/item.id/'+this.treeview.dialogNode.recordId, {
				success: function(r) {
					var result = nmdgf.parseJSON(r.responseText);
					if(result.success == true) {
						this.treeview.dialog.hide();
						this.treeview.popNode(node);
						node.insertAfter(nextSibling);
						nextSibling.parent.refresh();
						node.selected();
					}else {
						alert('There was a problem moving this item');
					}
				}, scope: this
			});
		}
	},
	
	dialogSuccess : function(node) {
		var name = nmdgf.byId('item.name').value;
		this.treeview.dialogNode.label = name;
		var nodeId = nmdgf.byId('item.id').value;
		node.recordId = nodeId;
		node.dialogURL = this.viewURL;
		this.treeview.dialogNode.refresh();
	},
	
	addNode : function(nodeConfig, root) {
		var node = new YAHOO.widget.DialogTreeNode({
			label: nodeConfig.label,
			expanded: false,
			title: nodeConfig.title,
			dialogURL: nodeConfig.dialogURL,
			recordId: nodeConfig.recordId
		}, root);
		
		if(nodeConfig.children !== undefined) {
			for(var idx in nodeConfig.children) {
				this.addNode(nodeConfig.children[idx], node);
			}
		}
	},
	
	updateNodeLabel: function(newValue, oldValue, node) {
		alert(newValue);
	},
	
	getHTML: function(label) {
		var sb = [];
		sb[sb.length] = '<span>';
		sb[sb.length] = label;
		sb[sb.length] = '</span>';
		
		sb[sb.length] = '<span class="node-links" >';
	    sb[sb.length] = '<img src="/static/images/icons/information.png" class="node-info" />';
	    sb[sb.length] = '</span>';
		
		return sb.join('');
	}
		
};

nmdgf.widgets.TreeNode = function(config) {
	this.id = config.id;
	this.label = config.label;
	this.parent = config.parent;
	
	this.node = new YAHOO.widget.TextNode({
	    label: this.label,
	    expanded: true
	}, this.parent);
};
nmdgf.widgets.TreeNode.prototype = {
	
	id : null,
	
	label : null,
	
	children : null,
	
	parent : null,
	
	node : null
	
};
