﻿Type.registerNamespace("ADM.UserControls.VEControls");

ADM.UserControls.VEControls.Map = function() {
    this._map = null;
    this._elementID = null;
    this._isLoaded = false;
    this._clientToken = null;
}
ADM.UserControls.VEControls.Map = function(elementID) {
    this._map = null;
    this._elementID = elementID;
    this._isLoaded = false;
    this._clientToken = null;
}
ADM.UserControls.VEControls.Map = function(elementID, clientToken) {
    this._map = null;
    this._elementID = elementID;
    this._isLoaded = false;
    this._clientToken = clientToken;
}
ADM.UserControls.VEControls.Map.prototype = {
    initialize : function() {
        if (this._elementID != null) {
            this._map = new VEMap(this._elementID);
            
            this._map.AttachEvent("onchangeview", Function.createDelegate(this, this._onChangeViewCallback));
            this._map.AttachEvent("onkeydown", Function.createDelegate(this, this._onKeyDownCallback));
        }
    },
    dispose : function() {
        if (this._map != null) {
            this.clear();
            
            this._map.DetachEvent("onchangeview", Function.createDelegate(this, this._onChangeViewCallback));
            this._map.DetachEvent("onkeydown", Function.createDelegate(this, this._onKeyDownCallback));
            
            this._map.Dispose();
            this._map = null;
            
            this.markAsUnloaded();
        }
    },
    load : function() {
        var vemap = this.get_instance();
        // set the client token.
        if (this._clientToken != null && this._clientToken != "") {
            vemap.SetClientToken(this._clientToken);
        }
        // must be called before LoadMap.
        vemap.SetDashboardSize(VEDashboardSize.Tiny);
        vemap.LoadMap();
        vemap.HideMiniMap();
        vemap.SetScaleBarDistanceUnit(VEDistanceUnit.Kilometers);
        
        this.markAsLoaded();
    },
    clear : function() {
        if (this._map != null) {
//            this._map.Clear();
//            this._map.DeleteRoute();
//            this._map.DeleteAllTileLayers();
            this._map.DeleteAllShapes();
            this._map.DeleteRoute();
            this._map.DeleteAllShapeLayers();
            this._map.DeleteAllTileLayers();
        }
    },
    get_instance : function() {
        if (this._map == null) {
            this.initialize();
        }
        return this._map;
    },
    get_elementID : function() {
        return this._elementID;
    },
    set_elementID : function(id) {
        this._elementID = id;
    },
    set_clientToken : function(clientToken) {
        this._clientToken = clientToken;
    },
    markAsLoaded : function() {
        this._isLoaded = true;
    },
    markAsUnloaded : function() {
        this._isLoaded = false;
    },
    isLoaded : function() {
        return this._isLoaded;
    },
    _onChangeViewCallback : function(e) {
        // disable birdeye, aerial, shaded, hybrid, oblique and birdseye hybrid map style keyboard shortcut.
        if (e.mapStyle != "r") {
            var vemap = this.get_instance();
            vemap.SetMapStyle(VEMapStyle.Road);
            return true;
        }
    },
    _onKeyDownCallback : function(e) {
        // disable 3D mode keyboard shortcut.
        if (e.keyCode == 51) {
            var vemap = this.get_instance();
            vemap.SetMapMode(VEMapMode.Mode2D);
            return true;
        }
    }
}
