DNow.Help = Class.create( aw.ui.Panel.Splitter, {

    activeTopicKey: null,

    _topicTree: null,

    _onHelpContentLoaded_fx: null,

    initialize: function ( properties, events ) {
        this.activeTopicTitle = null;
        this._topicTree = null;
        this._onHelpContentLoaded_fx = null;

        this.inheritCSS = true;

        Object.extend( properties || {}, {
            'title': 'Help',
            'moveable': true,
            'resizeable': true,
            'closeable': true,
            'orientation': 'horizontal',
            'height': 400,
            'width': 500,
            'container': $(document.getElementsByTagName('body')[0]),
            'position': {
                'top': '100px',
                'left': '200px'
            }
        });

        aw.ui.Panel.Splitter.prototype.initialize.apply( this, [properties, events] );

        if (this.componentBus) {
            this.componentBus.getComponentReferences( 'aw.HelpManager', function ( components ) {
                if ((components) && (components.length > 0)) {
                    var hm = components[0];

                    this._onHelpContentLoaded_fx = this._onHelpContentLoaded.bind( this );
                    hm.registerEvent( 'helpcontentloaded', this._onHelpContentLoaded_fx );

                    if ((hm.helpContent) && (hm.helpContent.length > 0))
                        this._onHelpContentLoaded.bind( hm.helpContent );
                    else
                        hm.loadHelpFile( './custom/js/HelpContent.js', '_DNowHelp' );
                }
            }.bind( this ) );
        }
    },

    close: function ( ) {
        aw.ui.Panel.Splitter.prototype.close.apply( this );
        this.destroy( );
    },

    destroy: function ( ) {
        if (this.componentBus) {
            if (this._onHelpContentLoaded_fx) {
                this.componentBus.unregisterEvent( 'helpcontentloaded', this._onHelpContentLoaded_fx );
                this._onHelpContentLoaded_fx = null;
            }
        }

        aw.ui.Panel.Splitter.prototype.destroy.apply( this );
    },

    draw: function ( ) {
        aw.ui.Panel.Splitter.prototype.draw.apply( this );

        this._topicTree = new aw.ui.Tree({
            'mode': 'single',
            'nodes': this._buildTopicNodes( )
        },{
            'onselectionchanged': this._onTopicSelectionChanged.bind( this )
        });

        this._contentPanel = new aw.ui.Panel({
            'width': 300,
            'minWidth': 250,
            'titleVisibility': false,
            'cssClass': 'DNow_Help_Content'
        });

        this.add([
            new aw.ui.Panel({
                'title': 'Table of Contents',
                'width': 200,
                'minWidth': 150,
                'items': [
                    this._topicTree
                ]
            }),
            this._contentPanel
        ]);

        return this.canvas;
    },

    loadTopicByTitle: function ( title ) {
        if (this._topicTree) {
            this._topicTree.clearSelection( );
            this._topicTree.selectNodeByKey( title );
        } else {
            this._loadTopicByTitle( title );
        }
    },

    _buildTopicNodes: function ( topics, parent ) {
        if (((!topics) || (!topics.length > 0)) && (!parent)) {
            var hm = this.componentBus.getComponentReferences( 'aw.HelpManager' );
            if ((hm) && (hm.length > 0))
                topics = hm[0].helpContent;
        }

        var nodes = [], node = null, key = null;
        if ((topics) && (topics.length > 0)) {
            for (var i=0; i < topics.length; i++) {
                key = ((parent) ? (parent.title + ':') : '') + topics[i].title;
                node = {
                    'key': key,
                    'label': topics[i].title,
                    'selectable': true,
                    'type': 'leaf',
                    'selected': ((this.activeTopicKey) && (key == this.activeTopicKey)) ? true : false
                };

                if ((topics[i].topics) && (topics[i].topics.length > 0)) {
                    Object.extend( node, {
                        'type': 'branch',
                        'children': this._buildTopicNodes( topics[i].topics, topics[i] )
                    });
                }

                nodes.push( node );
            }
        }

        return nodes;
    },

    _loadTopicByTitle: function ( title ) {
        if ((!title) || (title === ''))
            return;

        var hm = this.componentBus.getComponentReferences( 'aw.HelpManager' );
        if ((!hm) || (!hm.length > 0))
            return;

        var topic = hm[0].getTopicByTitle( title );
        if (!topic)
            return;

        this.setTitle( 'Help: ' + topic.title );
        this._contentPanel.update( new aw.ui.HTML({'html': topic.content }) );

        this.activeTopicKey = title;
    },

    _onHelpContentLoaded: function ( content ) {
        if ((!this._topicTree) || (!content) || (!content.length > 0))
            return;

        this._topicTree.clear( );
        this._topicTree.addNodes( this._buildTopicNodes( content ) );
        this._onTopicSelectionChanged( this._topicTree );
    },

    _onTopicSelectionChanged: function ( tree ) {
        var topic = tree.getSelectedKeys( );
        if ((topic) && (topic.length > 0)) {
            this._topicTree.expandParentNodesByKey( topic[0] );
            this._loadTopicByTitle( topic[0] );
        }
    },

    VALID_PROPERTIES: ['title','componentBus','container','cssClass','closeable','titleVisibility','height','width','moveable','resizeable','minHeight','minWidth','orientation','disabled','position','activeTopicKey'],

    CLASS_NAME: 'DNow.Help'
});
