(function () {
	
    var Y = YAHOO, Dom = Y.util.Dom, Widget = Y.widget;

	Widget.DialogTreeView = function(id, oConfig) {
		Widget.DialogTreeView.superclass.constructor.call(this, id, oConfig);
	};
	
	Y.lang.extend(Widget.DialogTreeView, Widget.TreeView, {
		
		dialog: null,
		
		dialogNode: null,
		
		DIALOG_CSS : 'dialog-treeview',
		
		init: function(id) {
			Widget.DialogTreeView.superclass.init.call(this, id);
			this.createEvent("addClicked", this);
			this.createEvent("deleteClicked", this);
			this.createEvent("upClicked", this);
			this.createEvent("downClicked", this);
			this.createEvent("dialogSuccess", this);
			
			this.subscribe('clickEvent', this.nodeFocusChanged);
			this.subscribe('collapse', this.nodeFocusChanged);
			this.subscribe('expand', this.nodeFocusChanged);
		},
		
		nodeFocusChanged : function(oArgs) {
			var newNode = oArgs.node ? oArgs.node : oArgs;
			
			if(newNode !== null && this.dialogNode !== newNode) {
				if(this.dialogNode !== null) {
					this.dialogNode.deselected();
				}
				if(this.dialog === null) {
					this.generateDialog();
				}
				newNode.selected();
				this.dialogNode = newNode;
			}
		},
		
		generateDialog : function() {
			var that = this;
			
			var buttons = [];
			buttons.push( { text:'Update', handler:function() { that.handleSubmit(); } });
			
			this.dialog = new Widget.Dialog(this.id+'-dialog', {
				fixedcenter : false,
				constraintoviewport : false,
				underlay : 'none',
				close : false,
				visible : false,
				draggable : false,
				modal: false,
				effect : null,
				context : null,
				hideaftersubmit : false,
				buttons : buttons
			});
			this.dialog.setHeader('');
			this.dialog.setBody('&nbsp;');
			this.dialog.render(document.body);
			Dom.addClass(this.dialog.element, this.DIALOG_CSS);
			this.dialog.callback.success = function(r) {
				that.handleSuccess(r);
			}
			
			var upButton = new YAHOO.widget.Button({
                id: "dialogTreeUpButton", 
                type: "button",
                label: "Up",
                container: this.dialog.header 
            });
            YAHOO.util.Dom.addClass(YAHOO.util.Dom.getFirstChild(upButton), 'button-arrow-up');
            upButton.subscribe('click', this.upButtonClicked, this, true);
            var downButton = new YAHOO.widget.Button({
                id: "dialogTreeDownButton", 
                type: "button",
                label: "Down",
                container: this.dialog.header 
            });
            YAHOO.util.Dom.addClass(YAHOO.util.Dom.getFirstChild(downButton), 'button-arrow-down');
            downButton.subscribe('click', this.downButtonClicked, this, true);
			var addButton = new YAHOO.widget.Button({
                id: "dialogTreeAddButton", 
                type: "push", 
                label: "Add Child", 
                container: this.dialog.header 
            });
			YAHOO.util.Dom.addClass(YAHOO.util.Dom.getFirstChild(addButton), 'button-add');
            addButton.subscribe('click', this.addButtonClicked, this, true);
            var deleteButton = new YAHOO.widget.Button({
                id: "dialogTreeDeleteButton", 
                type: "push",
                label: "Delete",
                container: this.dialog.header 
            });
            YAHOO.util.Dom.addClass(YAHOO.util.Dom.getFirstChild(deleteButton), 'button-delete');
            deleteButton.subscribe('click', this.deleteButtonClicked, this, true);
		},
		
		handleSubmit : function() {
			this.dialog.submit();
		},
		
		handleSuccess : function(r) {
			this.dialog.setBody(r.responseText);
			this.dialog.focusFirst();
			this.fireEvent('dialogSuccess', this.dialogNode);
		},
		
		addButtonClicked : function() {
			this.fireEvent('addClicked', this.dialogNode);
		},
		
		deleteButtonClicked : function() {
			this.fireEvent('deleteClicked', this.dialogNode);
		},
		
		upButtonClicked : function() {
			this.fireEvent('upClicked', this.dialogNode);
		},
		
		downButtonClicked : function() {
			this.fireEvent('downClicked', this.dialogNode);
		}

	});

})();
