﻿Ext.namespace('FinnKodeWeb');

FinnKodeWeb.HashManager = function(config) {
    config = config || {};

    var _isNavigating = false;
    var _window = config.wnd;
    var _splitChar = '|';
    var isObjectIdHash = function() {
        return this.objectId != null &&
            this.mainTabId == null &&
            this.bookSectionId == null;
    };
    this.addEvents('parsed');
    this.listeners = config.listeners;

    FinnKodeWeb.HashManager.superclass.constructor.call(this, config);

    Ext.apply(this, {
        beginNavigate: function() {
            _isNavigating = true;
        },
        endNavigate: function() {
            _isNavigating = false;
        },
        isNavigating: function() {
            return _isNavigating;
        },
        reset: function() {
            this.mainTabId = null;
            this.bookSectionId = null;
            this.bookSectionViewId = null;
            this.objectId = null;
            this.anchor = null;
            this.source = null;
        },
        parseHash: function(hashString) {
            if (!_isNavigating) {
                this.reset();
                // remove #
                var hash = hashString.toString();
                hash = hash.replace(/#/, '');
                // remove first split character to avoid empty array entry
                hash = hash.indexOf(_splitChar) == 0 ? hash.substring(1) : hash;
                if (hash == "") return;
                var hashArray = hash.split(_splitChar);
                var hashIndex = 0;
                for (hashIndex = 0; hashIndex < hashArray.length; hashIndex++) {
                    var hashStringValue = new String(hashArray[hashIndex]).toString();
                    if (hashIndex == 0) {
                        // check if value is an integer and hence an anchor/objectId
                        if (!isNaN(hashStringValue)) {
                            this.objectId = hashStringValue;
                            break;
                        }
                        else this.mainTabId = hashStringValue;
                        continue;
                    }
                    if (hashIndex == 1) {
                        this.bookSectionId = hashStringValue;
                        continue;
                    }
                    if (hashIndex == 2) {
                        if (!isNaN(hashStringValue)) {
                            this.objectId = hashStringValue;
                        }
                        else this.anchor = hashStringValue;
                        continue;
                    }
                    if (hashIndex == 3) {
                        // check if value is a filepath and hence a source
                        if (hashStringValue.indexOf('.') > 0) {
                            this.source = hashStringValue;
                        }
                        else this.bookSectionViewId = hashStringValue;
                        continue;
                    }
                    if (hashIndex == 4) {
                        this.source = hashStringValue;
                        continue;
                    }
                }
                if (isObjectIdHash.call(this)) {
                    // find the book and bookSection for this objectId
                    MsgSvc.addMsg({
                        Category: 'HashManager',
                        Action: 'BeforeGetBookSection',
                        Label: this.objectId
                    });
                    // load children of node
                    var cs = new finnkode.kith.no.icontentservice();
                    cs.GetBookSectionForObjectId(this.objectId,
                    function(result, userContext, methodName) {
                        MsgSvc.addMsg({
                            Category: 'HashManager',
                            Action: 'AfterGetBookSection',
                            Label: userContext.objectId
                        });
                        userContext.mainTabId = result.BookID;
                        userContext.bookSectionId = result.ID;
                        userContext.fireEvent('parsed');
                    },
                    function(result, userContext, methodName) {
                        MsgSvc.addMsg({
                            Category: 'HashManager',
                            Action: 'AfterGetBookSection',
                            Label: 'Feilet i lasting av ' + userContext.objectId
                        });
                        userContext.fireEvent('parsed');
                    },
                    this);
                }
                else {
                    this.fireEvent('parsed');
                }
            }
        },
        createHashString: function() {
            var hashString = new String();
            if (this.mainTabId) hashString = hashString.concat('|', this.mainTabId);
            if (this.bookSectionId) hashString = hashString.concat('|', this.bookSectionId);
            if (this.anchor || this.objectId) hashString = hashString.concat('|', this.anchor || this.objectId);
            if (this.bookSectionViewId) hashString = hashString.concat('|', this.bookSectionViewId);
            if (this.source) hashString = hashString.concat('|', this.source);
            return hashString.length > 0 ? '#'.concat(hashString) : '';
        },
        updateLocationHash: function() {
            var hashString = this.createHashString();
            if (_window.location.hash != hashString) {
                _window.location.hash = hashString;
            }
        },
        hasContentHash: function() {
            return this.objectId != null || (this.anchor != null && this.source);
        }
    });
};
Ext.extend(FinnKodeWeb.HashManager, Ext.util.Observable);
