﻿Ext.namespace('FinnKodeWeb');

FinnKodeWeb.PathItem = function(config) {
    config = config || {};
    // call superclass constructor
    FinnKodeWeb.PathItem.superclass.constructor.call(this, config);

    this.bookId = config.bookId;
    this.bookSectionId = config.bookSectionId;
    this.objectId = config.objectId;
    this.bookName = config.bookName;

}

Ext.extend(FinnKodeWeb.PathItem, Ext.menu.Item, {});

Ext.reg('pathitem', FinnKodeWeb.PathItem);



FinnKodeWeb.PathButton = function(config) {
    // call superclass constructor
    FinnKodeWeb.PathButton.superclass.constructor.call(this, config);

    var objectPathRecord = Ext.data.Record.create([
        { name: 'caption', mapping: '@caption' },
        { name: 'locateId', mapping: '@locateId' }
    ]);
    var xmlObjectPathReader = new Ext.data.XmlReader({
        record: 'pathItem',
        id: '@locateId'
    }, objectPathRecord);

    var httpProxyLoadObjectSiblings = new Ext.data.HttpProxy({
        url: 'FinnKodeWS/ContentService.svc/GetObjectSiblings',
        method: 'GET'
    });

    this.bookId = config.bookId;
    this.bookSectionId = config.bookSectionId;
    this.objectId = config.objectId;
    this.bookName = config.bookName;
    
    this.menuLoaded = false;
    if (!this.menu) {
        this.menu = new Ext.menu.Menu({
            items: [{
                text: 'Laster meny',
                icon: 'ExtJS/resources/images/default/grid/loading.gif'
            }]
        });
    }
    var callbackLoadObjectSiblings = function(result, args, success) {
        this.menu.removeAll(true);
        if (success) {
            var recordIndex = 0;
            for (recordIndex = 0; recordIndex < result.records.length; recordIndex++) {
                var item = new FinnKodeWeb.PathItem({
                    text: result.records[recordIndex].get('caption'),
                    bookId: this.bookId,
                    bookSectionId: this.bookSectionId,
                    objectId: result.records[recordIndex].get('locateId'),
                    bookName: this.bookName,
                    handler: this.handler,
                    scope: this.scope
                });
                this.menu.add(item);
            }
            this.menuLoaded = true;
            this.showMenu();
        }
    }
    var loadObjectSiblings = function(bookId, bookSectionId, objectId, scope) {
        httpProxyLoadObjectSiblings.doRequest(
    'read',
    null,
    {
        bookId: bookId,
        bookSectionId: bookSectionId,
        objectId: objectId
    },
    xmlObjectPathReader,
    callbackLoadObjectSiblings,
    scope);
    };

    var handlerSplitButtonArrowClikced = function(button, eventObject) {
        MsgSvc.addMsg({
            Category: 'TableView',
            Action: 'PathNavigatorToolBarExpanded',
            Lable: 'Ekspanderer ' + button.getText()
        });
        if (!this.menuLoaded) {
            loadObjectSiblings(this.bookId, this.bookSectionId, this.objectId, this);
        }
    };
    this.on({
        'arrowclick': handlerSplitButtonArrowClikced,
        scope: this
    });
}

Ext.extend(FinnKodeWeb.PathButton, Ext.SplitButton, {

});

Ext.reg('pathbutton', FinnKodeWeb.PathButton);
