﻿/**
 * A panel hold vemap control
 * @class Ext.ux.VEPanel
 * @extends Ext.Panel
 */
Ext.ux.VEPanel = Ext.extend(Ext.Panel, {
    veNotFound: '<h2>VEMap is undefined! Please include VEMap javascript before Ext.ux.VEPanel !</h2>',
    latitude: 23.0,
    longitude: 113.0,
    zoomLevel: 7,
    minZoomLevel: 7,
    maxZoomLevel: 10,
    mapType: 'r',
    fixed: false,
    e: null,
    gdBounds: [new VELatLongRectangle(new VELatLong(26, 108), new VELatLong(19, 117))],
    html: '<h1> Loading VEMap ... </h1>',
    initComponent: function() {
        Ext.ux.VEPanel.superclass.initComponent.apply(this, arguments);
        //
    },
    afterRender: function() {
        Ext.ux.VEPanel.superclass.afterRender.apply(this, arguments);
        if (typeof VEMap == 'undefined') {
            this.body.update(this.veNotFound);
        }
    },
    onResize: function(w, h) {
        Ext.ux.VEPanel.superclass.onResize.call(this, w, h);
        if (!this.map && this.rendered) {
            this.map = new VEMap(this.body.id);
            /* */
            e = document.getElementById(this.body.id);
            this.map.AddPushpin = function(a) {
                if (typeof a == "object" && !(a instanceof VEPushpin)) {
                    var e = new VEShape(VEShapeType.Pushpin, a);
                    this.AddShape(e);
                    return e
                }
                VEValidator.ValidateObject(a, "vePushpin", VEPushpin, "VEPushpin");
                var f = this.pushpins.length;
                for (var d = 0; d < f; d++) {
                    var g = this.pushpins[d];
                    if (g.ID == a.ID) throw new VEException("VEMap:AddPushpin", "err_invalidpushpinid", L_invalidpushpinid_text)
                }
                this.pushpins.push(a);
                a._SetMapInstance(this);
                var c = { Latitude: a.LatLong.Latitude, Longitude: a.LatLong.Longitude };
                if (this.GetMapMode() == Msn.VE.MapActionMode.Mode3D) this.m_vegraphicsmanager.Add3DPushpin(a.ID, c.Latitude, c.Longitude, Ext.ux.VEPanel.icon.width, Ext.ux.VEPanel.icon.height, "VEAPI_Pushpin", a, Msn.VE.API.Globals.vepushpinpanelzIndex - 1);
                else this.vemapcontrol.AddPushpin(a.ID, c.Latitude, c.Longitude, a.width || Ext.ux.VEPanel.icon.width, a.height || Ext.ux.VEPanel.icon.height, "VEAPI_Pushpin", a.GetContent(), Msn.VE.API.Globals.vepushpinpanelzIndex - 1)
            };
            /*加载初始化地图  */
            var mapOptions = null;
            mapOptions = new VEMapOptions();
            mapOptions.EnableBirdseye = false;
            mapOptions.EnableDashboardLabels = false;
            mapOptions.LoadBaseTiles = true;
            this.map.SetDashboardSize(VEDashboardSize.Tiny);
            this.map.onLoadMap = this.changeBGColor;
            //          this.map.LoadMap(new VELatLong(this.latitude, this.longitude), null, this.mapType, null, Msn.VE.MapActionMode.Mode3D, null, null, mapOptions);
            this.map.LoadMap(new VELatLong(this.latitude, this.longitude), this.zoomLevel, this.mapType, this.fixed, null, null, mapOptions);
            if (!this.setMapZoomRangeDelegate) {
                this.setMapZoomRangeDelegate = this.setMapZoomRange.createDelegate(this);
            }
            this.map.AttachEvent("onchangeview", this.changeBGColor);
            this.map.AttachEvent("onstartzoom", this.setMapZoomRangeDelegate);
            this.map.AttachEvent("onendzoom", this.setMapZoomRangeDelegate);
            this.map.AttachEvent("onmousewheel", this.setMapZoomRangeDelegate);

            //            if (!this.setDefaultMapTileDelegate) {
            //                this.setDefaultMapTileDelegate = this.setDefaultMapTile.createDelegate(this);
            //            }
            //            this.map.AttachEvent("onresize", this.setDefaultMapTileDelegate);
            this.map.SetScaleBarDistanceUnit(VEDistanceUnit.Kilometers);
        }
        if (this.map) {
            this.map.Resize(w, h);
        }
    },
    changeBGColor: function() {
        if (e) {
            //e.style.backgroundColor = "#B3C6D4";
            e.style.backgroundRepeat = "no-repeat";
            e.style.backgroundPosition = "center center";
            e.style.backgroundImage = "url(\\EQPublish\\images\\loading.gif)";
        }
    },
    destroy: function() {
        if (this.map) {
            this.map.Dispose();
            delete this.map;
        }
        Ext.ux.VEPanel.superclass.destroy.apply(this, arguments);
    },
    cleanIcon: function() {
        if (!this.map) {
            return false;
        }
        try {
            this.map.DeleteAllPushpins();
        }
        catch (ex) { }
    },
    cleanPolygon: function() {
        if (!this.map) {
            return false;
        }
        try {
            this.map.DeleteAllPolygons();
        }
        catch (ex) { }
    },
    setMapPosition: function(lat, lon, level) {
        if (!this.map) {
            return false;
        }
        try {
            this.map.SetCenterAndZoom(new VELatLong(lat, lon), !level ? this.map.GetZoomLevel() : level);
        }
        catch (ex) { }
    },
    setMapPan: function(lat, lon) {
        if (!this.map) {
            return false;
        }
        try {
            this.map.Pan(lat, lon);
        }
        catch (ex) { }
    },
    deleteIcon: function(id) {
        if (!this.map) {
            return false;
        }
        try {
            this.map.DeletePushpin(id);
        }
        catch (ex) { }
    },
    /**
    * item format: { id: 'item-id', lat: 22.1, lon: 114.5, icon: 'iconurl', title: 'item title', details: 'item details'}
    * or an array or the format above
    **/
    addIcon: function(item, iskeeplast) {
        if (!this.map) {
            return false;
        }
        if (!iskeeplast) this.cleanIcon();
        try {
            if (Ext.isArray(item)) { // item is array
                //this.cleanPie();
                this.cleanIcon()
                var lefttopCoord = new VELatLong(0, 180);
                var rightbottomCoord = new VELatLong(90, 0);
                var locs = new Array;

                for (var i = 0; i < item.length; i++) {
                    lefttopCoord.Latitude = item[i].lat > lefttopCoord.Latitude ? item[i].lat : lefttopCoord.Latitude;
                    lefttopCoord.Longitude = item[i].lon < lefttopCoord.Longitude ? item[i].lon : lefttopCoord.Longitude;
                    rightbottomCoord.Latitude = item[i].lat < rightbottomCoord.Latitude ? item[i].lat : rightbottomCoord.Latitude;
                    rightbottomCoord.Longitude = item[i].lon > rightbottomCoord.Longitude ? item[i].lon : rightbottomCoord.Longitude;

                    var pin = new VEPushpin(item[i].id, new VELatLong(item[i].lat, item[i].lon), item[i].icon, item[i].title, item[i].details);
                    pin.width = item.width || 16;
                    pin.height = item.height || 16;
                    this.map.AddPushpin(pin);
                }
                locs.push(lefttopCoord);
                locs.push(rightbottomCoord);
                this.map.SetMapView(locs);
            }
            else { // single item
                var pin = new VEPushpin(item.id, new VELatLong(item.lat, item.lon), item.icon, item.title, item.details);
                pin.width = item.width || 16;
                pin.height = item.height || 16;
                this.map.AddPushpin(pin);
            }
        }
        catch (ex) { }
    },
    addPolygon: function(item, iskeeplast) {
        if (!this.map) {
            return false;
        }
       if (!iskeeplast) this.cleanPolygon();
        try {
            var lefttopCoord = new VELatLong(0, 180);
            var rightbottomCoord = new VELatLong(90, 0);
            var locs = new Array;

            var PolygonLatLon = [];
            for (var c = 0; c < item.length; c++) {
                var strpgon = item[c].Polygon;
                for (var i1 = 0; i1 < strpgon.length; i1++) {
                    PolygonLatLon.push(new VELatLong(strpgon[i1].Lat, strpgon[i1].Lon));
                    lefttopCoord.Latitude = strpgon[i1].Lat > lefttopCoord.Latitude ? strpgon[i1].Lat : lefttopCoord.Latitude;
                    lefttopCoord.Longitude = strpgon[i1].Lon < lefttopCoord.Longitude ? strpgon[i1].Lon : lefttopCoord.Longitude;
                    rightbottomCoord.Latitude = strpgon[i1].Lat < rightbottomCoord.Latitude ? strpgon[i1].Lat : rightbottomCoord.Latitude;
                    rightbottomCoord.Longitude = strpgon[i1].Lon > rightbottomCoord.Longitude ? strpgon[i1].Lon : rightbottomCoord.Longitude;

                };
                var pgon = {
                    id: c,
                    l: PolygonLatLon,
                    color: null,
                    color2: null,
                    w: 2
                };
                var p = new VEPolygon(pgon.id, pgon.l, pgon.color, pgon.color2, pgon.w);
                this.map.AddPolygon(p);
                locs.push(lefttopCoord);
                locs.push(rightbottomCoord);
                this.map.SetMapView(locs);
                PolygonLatLon = [];
            }
        }
        catch (ex) { }
    },
    addLayer: function(id, url, Opacity, ZIndex) {
        if (!this.map) {
            return false;
        }
        if (this.map.GetTileLayerByID(id)) this.map.DeleteTileLayer(id);

        var ts = new VETileSourceSpecification(id, url);
        ts.Bounds = this.gdBounds;
        ts.Opacity = Opacity || 0.5;
        ts.ZIndex = ZIndex || 26;
        ts.NumServers = 1;
        ts.MinZoom = 8;
        ts.MaxZoom = 18;
        this.map.AddTileLayer(ts, true);
    }
	,
    setMapZoomRange: function(event) {
        if (!this.map) {
            return false;
        }
        switch (event.eventName) {
            case 'onstartzoom':
                break;
            case 'onendzoom':
                if (this.map.GetZoomLevel() < this.minZoomLevel) this.map.SetZoomLevel(this.minZoomLevel);
                if (this.map.GetZoomLevel() > this.maxZoomLevel) this.map.SetZoomLevel(this.maxZoomLevel);
                break;
            case 'onmousewheel':
                if (event.mouseWheelChange > 0 && event.zoomLevel == this.maxZoomLevel) { this.map.SetZoomLevel(this.maxZoomLevel); }
                if (event.mouseWheelChange < 0 && event.zoomLevel == this.minZoomLevel) { this.map.SetZoomLevel(this.minZoomLevel); }
                break;
        }

    }
});

Ext.ux.VEPanel.icon = { width: 32, height: 32 };

Ext.reg('vepanel', Ext.ux.VEPanel);