DNow.VariableDialog = Class.create( aw.ui.Dialog, {

    _variableTree: null,
    
    initialize: function ( properties, events ) {
        this.inheritCSS = true;

        this._variableTree = null;

        Object.extend( properties, {
            'title': 'Select Variable'
        });

        aw.ui.Dialog.prototype.initialize.apply( this, arguments );
    },

    draw: function ( ) {
        aw.ui.Dialog.prototype.draw.apply( this );

        var contents = this.canvas.down('div.contents');
        if (contents) {
            contents.update( this._local_templates['ui'].evaluate( { } ) );

            var tree = contents.down('div.variableTree');
            if (tree) {
                var gm = this.componentBus.getComponentReferences( 'aw.GeographyManager' );
                if ((gm) && (gm.length > 0)) {
                    gm = gm[0];

                    this._variableTree = new aw.ui.Tree({
                        'container': tree,
                        'componentBus': this.componentBus,
                        'branchSelectable': false,
                        'mode': 'single',
                        'baseRequest': {
                            'cn': 'SRC.Web.Client.Handler.AllocateHandler|' + gm.allocateDataset.variant,
                            'service': 'variableTree'
                        }
                    });
                }
            }
        }

        this._renderButton( 'OK', this._onSelectVariable.bind( this ) );
        this._renderButton( 'Cancel', this.close.bind( this ) );

        return this.canvas;
    },

    _onSelectVariable: function ( ) {
        if (!this._variableTree)
            return;

        var selectedNode = this._variableTree.getSelectedNodes( );
        if ((selectedNode) && (selectedNode.length === 1)) {
            selectedNode = selectedNode[0];

            var gm = this.componentBus.getComponentReferences( 'aw.GeographyManager' );
            if ((!gm) || (!gm.length > 0))
                return;

            gm = gm[0];

            var key = ((selectedNode.key) && (selectedNode.key.split('.').length > 1)) ? selectedNode.key.split('.').last( ) : selectedNode.key;
            var req = {
                'cn': 'SRC.Web.Client.Handler.AllocateHandler|' + gm.allocateDataset.variant,
                'service': 'getVariableInfo',
                'varKey': key
            };

            this.request( req, function ( classname, o ) {
                if ((!o) || (!o.payload) || (!o.payload.variables))
                    return;

                if (o.payload.variables.length === 1) {
                    this.triggerEvent( 'onvariableselected', o.payload.variables[0] );
                    this.close( );
                } else {
                    new aw.ui.Dialog.Error({'title':'Error', 'content': 'You must select exactly one variable.'});
                }
            }.bind( this ) );
        } else {
            new aw.ui.Dialog.Error({'title':'Error', 'content': 'You must select exactly one variable.'});
        }
    },

    _local_templates: {
        'ui': new Template(
            '<div class="variableTree"></div>' + 
            '<div class="variableHelp">Select a variable from the list at the left and click OK when done.</div>')
    },

    EVENTS: ['onvariableselected'],

    VALID_PROPERTIES: ['title','closeable','content','moveable','cssClass','componentBus'],

    CLASS_NAME: 'DNow.VariableDialog'
});
