﻿var KartController = Class.create();

KartController.prototype =
{
    m_mapExpandedEventName: "controller:mapexpanded",
    m_mapDimensionChangedEventName: "controller:mapdimensionchanged",
    m_mapMarkersRemoveEventName: "controller:removemarkers",
    m_customerId: null,
    m_themeId: null,
    m_serviceName: null,
    m_customerName: null,
    m_getFeatureTolerancePixels: null,
    m_initValues: null,
    m_wmsResources: null,
    m_poiLayers: null,

    m_applicationBaseUrl: null,

    m_map: null,
    m_mapElementId: null,
    m_mapElement: null,
    m_radMapElementContainer: null,
    m_radMainSplitter: null,
    m_radLeftMenu: null,
    m_radLeftMenuLogoPane: null,
    m_radLeftMenuContentPane: null,
    m_radLeftMenuButtonPane: null,
    m_radMapAndBars: null,
    m_pageElements: null,
    m_radPaneSlidingMenu: null,
    m_radSlidingMenuTabStrip: null,
    m_radSlidingMenuMultiPage: null,
    m_statusbarInformationDiv: null,

    m_globeLoaded: false,
    m_globeCallstack: [],
    m_cacheUrlPattern: "",
    m_srs: "EPSG:32633", //srs used in map
    m_display3D: false,
    m_display2D: true,
    m_open3DCount: 0,

    initialize: function(
            t_display2D,
            t_display3D,
            t_pageElements,
            t_customerId, //From HttpContext.Items["customerId"]
            t_themeId, //From HttpContext.Items["themeId"]
            t_serviceName, //From HttpContext.Items["serviceName"]
            t_customerName, //From HttpContext.Items["customerName"]
            t_getFeatureTolerancePixels,
            t_initValues, //From map initialization values in configuration provider.
            t_wmsResources, //WMS-list from configuration provider. Hash with name as key
            t_poiLayers, //POI-list from configuration provider. Hash with poiId as key

            t_applicationBaseUrl,
            t_cacheUrlPattern,

            t_mapElementId, //Container for webatlas-tiles.
            t_radMapElementContainer, //RadPane-Wrapper for the webatlas-element. 

            t_radMainSplitter, //RadSplitter with full layout.
            t_radLeftMenu, //RadPane containing left menu content.
            t_radLeftMenuLogoPane, //RadPane containing logo image and solution presentation text
            t_radLeftMenuContentPane, //RadPane containing left menu items.
            t_radLeftMenuButtonPane, //RadPane containing buttons for left menu bottom row.
            t_radMapAndBars, //RadPane containing splitter for content to the right of the menu
            t_radPaneSlidingMenu,
            t_radSlidingMenuTabStrip,
            t_radSlidingMenuMultiPage,
            t_statusbarInformationDiv,
            t_mapGlobeSplitter
        ) {
        this.m_eventElement = $("controller");
        this.m_pageElements = t_pageElements;
        this.m_display2D = t_display2D;
        this.m_display3D = t_display3D;

        if (t_pageElements.globePane != null) {
            t_pageElements.globePane.add_expanded(this._globePaneExpanded.bind(this));
            t_pageElements.globePane.add_collapsed(this._globePaneCollapsed.bind(this));
            t_pageElements.globePane.add_beforeCollapse(this._globePaneBeforeCollapse.bind(this));
        }

        if (t_pageElements.mapPane != null) {
            t_pageElements.mapPane.add_expanded(this._mapPaneExpanded.bind(this));
            t_pageElements.mapPane.add_collapsed(this._mapPaneCollapsed.bind(this));
            t_pageElements.mapPane.add_beforeCollapse(this._mapPaneBeforeCollaps.bind(this));
            t_pageElements.mapPane.add_resized(this.internal_resizeMap.bind(this));
        }

        this.m_customerId = t_customerId;
        this.m_themeId = t_themeId;
        if(this.m_themeId == 128) { this.m_open3DCount = 1; } // increase 3D count to indicate 3D already open in theme Web3D
        this.m_serviceName = t_serviceName;
        this.m_customerName = t_customerName;
        this.m_getFeatureTolerancePixels = t_getFeatureTolerancePixels;
        this.m_initValues = t_initValues;
        this.m_wmsResources = t_wmsResources;
        this.m_poiLayers = t_poiLayers;

        this.m_applicationBaseUrl = t_applicationBaseUrl;
        this.m_cacheUrlPattern = t_cacheUrlPattern
                                    .sub("{customerId}", String(t_customerId))
                                    .sub("{themeId}", String(t_themeId));

        this.m_mapElementId = t_mapElementId;
        this.m_mapElement = $(t_mapElementId);

        this.m_radMainSplitter = t_radMainSplitter;
        this.m_radMapElementContainer = t_radMapElementContainer;
        this.m_radLeftMenu = t_radLeftMenu;
        this.m_radLeftMenuLogoPane = t_radLeftMenuLogoPane;
        this.m_radLeftMenuContentPane = t_radLeftMenuContentPane;
        this.m_radLeftMenuButtonPane = t_radLeftMenuButtonPane;
        this.m_radMapAndBars = t_radMapAndBars;
        this.m_radPaneSlidingMenu = t_radPaneSlidingMenu;
        this.m_radPaneSlidingMenu.collapse();
        this.m_radSlidingMenuTabStrip = t_radSlidingMenuTabStrip;
        this.m_radSlidingMenuMultiPage = t_radSlidingMenuMultiPage;
        this.m_statusbarInformationDiv = t_statusbarInformationDiv;
        this.m_statusbarInformationDiv.className = "hiddenDiv";

        //this._setLayout();
        this._initMap();

        if (t_display3D) {
            $attemptInvoke(
                (function() {
                    this._showGlobe(this.m_initValues.coordinate, this.m_initValues.zoom);
                }).bind(this)
            );
        }
    },

    getSrs: function() {
        return this.m_srs;
    },

    getDisplay3D: function() {
        return this.m_display3D;
    },

    getDisplay2D: function() {
        return this.m_display2D;
    },

    //calculates feature search tolerance in meters based on configuration value provided in pixels.
    getFeatureSearchTolerance: function() {
        var t_dim = this.getMapPaneDimensions();
        var t_topLeft = this.m_map.getPixelLatLon(new Coordinate(0, 0)).transform('EPSG:32633');
        var t_bottomRight = this.m_map.getPixelLatLon(new Coordinate(t_dim.width, t_dim.height)).transform('EPSG:32633');
        var t_mpp = (t_bottomRight.x - t_topLeft.x) / t_dim.width;
        return parseInt(t_mpp * this.m_getFeatureTolerancePixels);
    },

    getCacheUrl: function(t_wms, t_layer) {
        return "/" + this.m_cacheUrlPattern
                    .sub("{resourceId}", String(t_wms.id))
                    .sub("{layerId}", String(t_layer.id));
    },

    getMapPaneDimensions: function() {
        return {
            width: this.m_radMapElementContainer.getWidth(),
            height: this.m_radMapElementContainer.getHeight()
        };
    },

    navigatePath: function(t_coordinates) {
        this._navigatePath(t_coordinates);
        //this.navigatePath2D(t_coordinates);
    },

    stopGlobeUpdate: function() {
        if (this.m_globeLoaded) {
            //TODO: Sjekk capabilities.
            var t_globeController = this.m_pageElements.globePane.getExtContentElement().contentWindow.g_globeController;

            //Rotate view
            t_globeController.stopUpdate();
        }
    },

    updateGlobeRotate: function(t_anglex, t_angley) {
        if (this.m_globeLoaded) {
            //TODO: Sjekk capabilities.
            var t_globeController = this.m_pageElements.globePane.getExtContentElement().contentWindow.g_globeController;

            //Rotate view
            t_globeController.updateRotate(t_anglex, t_angley);
        }
    },

    rotateGlobe: function(t_anglex, t_angley) {
        if (this.m_globeLoaded) {
            //TODO: Sjekk capabilities.
            var t_globeController = this.m_pageElements.globePane.getExtContentElement().contentWindow.g_globeController;

            //Rotate view
            t_globeController.rotateAroundPointer(t_anglex, t_angley);
        }
    },

    updateGlobeUpDown: function(t_speed) {
        if (this.m_globeLoaded) {
            //TODO: Sjekk capabilities.
            var t_globeController = this.m_pageElements.globePane.getExtContentElement().contentWindow.g_globeController;

            //Translate view
            t_globeController.updateUpDown(t_speed);
        }
    },

    translateGlobeUpDown: function(t_distance) {
        if (this.m_globeLoaded) {
            //TODO: Sjekk capabilities.
            var t_globeController = this.m_pageElements.globePane.getExtContentElement().contentWindow.g_globeController;

            //Translate view
            t_globeController.translateUpDown(t_distance);
        }
    },

    _showGlobeNavigationButtons: function(t_show) {
        if (this.m_globeLoaded) {
            //TODO: Sjekk capabilities.
            var t_globeController = this.m_pageElements.globePane.getExtContentElement().contentWindow.g_globeController;
            t_globeController.showNavigationButtons(t_show);
        }

        // Don't push! If Globe isn't loaded, it will be set in Applet's init-method. Otherwise it will be too early...
        //        else {
        //            this.m_globeCallstack.push(this._showGlobeNavigationButtons.bind(this, t_show));
        //        }
    },

    addGlobeAnnotation: function(t_annotation) {
        if (this.m_globeLoaded) {
            //TODO: Sjekk capabilities.
            var t_globeController = this.m_pageElements.globePane.getExtContentElement().contentWindow.g_globeController;
            //Add annotation
            t_globeController.setAnnotation(t_annotation);
        }
        //        else
        //        {
        //            this.m_globeCallstack.push(this.addGlobeAnnotation.bind(this, t_annotation));
        //        }
    },

    startGlobeMeasure: function() {
        if (this.isGlobeExpanded()) {
            //TODO: Sjekk capabilities.
            var t_globeController = this.m_pageElements.globePane.getExtContentElement().contentWindow.g_globeController;
            t_globeController.startMeasure();
        }
    },
    
    flyToGlobeObject: function(t_objectName) {
        if (this.isGlobeExpanded()) {
            //TODO: Sjekk capabilities.
            var t_globeController = this.m_pageElements.globePane.getExtContentElement().contentWindow.g_globeController;
            t_globeController.flyToObject(t_objectName);
        }
    },

    _navigatePath: function(t_coordinates) {
        if (!this.isGlobeExpanded()) {
            this.showGlobe();
        }
        if (this.m_globeLoaded) {
            //TODO: Sjekk capabilities.
            var t_globeController = this.m_pageElements.globePane.getExtContentElement().contentWindow.g_globeController;
            t_globeController.setGlobeFollow(false);
            this.zoomOnBBox(t_coordinates);

            //Fly polyline
            t_globeController.flyPolyline(t_coordinates);
        }
        else {
            //Lagre funksjonskall i påvente av at globen lastes.
            this.m_globeCallstack.push(this._navigatePath.bind(this, t_coordinates));
        }
    },

    zoomOnBBox: function(t_coordinates) {
        //Set 2d viewport
        var x1 = t_coordinates[0].x;
        var y1 = t_coordinates[0].y;
        var x2 = t_coordinates[0].x;
        var y2 = t_coordinates[0].y;
        var i, t_length = t_coordinates.length;
        for (i = 1; i < t_length; i++) {
            if (t_coordinates[i].x > x2) {
                x2 = t_coordinates[i].x;
            }
            if (t_coordinates[i].x < x1) {
                x1 = t_coordinates[i].x;
            }
            if (t_coordinates[i].y > y2) {
                y2 = t_coordinates[i].y;
            }
            if (t_coordinates[i].y < y1) {
                y1 = t_coordinates[i].y;
            }
        }
        var t_coords = {
            'x1': x1,
            'y1': y1,
            'x2': x2,
            'y2': y2
        };

        this.m_map.zoomOnBoundingBox(t_coords, true);
    },

    getZoomLevelForBBox: function(t_x1, t_y1, t_x2, t_y2) {
        var t_metersX = t_x2 - t_x1;
        var t_metersY = t_y2 - t_y1;

        var t_halfWidth = this.m_radMapAndBars.get_width() / 2;
        var t_halfHeight = (this.m_radMapAndBars.get_height() - 61) / 2;

        var t_zoomPoint = new Coordinate(t_x1 + ((t_x2 - t_x1) / 2), t_y1 + ((t_y2 - t_y1) / 2));

        for (var i = 1; i < AJAXMAP.ZOOMLEVELS.length; i++) {
            var t_tmpLevel = AJAXMAP.ZOOMLEVELS[i];
            var t_coordsPerPixel = t_tmpLevel / 256;
            var t_tmpBoundX = t_zoomPoint.x + (t_halfWidth * t_coordsPerPixel);
            var t_tmpBoundY = t_zoomPoint.y + (t_halfHeight * t_coordsPerPixel);

            if (t_tmpBoundX > t_x1 && t_tmpBoundY > t_y2) {
                return i;
            }
        }

        return 6;
    },

    //    /**
    //    / Funksjon for å flytte linjemarkøren til 2d-kartet 
    //    */
    //    movePathMarker: function(t_coordinates, t_timedTraverse) {
    //        if (t_coordinates.length == 0)
    //            return;
    //        var marker = this.m_map.getAnnotation('pathMarker');

    //        if (!marker) {
    //            var ann = new Annotation(new Coordinate(t_coordinates[0].x, t_coordinates[0].y));
    //            this.m_map.addAnnotation(ann);
    //            ann.id = 'pathMarker';
    //        }

    //        var ann = this.m_map.getAnnotation('pathMarker');
    //        ann.coordinate = new Coordinate(t_coordinates[0].x, t_coordinates[0].y);
    //        var screenCoords = this.m_map.geoToScreenCoordinates(ann.coordinate);
    //        ann.x = screenCoords.x + ann.xOffset;
    //        ann.y = screenCoords.y + ann.yOffset;
    //        ann.element.style.left = ann.x + 'px';
    //        ann.element.style.top = ann.y + 'px';

    //        t_coordinates = t_coordinates.splice(1, t_coordinates.length);
    //        // Repeat for remaining coordinates
    //        if (t_timedTraverse)
    //            if (t_timedTraverse == true)
    //            setTimeout(this.movePathMarker.bind(this, t_coordinates), 200);
    //    },

    movePathMarkerToLatLon: function(t_latrad, t_lonrad, t_action, t_azimuth) {
        var t_latDeg = t_latrad * 180 / Math.PI;
        var t_lonDeg = t_lonrad * 180 / Math.PI;
        var c = new Coordinate;
        var t_UTMCoordinate = c.toUTM(t_lonDeg, t_latDeg, 33);

        var marker = this.m_map.getAnnotation('pathMarker');

        if (!marker) {
            var ann = new Annotation(
                new Coordinate(t_UTMCoordinate.x, t_UTMCoordinate.y),
                "",
                "",
                "/Static/Shared/images/ui/pilen2.gif"
            );
            this.m_map.addAnnotation(ann);
            ann.id = 'pathMarker';
        }

        var ann = this.m_map.getAnnotation('pathMarker');
        ann.coordinate = new Coordinate(t_UTMCoordinate.x, t_UTMCoordinate.y);
        var screenCoords = this.m_map.geoToScreenCoordinates(ann.coordinate);
        ann.x = screenCoords.x + ann.xOffset;
        ann.y = screenCoords.y + ann.yOffset;
        ann.element.style.left = ann.x + 'px';
        ann.element.style.top = ann.y + 'px';

        if (t_action == "stop") {
            this.m_map.removeAnnotation('pathMarker');
            this.m_pageElements.globePane.getExtContentElement().contentWindow.g_globeController.setGlobeFollow(true);
        }
    },

    getInitValues: function() {
        return this.m_initValues;
    },

    getCustomerId: function() {
        return this.m_customerId;
    },

    getThemeId: function() {
        return this.m_themeId;
    },

    getServiceName: function() {
        return this.m_serviceName;
    },

    getWmsResources: function() {
        return this.m_wmsResources;
    },

    getPoiLayers: function() {
        return this.m_poiLayers;
    },

    getMap: function() {
        return this.m_map;
    },

    getDimensions: function() {
        return {
            width: this.m_radMainSplitter.get_width(),
            height: this.m_radMainSplitter.get_height()
        };
    },

    getApplicationBaseUrl: function() {
        return this.m_applicationBaseUrl;
    },

    _initMap: function() {
        // Tile-log key-format: TEMA-sandnes-sykkelkart
        var t_customer = this.m_customerName.split(" ", 1)[0].replace("-", "");
        var t_client = 'TEMA-' + t_customer.toLowerCase() + '-' + this.m_serviceName.toLowerCase();

        var t_map = new AJAXMAP(
            this.m_mapElementId,
            'http://webcache.test.gisline.no/',
            false,
            t_client
        );

        t_map.init(
            this.m_initValues.coordinate,
            this.m_initValues.zoom);

        t_map.disableAnnotationInfo();
        t_map.setMouseZoom(true);

        var t_element = $(this.m_mapElementId);
        t_map.resize(
            this.m_radMapElementContainer.getHeight(),
            this.m_radMapElementContainer.getWidth());

        if (this.m_initValues.disableTiles) {
            t_map.hideLayer("layer_0");
        }

        this.m_map = t_map;
    },

    setMapStyle: function(t_schemeNumber) {
        if (!this.m_initValues.disableTiles) {
            this.m_map.setMapStyle(t_schemeNumber);
        }
        else {
            //0 = kart, 1 = foto, 2 = hybrid
            if (t_schemeNumber != 0) {
                this.m_map.showLayer("layer_0");
                this.m_map.setMapStyle(t_schemeNumber);
            }
            else {
                this.m_map.hideLayer("layer_0");
            }
        }
    },

    addMapElementEventListener: function(
        t_eventName,
        t_callback) {
        Event.observe(
                this.m_mapElement,
                t_eventName,
                t_callback
            );
    },

    getContentWindowDimension: function() {
        return {
            width: (this.m_radMapAndBars.getInnerWidth() - 22),
            height: this.m_radMapAndBars.getInnerHeight()
        };
    },

    isSlidingMenuCollapsed: function() {
        return this.m_radPaneSlidingMenu.get_collapsed();
    },

    selectFirstSlidingMenuTab: function() {
        this.m_radSlidingMenuTabStrip.get_tabs().getTab(0).select();
    },

    openSlidingMenu: function() {
        var t_centerCoord = this.m_map.getCenterCoordinate();
        this.m_radSlidingMenuTabStrip.get_tabs().getTab(0).select();
        this.m_radPaneSlidingMenu.expand();
        this.m_statusbarInformationDiv.className = "hiddenDiv";
        this.m_map.centerOnCoords(t_centerCoord);
    },

    hideSlidingMenu: function() {
        this._collapseSlidingMenuPane();
        this.m_statusbarInformationDiv.className = "shownDiv";
    },

    closeSlidingMenu: function() {
        this._collapseSlidingMenuPane();
        this.m_statusbarInformationDiv.className = "hiddenDiv";
    },

    _collapseSlidingMenuPane: function() {
        var t_centerCoord = this.m_map.getCenterCoordinate();
        this.m_radPaneSlidingMenu.collapse();
        this.m_map.centerOnCoords(t_centerCoord);
    },

    addSlidingMenuTab: function(t_url, t_title, t_tabValue) {
        var tab = this.m_radSlidingMenuTabStrip.findTabByValue(t_tabValue);

        if (tab != null) {
            if (!tab.get_visible()) { tab.set_visible(true); }
            tab.set_text(t_title);
            var pageView = tab.get_pageView();

            if (pageView != null) {
                //Get content from server.
                pageView.get_element().innerHTML = "<br/><img src='/Static/Shared/images/loading_bar.gif' />";
                new Ajax.Updater(
                    pageView.get_element(),
                    t_url,
                    {
                        evalScripts: true,
                        method: "GET"
                    });
            }
        }
    },

    hideUnusedTabs: function(t_firstUnusedTabValue) {
        var i;
        for (i = t_firstUnusedTabValue; i < 6; i++) {
            var tab = this.m_radSlidingMenuTabStrip.findTabByValue(i);
            if (tab != null) {
                tab.set_visible(false);
            }
        }
    },

    removeAllMapMarkers: function() {
        this.closeSlidingMenu();
        DrawingControl.removeDrawingGroup("profiledirection"); // Høydeprofilpiler
        SearchControl.clearSearchInfo(); // Søkeresultat
        
        this.m_eventElement.fire(this.m_mapMarkersRemoveEventName);
    },

    getGlobeView: function() {
        if (this.m_globeLoaded) {
            //TODO: Sjekk capabilities.
            var t_globeController = this.m_pageElements.globePane.getExtContentElement().contentWindow.g_globeController;

            return t_globeController.getGlobeView();
        }
        return null;
    },

    getGlobeController: function() {
        try {
            return this.m_pageElements.globePane.getExtContentElement().contentWindow.g_globeController;
        } catch (e) { }
        return null;
    },

    showGlobe: function() {
        if (this.m_pageElements.globePane != null) {
            if(navigator.platform.toLowerCase().indexOf("mac") == -1) {
                //move center view.
                var t_width = this.m_map.getWidth();
                this.m_map.move(new Move(-(t_width / 4), 0));
                
                if (this.m_display2D) {
                    g_navigationMenu.show3DButtons();
                }
            }
            
            if (this.m_pageElements.globePane.isExternalContent()) {
                this.m_pageElements.globePane.getExtContentElement().contentWindow.g_globeController.setHiddenMode(false);
            }

            this.m_pageElements.globePane.expand();
            //TODO: Update center point and POIs
        }
    },

    hideGlobe: function() {
        if (this.m_pageElements.globePane.getExtContentElement() != null) {
            //stopp eventuelle pågående globe-operasjoner
            this.stopGlobeUpdate();

            //move center view.
            var t_width = this.m_map.getWidth();
            this.m_map.move(new Move((t_width / 2), 0));

            this.m_pageElements.globePane.getExtContentElement().contentWindow.g_globeController.setHiddenMode(true);
            this.m_pageElements.globePane.collapse();
        }
    },

    _fireMapDimensionChanged: function() {
        this.m_eventElement.fire(this.m_mapDimensionChangedEventName, {
            display3D: this.m_display3D,
            display2D: this.m_display2D
        });
    },

    m_mapDimensionChangedFromButton: false, // Used for avoiding double-fired event...
    setMapDimension: function(t_2d, t_3d) {
        this.m_mapDimensionChangedFromButton = true;
        if (t_3d) {
            if (!this.m_display3D) {
                this.showGlobe();
            }
            if (t_2d) {
                if (!this.m_display2D) {
                    this._showMap();
                }
            }
            else {
                this._hideMap();
            }
        }
        if (t_2d) {
            if (!this.m_display2D) {
                this._showMap();
            }
            if (!t_3d && this.m_display3D) {
                this.hideGlobe();
            }
        }
        this._fireMapDimensionChanged();
        this.m_mapDimensionChangedFromButton = false;
    },

    _showMap: function() {
        if (this.m_pageElements.mapPane != null) {
            this.m_pageElements.mapPane.expand();
        }

        if (this.isGlobeExpanded()) {
            g_navigationMenu.show3DButtons();
        }
    },

    _hideMap: function() {
        this.m_pageElements.mapPane.collapse();
    },

    isGlobeExpanded: function() {
        if (this.m_pageElements.globePane != null) {
            return !this.m_pageElements.globePane.get_collapsed();
        }
        return false;
    },

    isLeftMenuExpanded: function() {
        if (this.m_radLeftMenu != null) {
            return !this.m_radLeftMenu.get_collapsed();
        }
        return false;
    },

    onGlobePageLoaded: function(t_globeCapabilities) {
        this.m_globeLoaded = true;
        try {
            this.m_globeCallstack.each(
				function(t_func) { t_func() }
			);
        }
        finally {
            this.m_globeCallstack.length = 0;
        }
    },

    m_globeInitialized: false,
    _globePaneExpanded: function(t_sender, t_eventArgs) {
        if (Prototype.Browser.Gecko && this.m_open3DCount > 0) {
            alert("På grunn av problemer med Firefox kan løsningen gå i heng hvis du lukker 3D-vinduet og prøver å åpne det igjen. Da må løsningen restartes med ctrl+F5.");
        }
        
        var t_utm = this.m_map.getCenterCoordinate();
        var t_zoomLevel = this.m_map.getZoomLevel();
        this._showGlobe(t_utm, t_zoomLevel);

        if (!this.m_mapDimensionChangedFromButton) {
            this._fireMapDimensionChanged();
        }

        this.m_open3DCount++;
    },

    _showGlobe: function(t_utm, t_zoomLevel) {
        if (!this.m_globeInitialized ||
            !this.m_pageElements.globePane.getExtContentElement().contentWindow.g_globeController.isAppletInitialized()) {
            var t_ll = this.m_map.UTMToLatLon(t_utm);
            this.m_pageElements.globePane.set_contentUrl(
                "/System/Globe/Applet/" +
                this.m_customerId +
                "?themeId=" + this.m_themeId +
                "&lat=" + t_ll.y +
                "&lon=" + t_ll.x +
                "&zoomlevel=" + t_zoomLevel +
                "&onloadedCallback=window.parent.g_kartController.onGlobePageLoaded.bind(window.parent.g_kartController)");
            this.m_globeInitialized = true;
        }
        else {
            this.m_pageElements.globePane.getExtContentElement().contentWindow.g_globeController.setCenterUTM(
                t_utm
            );
        }
        this.m_display3D = true;

        if (this.m_display2D) {
            g_navigationMenu.show3DButtons();
        }
    },

    _globePaneCollapsed: function(t_sender, t_eventArgs) {
        this.m_display3D = false;
        g_navigationMenu.hide3DButtons();
        if (!this.m_mapDimensionChangedFromButton) {
            this._fireMapDimensionChanged();
        }
    },

    _globePaneBeforeCollapse: function(t_sender, t_eventArgs) {
        //stopp eventuelle pågående globe-operasjoner
        this.stopGlobeUpdate();
        //expand map
        if (this.m_pageElements.mapPane.get_collapsed()) {
            this._showMap();
        }
    },

    _mapPaneExpanded: function(t_sender, t_eventArgs) {
        this.m_display2D = true;
        this._fitMap2D();
        this.m_eventElement.fire(this.m_mapExpandedEventName);
        if (!this.m_mapDimensionChangedFromButton) {
            this._fireMapDimensionChanged();
        }

        // Gjem knapper i applet:
        this._showGlobeNavigationButtons(false);
    },

    _mapPaneCollapsed: function(t_sender, t_eventArgs) {
        this.m_display2D = false;
        if (!this.m_mapDimensionChangedFromButton) {
            this._fireMapDimensionChanged();
        }

        // Vis knapper i applet:
        this._showGlobeNavigationButtons(true);
    },

    _mapPaneBeforeCollaps: function(t_sender, t_eventArgs) {
        //expand map
        if (this.m_pageElements.globePane.get_collapsed()) {
            this.showGlobe();
        }
    },

    //    //Function called after a mappane resize.
    //    _mapPaneResized : function(t_sender, t_eventArgs)
    //    {
    //        this.internal_resizeMap();
    //    },

    internal_resizeMap: function(sender, Args) {
        if (this.m_map != null) {
            this._resizeMap2D(sender.get_width(), sender.get_height());
        }
    },

    _fitMap2D: function() {
        this._resizeMap2D(
                this.m_pageElements.globePane.get_width(),
                this.m_pageElements.globePane.get_height()
            );
    },

    _resizeMap2D: function(t_width, t_height) {
        this.m_map.resize(t_height, t_width);
    },

    _setLayout: function() {
        //WORKAROUND:1 In order to make content fill screen in firefox
        //TODO:1 Do same thing on resize.
        var t_pxHeight = Utilities.Window.getSize().height;
        var t_height = t_pxHeight + "px";
        this.m_radLeftMenu.set_height(t_height);
        this.m_radMapAndBars.set_height(t_height);

        var t_logoContainerHeight = this.m_radLeftMenuLogoPane.get_height();
        var t_buttonContainerHeight = this.m_radLeftMenuButtonPane.get_height();
        var t_leftmenufixedheight = t_logoContainerHeight + t_buttonContainerHeight;
        this.m_radLeftMenuContentPane.set_height((t_pxHeight - t_leftmenufixedheight) + "px");
    }
}
