(function() {
    cm.define('layoutManager', [], function() {
        var mapEl = L.DomUtil.create('div', 'mapContainer', document.body);
        return {
            getMapContainer: function() {
                return mapEl;
            }
        }
    });
    
    cm.define('mapApplicationConstructor', ['layoutManager'], function(cm, cb) {
        var layoutManager = cm.get('layoutManager');
        var macm = nsGmx.createMapApplication(layoutManager.getMapContainer(), {
            gmxMap: {
                mapID: 'UFKQE'
            }
        })
        macm.create().then(function() {
            cb(macm);
        });
    });

    cm.create();
})();
(function() {
    cm.define('urlManager', [], function(cm) {
        var parser = document.createElement('a');
        parser.href = window.location.href;

        var getQueryVariable = function(variable) {
            var query = parser.search.substring(1);
            var vars = query.split('&');
            for (var i = 0; i < vars.length; i++) {
                var pair = vars[i].split('=');
                if (decodeURIComponent(pair[0]) == variable) {
                    return decodeURIComponent(pair[1]);
                }
            }
        };

        return {
            getParam: getQueryVariable
        };
    });

    cm.define('config', ['urlManager'], function(cm, cb) {
        var urlManager = cm.get('urlManager');
        if (urlManager.getParam('config')) {
            var oReq = new XMLHttpRequest();
            oReq.onload = function(e) {
                if (e.currentTarget.readyState === 4 && e.currentTarget.status === 200) {
                    try {
                        var resp = JSON.parse(e.currentTarget.response.slice(1, -1));
                        var cfg = JSON.parse(resp.Result);
                        cb(cfg);
                    } catch (e) {
                        console.warn('invalid JSON');
                        cb({});
                    }
                }
            };
            oReq.open('get', 'http://maps.kosmosnimki.ru/TinyReference/Get.ashx?id=' + urlManager.getParam('config'), true);
            oReq.send();
        } else {
            return {};
        }
    });

    cm.define('layoutManager', [], function() {
        var mapEl = L.DomUtil.create('div', 'mapContainer', document.body);
        return {
            getMapContainer: function() {
                return mapEl;
            }
        }
    });

    cm.define('mapApplicationConstructor', ['layoutManager', 'config'], function(cm, cb) {
        var config = cm.get('config');
        var layoutManager = cm.get('layoutManager');
        var macm = nsGmx.createMapApplication(layoutManager.getMapContainer(), config)
        macm.create().then(function() {
            cb(macm);
        });
    });

    cm.create();
})();
load from permalink