﻿Ext.namespace('FinnKodeWeb');

FinnKodeWeb.PathItem = function(config) {
    // call superclass constructor
    FinnKodeWeb.PathItem.superclass.constructor.call(this, config);

    this.bookId = config.bookId;
    this.bookSectionId = config.bookSectionId;
    this.objectId = config.objectId;

}

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.menuLoaded = false;
    if (!this.menu) this.menu = new Ext.menu.Menu();

    var callbackLoadObjectSiblings = function(result, args, success) {
        this.menu.removeAll();
        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'),
                    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 handlerSplitButtonClicked = function(button, eventObject) {

    }
    var handlerSplitButtonArrowClikced = function(button, eventObject) {
        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);
