﻿// JScript 文件
// Jfp 09-05-21
// 地图相关类

var isIE = (navigator.userAgent.indexOf("MSIE") != -1);
var apiSearch = {};

if(isIE) {
    //获取引用api时传送的参数
    function findValue(str, key) {
        var re = new RegExp(key + "=[^&$]*");
        var re1 = new RegExp(key + "=");
        var m = str.match(re);
        if(m) return m.toString().substring(key.length + 1, str.length);
        else return "";
    }
    var __ss = document.scripts[document.scripts.length - 1].src.match(/\?[^\?]*$/);
    if(!__ss) __ss = "";
    else __ss = __ss.toString().replace(/\?/, "");
    if(__ss != "") {
        apiSearch.hotspotVersion = findValue(__ss, "hotspotVersion"); // hotSpot版本
    }
}else {
    apiSearch.hotspotVersion = "firefox"; // hotSpot版本
}

var AutoGis;
var LIB_LOADED, EXTEND_LOADED, ZOOMBAR_LOADED, APICTRL_LOADED, DRAW_LOADED;

var apiPath = "/js/autoGis/";
var include = function(file) {
    var files = typeof file == "string" ? [file] : file;
    for (var i = 0; i < files.length; i++) {
        var name = files[i].replace(/^\s|\s$/g, "");
        var att = name.split('.');
        var ext = att[att.length - 1].toLowerCase();
        var isCSS = ext == "css";
        var tag = isCSS ? "link" : "script";
        var attr = isCSS ? " type='text/css' rel='stylesheet' " : " language='javascript' type='text/javascript' ";
        var link = (isCSS ? "href" : "src") + "='" + apiPath + name + "'";
        document.write("<" + tag + attr + link + "></" + tag + ">");
    }
}
include(["lib.js", "generalConfig.js", "gMap_main.js", "mapExtend.js", "apiControls.js", "zoomBar.js", "draw.js", "hotSpot.js", "sign.js", "images/styles.css", "dialog.js", "images/dialog.css"]);

function Def_AutoGis() {
    if(!LIB_LOADED || !EXTEND_LOADED || !ZOOMBAR_LOADED || !APICTRL_LOADED || !DRAW_LOADED) {
        setTimeout(Def_AutoGis, 10);
        return;
    }else {
        AutoGis = function() {return this.initialize.apply(this, arguments)}; // 地图类
        AutoGis.prototype = {
            // autoGisPro 可选参数
            // centerPoint: 默认中心点, useZoomBar: 使用缩放条, useMapType: 使用地图类型切换条, useEye: 使用鹰眼, unBindResize: 不要将窗口缩放事件绑定到该对象
	        initialize : function(div, autoGisPro) {
	            if(!autoGisPro) autoGisPro = {};
	            if(typeof div == "string") div = $(div);
	            this.div = div;
	            this.vG = new GMap2(div);
	            var vG = this.vG;
                vG.div = div;
                vG.mapSize = this.mapSize = {width : div.offsetWidth, height : div.offsetHeight};
        	    
        	    vG.isIE = this.isIE = (navigator.userAgent.indexOf("MSIE") != -1);
                this.loading = new Loading(this.vG);
	            with(this) {
                    vG.div.style.backgroundColor = autoGisPro.bgColor ? autoGisPro.bgColor : "#F2EFE9";
                    
                    this.curMapType = autoGisPro.mapType ? autoGisPro.mapType : "MODEL";
                    vG.mapType = this.curMapType;
                    this.zoom = (typeof autoGisPro.zoom == "number") ? autoGisPro.zoom : 1;
                    vG.tZoom = this.zoomScale[curMapType][zoom];
                    
                    this.centerLatlng = autoGisPro.centerLatlng || (autoGisPro.centerPoint && mapToGis(autoGisPro.centerPoint)) || (GeneralConfig.centerPoint && mapToGis(GeneralConfig.centerPoint)) || (GeneralConfig.centerLatlng && new GLatLng(GeneralConfig.centerLatlng.lat, GeneralConfig.centerLatlng.lng));
                    this.defCenterLatlng = this.centerLatlng;
                    vG.pointer = this.defCenterLatlng;
                    var x, y;
                    if((x = getUrlValue("x")) != "" && (y = getUrlValue("y")) != "")
                        centerLatlng = mapToGis({x: parseInt(x), y: parseInt(y)});
                    else if((x = getUrlValue("lat")) != "" && (y = getUrlValue("lng")) != "")
                        centerLatlng = new GLatLng(parseFloat(x), parseFloat(y));
                    
                    vG.originLatlng = new GLatLng(GeneralConfig.originLat, GeneralConfig.originLng);
                    _chgMapType();
                    vG.setCenter(centerLatlng, vG.tZoom);

                    if(!autoGisPro.cursor) autoGisPro.cursor = "default";
                    if(autoGisPro.cursor != "keepDefault") {
                        var dragObject = vG.getDragObject();
                        dragObject.setDraggableCursor(autoGisPro.cursor);
                        dragObject.setDraggingCursor(autoGisPro.cursor);
                    }
                    vG.disableDoubleClickZoom();
                    
                    vG.zoomMultiple = [1, 0.5, 0.25, 0.125, 0.0625, 0.03125];
                }
                GMap2.appendSpanAfterLogo.bind(vG)();
                vG.centerPoint = new CenterPoint(vG);
                
                if(autoGisPro.useZoomBar || autoGisPro.useMapType || autoGisPro.useEye)
                    this.createZoomBar(autoGisPro.useZoomBar, autoGisPro.useMapType, autoGisPro.useEye, autoGisPro.useNav);
                
                vG.appendEventLayer();
                vG.originPixel = vG.fromLatLngToDivPixel(vG.originLatlng);
                
                vG.coverLayer = new CoverLayer(vG);
                
                if(autoGisPro.useNav) {
                    vG.navMap = new NavMap(vG, [{type : "默认", src : "/images/daohang.gif", hots : [
                        {rect: "282,119,365,159", x: 21961, y: 9375},
                        {rect: "441,155,518,192", x: 2322, y: 8775},
                        {rect: "529,316,606,356", x: 17771, y: 10610},
                        {rect: "527,385,600,437", x: 19360, y: 8606},
                        {rect: "350,220,428,259", x: 23644, y: 7268},
                        {rect: "252,166,326,209", x: 23488, y: 21871},
                        {rect: "311,321,383,359", x: 10624, y: 8381}
                    ]}]);
                    if(typeof autoGisPro.showNav == "undefined" || autoGisPro.showNav) // 默认是否显示导航图
                        vG.navMap.show(0, null, true);
                    if(autoGisPro.useMapType !== false)
                        this.zoomBarCtrl.addMapTypeBut({value:"导航", onclick : function(){vG.navMap.show(0, null, true)}});
                }

                if(autoGisPro.useHotSpot)
                    vG.hotSpot = new HotSpot(vG, vG.originPixel.x, vG.originPixel.y, autoGisPro.showHotspotInfo, autoGisPro.usePop);
                else
                    vG.hotSpot = null;
                vG.originPixel.x0 = vG.originPixel.x / vG.zoomMultiple[this.zoom];
                vG.originPixel.y0 = vG.originPixel.y / vG.zoomMultiple[this.zoom];
                
                if(autoGisPro.showAD) {
                    this.vG.showAD = true;
                    this.vG.adList = [];
                    this.loadAD();
                }

                this.createPopMenu();
                
                this.unBindResize = !!autoGisPro.unBindResize;
                this.bindEvents();
                
                if(vG.hotSpot) {
                    vG.hotSpot.loadCellDatas();
                    vG.hotSpot.hotSpot_loaded = true;
                }
                    
                this.busLayer = this.vG.newLayer("busLayer", 18, 13);
                this.bus = [];
                
                vG.sign = new Sign(vG);
	        },
	        mapType : {
	            "MODEL" : U_MODEL_MAP,
	            "NORMAL" : G_NORMAL_MAP,
	            "SATELLITE" : G_SATELLITE_MAP,
	            "HYBRID" : G_HYBRID_MAP
	        },
	        zoomScale : {
	            "MODEL" : [18, 17, 16 , 15, -1, -1],
                "NORMAL" : [-1, 17, 16, 15, 14, 13],
                "SATELLITE" : [18, 17, 16, 15, 14, 13],
                "HYBRID" : [18, 17, 16, 15, 14, 13],
                "Eye" : [14, 13, 12, 11, 10, 9]
            },
            zoomMultiple : [1, 0.5, 0.25, 0.125, 0.0625, 0.03125],
            chgMapType : function(type) {
                this.loading.setView(true);
                setTimeout(function(){this.loading.setView(false)}.bind(this), 2000);
                
                this.curMapType = type;
                this.vG.mapType = this.curMapType;
                if(this.zoomScale[type][this.zoom] == -1) this.setZoom(this.zoom);
                setTimeout(this._chgMapType.bind(this), 10);
            },
            _chgMapType : function() {
                if(this.vG.hotSpot)
                    this.vG.hotSpot.removeAllTile();
            
                if(typeof this.onBeforeMapTypeChange == "function") this.onBeforeMapTypeChange();
                this.vG.setMapType(this.mapType[this.curMapType]);
                if(typeof this.onMapTypeChanged == "function") this.onMapTypeChanged(this.curMapType);

                if(this.vG.logoSpan) {
                    this.vG.logoSpan.style.color = (this.curMapType == "NORMAL") ? "#000000" : "#FFFFFF";
                }
                
                if(this.curMapType == "HYBRID")
                    this.setNormalLayer();
                
                this.refMapArea();
            },
            setZoom : function(zoom) {
                var zoomScale = this.zoomScale[this.curMapType];
                if(zoom < 0 || zoom > zoomScale.length - 1) return;
                var scale = zoomScale[zoom];
                if(scale == -1) {
                    var append = zoom > 2 ? -1 : 1;
                    while(scale == -1) {
                        zoom += append;
                        scale = zoomScale[zoom];
                    }
                }
                
                if(this.zoomBarCtrl)
                    this.zoomBarCtrl.setZoom(zoom, true);
                
                this.refMapArea();
                
                if(this.zoom == zoom) return;
//                this.cZoomConfig = {};
//                this.cZoomConfig.div = this.div.childNodes[0].childNodes[0];
//                this.cZoomConfig.divLeft = this.div.offsetLeft;
//                this.cZoomConfig.divTop = this.div.offsetTop;
//                this.cZoomConfig.curZoom = 100;
//                this.cZoomConfig.cz = 100 * Math.pow(2, zoomScale[zoom] - zoomScale[this.zoom]);
//                this.cZoomConfig.step = (this.cZoomConfig.cz - 100) / 3;
//                this.cZoomConfig.zoom = scale;
//                this.continuousZoom.delay(10, this);
                this.zoom = zoom;
                this.vG.tZoom = scale;
                
                if(this.curMapType == "HYBRID")
                    this.setNormalLayer();

                if(this.vG.hotSpot)
                    this.vG.hotSpot.removeAllTile();
                    
                if(typeof this.onBeforeZoomChange == "function") this.onBeforeZoomChange();
                this.vG.setZoom(scale);
                
                if(this.vGE)
                    this.vGE.setZoom(this.zoomScale["Eye"][this.zoom]);
            },
//            continuousZoom : function() {
//                //var config = this.cZoomConfig;
//                
//                with(this.cZoomConfig) {
//                    curZoom += step;
//                    div.style.zoom = curZoom + "%";
//                    div.style.left = Math.floor(divLeft - (this.mapSize.width * curZoom / 100 - this.mapSize.width) / 2) + "px";
//                    div.style.top = Math.floor(divTop - (this.mapSize.height * curZoom / 100 - this.mapSize.height) / 2) + "px";
//                    if((step > 0 && curZoom < cz) || (step < 0 && curZoom > cz))
//                        this.continuousZoom.delay(10, this);
//                    else {
//                        div.style.zoom = "";
//                        div.style.left = divLeft + "px";
//                        div.style.top = divTop + "px";
//                        this.vG.setZoom(zoom);
//                        this.cZoomConfig = null;
//                        delete this.cZoomConfig;
//                    }
//                }
//            },
            setNormalLayer : function() {
                var _NORMALLayer = this.vG.Yc.fe.pane.nextSibling;
                if(!_NORMALLayer) {
                    setTimeout(this.setNormalLayer.bind(this), 10);
                    return;
                }
                var times = Math.pow(2, 4 - this.zoom);
                _NORMALLayer.style.left = -54.25 * times + "px";
                _NORMALLayer.style.top = -26.75 * times + "px";
                _NORMALLayer = null;
            },
            createZoomBar : function(useZoomBar, useMapType, useEye) {
	            this.zoomBarCtrl = new ZoomBarCtrl(this, useZoomBar, useMapType, useEye);
	            if(this.zoomBarCtrl.eye)
	                this.vGE = this.zoomBarCtrl.eye.map;
	            
                if(this.vGE)
                    this.zoomBarCtrl.onEyeExpandChanged = function(expand) {
                        this.vGE.checkResize();
                        if(this.centerLatlng)
                            this.vGE.setCenter(this.centerLatlng);
                    }.bind(this);
                
                if(this.zoomBarCtrl.mapType)
                    this.zoomBarCtrl.onMapTypeChanged = this.chgMapType.bind(this);
                
                this.zoomBarCtrl.onZoomChanged = this.setZoom.bind(this);
	            
	            if(this.vGE) {
                    GEvent.addListener(this.vGE, "moveend", function() {
                        if(this.parentMoving) return;
                        
                        this.centerLatlng = this.vGE.getCenter();
                        this.parentMoving = true;
                        this.vG.setCenter(this.centerLatlng);
                        this.parentMoving = false;
                        
                        with(this.vG) {
                            originPixel = fromLatLngToDivPixel(originLatlng);
                            if(hotSpot)
                                hotSpot.setLayerXY(originPixel.x, originPixel.y);
                            originPixel.x0 = originPixel.x / zoomMultiple[this.zoom];
                            originPixel.y0 = originPixel.y / zoomMultiple[this.zoom];
                        }
                    }.bind(this));
                    
                    $attachEvent(this.vGE.div, $EventName.mousewheel, function(event) {
                        if(this.wheelZooming) return;
                        
                        event = event || window.event;
                        this.wheelZooming = true;
                        setTimeout(function(){this.wheelZooming = null}.bind(this), 500);
                        if (event.wheelDelta > 0 || event.detail < 0)
                            this.setZoom(this.zoom - 1);
                        else
                            this.setZoom(this.zoom + 1);
                    }.bindWithEvent(this));
                }
	                
	            this.refMapArea();
            },
            createPopMenu : function() {
                var vG = this.vG;
                var menuNodes = [
                    {innerHTML : "移动到该点", action : function(){this.setCenter(this.popMenu.latlng)}.bind(vG)},
                    {innerHTML : "回到中心点", action : function(){this.vG.setCenter(this.defCenterLatlng)}.bind(this)},
                    {innerHTML : "放大", action : function(){this.setZoom(this.zoom - 1)}.bind(this)},
                    {innerHTML : "缩小", action : function(){this.setZoom(this.zoom + 1)}.bind(this)},
                    {innerHTML : "拷贝坐标", action : function(){this.copyCoords(this.vG.popMenu.latlng)}.bind(this)},
                    (vG.hotSpot ? {id: "hotSpotLabel", innerHTML : "显示标签", action : function(){this.chgHotSpotLabelView()}.bind(this)} : null),
                    new PopMenu.Separator(),
                    {innerHTML : "关于 AutoGis", action : function(){alert("AutoGis")}}
                ];
                vG.popMenu = new PopMenu(vG, menuNodes);
            },
            chgHotSpotLabelView: function() {
                var vG = this.vG;
                if(vG.hotSpot.labelShowed) {
                    vG.popMenu.setNode("hotSpotLabel", {innerHTML: "显示标签"});
                    vG.hotSpot.hideLabel();
                }else {
                    vG.popMenu.setNode("hotSpotLabel", {innerHTML: "隐藏标签"});
                    vG.hotSpot.showLabel();
                }
            },
            copyCoords : function(latlng) {
                var lat = latlng.lat();
                var lng = latlng.lng();
                var pixel = this.vG.fromLatLngToDivPixel(latlng);
                var pixel1 = this.vG.fromLatLngToContainerPixel(latlng);
                var point = gisToMap(latlng);
                var s = "纬度(值): " + lat + "\r\n";
                s += "经度(值): " + lng + "\r\n";
                s += "纬度(度): " + gisConvert(lat) + "\r\n";
                s += "经度(度): " + gisConvert(lng) + "\r\n";
                s += "divPixel: " + pixel.x + "," + pixel.y + "\r\n";
                s += "containerPixel: " + pixel1.x + "," + pixel1.y + "\r\n";
                s += "point: " + point.x + "," + point.y;
                copyToClipboard(s, s + "\r\n" + "已拷贝进剪贴板。");
            },
            refMapArea : function() {
                if(!this.vGE || !this.vGE.mapArea) return;
                var mapArea = this.vGE.mapArea;
                var width = this.mapSize.width - 1, height = this.mapSize.height - 1;

                with(this.vG) {
                    mapArea.latlngs = [
                        fromContainerPixelToLatLng(new GPoint(0, 0)),
                        fromContainerPixelToLatLng(new GPoint(width, 0)),
                        fromContainerPixelToLatLng(new GPoint(width, height)),
                        fromContainerPixelToLatLng(new GPoint(0, height))
                    ];
                    mapArea.latlngs[3].close = true;
                }
                mapArea.redraw(true);
            },
            checkResize : function() {
                if(this.mapSize.width == this.div.offsetWidth && this.mapSize.height == this.div.offsetHeight) return;
                this.mapSize.width = this.div.offsetWidth;
                this.mapSize.height = this.div.offsetHeight;
                this.vG.checkResize();
                if(this.vGE && this.centerLatlng)
                    this.vGE.setCenter(this.centerLatlng);
                
                with(this.vG) {
                    originPixel = fromLatLngToDivPixel(originLatlng);
                    if(hotSpot) {
                        hotSpot.setLayerXY(originPixel.x, originPixel.y);
                    }
                    originPixel.x0 = originPixel.x / zoomMultiple[this.zoom];
                    originPixel.y0 = originPixel.y / zoomMultiple[this.zoom];
                }
                if(this.vG.hotSpot)
                    this.vG.hotSpot.loadCellDatas();
                    
                this.vG.sign.refresh(null, true);
            },
            checkArea : function() {return true;
                var modelOut = false; // 3D 超出范围
                var otherOut = false; // 卫星图超出范围
                if(this.curMapType == "MODEL") {
                    var pixel = gisToMap(this.centerLatlng);
                    if(pixel.x < GeneralConfig.bound3DPixel[0] || pixel.x > GeneralConfig.bound3DPixel[2] || pixel.y < GeneralConfig.bound3DPixel[1] || pixel.y > GeneralConfig.bound3DPixel[3]) {
                        modelOut = true;
                    }
                }
                if(modelOut || this.curMapType != "MODEL") {
                    var lat = this.centerLatlng.lat(), lng = this.centerLatlng.lng();
                    if(lat < GeneralConfig.boundWXLatlng[2] || lat > GeneralConfig.boundWXLatlng[0] || lng < GeneralConfig.boundWXLatlng[1] || lng > GeneralConfig.boundWXLatlng[3]) {
                        if(lat < GeneralConfig.boundWXLatlng[2]) lat = GeneralConfig.boundWXLatlng[2];
                        else if(lat > GeneralConfig.boundWXLatlng[0]) lat = GeneralConfig.boundWXLatlng[0];
                        if(lng < GeneralConfig.boundWXLatlng[1]) lng = GeneralConfig.boundWXLatlng[1];
                        else if(lng > GeneralConfig.boundWXLatlng[3]) lng = GeneralConfig.boundWXLatlng[3];
                        
                        otherOut = true;
                    }
                    if(modelOut || otherOut) {
                        if(modelOut) this.chgMapType("NORMAL");
                        this.vG.setCenter(new GLatLng(lat, lng));
                        return false;
                    }
                }
                
                return true;
            },
            bindEvents : function() {
                var vG = this.vG, vGE = this.vGE;
                GEvent.addListener(vG, "move", function() {
                    if(this.vG.hotSpot && this.vG.hotSpot.showed) {
                        this.vG.hotSpot.hideInfo();
                    }
                    this.refMapArea();
                }.bind(this));
                
                GEvent.addListener(vG, "moveend", function() {//alert(this.centerLatlng.lat() + "," + this.vG.getCenter().lat() + "," + this.centerLatlng.lng() + "," + this.vG.getCenter().lng());
                    if(this.centerLatlng.lat() == this.vG.getCenter().lat() && this.centerLatlng.lng() == this.vG.getCenter().lng()) return;
                    this.centerLatlng = this.vG.getCenter();
                    var point = this.vG.fromLatLngToDivPixel(this.vG.fromContainerPixelToLatLng(new GPoint(0, 0)));
                    with(this.vG) {
                        eventLayer.style.left = point.x + "px";
                        eventLayer.style.top = point.y + "px";
                        originPixel = fromLatLngToDivPixel(originLatlng);
                        if(hotSpot) {
                            hotSpot.setLayerXY(originPixel.x, originPixel.y);
                        }
                        originPixel.x0 = originPixel.x / zoomMultiple[this.zoom];
                        originPixel.y0 = originPixel.y / zoomMultiple[this.zoom];
                    }
                    
                    if(this.vGE && !this.parentMoving) {
                        this.parentMoving = true;
                        this.vGE.setCenter(this.centerLatlng);
                        this.parentMoving = false;
                    }
                    
                    if(!this.checkArea()) return;
                    
                    if(this.vG.hotSpot) {
                        //if(this.zoom < 2)
                            this.vG.hotSpot.loadCellDatas();
                        if(this.vG.hotSpot.infoDlgTemp)
                            if(this.vG.hotSpot.infoDlgTemp.style.display == "block")
                        this.vG.hotSpot.showed = true;
                    }
                    
                    this.vG.sign.refresh(null, true);
                    
                    if(this.vG.showAD)
                        this.loadAD();
                        
                    if(typeof(this.onMoveEnd) == "function")
                        this.onMoveEnd(this.centerLatlng);
                }.bind(this));
                
                GEvent.addListener(vG, "maptypechanged", function() {
                    with(this.vG) {
                        originPixel = fromLatLngToDivPixel(originLatlng);
                        if(hotSpot) {
                            hotSpot.setLayerXY(originPixel.x, originPixel.y);
                        }
                        originPixel.x0 = originPixel.x / zoomMultiple[this.zoom];
                        originPixel.y0 = originPixel.y / zoomMultiple[this.zoom];
                    }
                    if(this.zoomBarCtrl)
                        this.zoomBarCtrl.setCurMapType(this.curMapType, true);
                    this.checkArea();
                    
                    if(this.vG.hotSpot)
                        this.vG.hotSpot.loadCellDatas();
                    
                    this.vG.sign.refresh(null, true);
                    
                    if(this.vG.showAD)
                        this.loadAD();
                }.bind(this));
                
                GEvent.addListener(vG, "zoomend", function(oldLevel, newLevel) {
                    with(this.vG) {
                        originPixel = fromLatLngToDivPixel(originLatlng);
                        if(hotSpot)
                            hotSpot.setLayerXY(originPixel.x, originPixel.y);
                        originPixel.x0 = originPixel.x / zoomMultiple[this.zoom];
                        originPixel.y0 = originPixel.y / zoomMultiple[this.zoom];
                    }
                
                    //if(this.zoom < 2 && this.vG.hotSpot)
                    if(this.vG.hotSpot)
                        this.vG.hotSpot.loadCellDatas();
                        
                    this.vG.sign.refresh(null, true);
                        
                    if(this.vG.showAD)
                        this.loadAD(true);
                        
                    if(typeof this.onZoomChanged == "function") this.onZoomChanged(newLevel);
                }.bind(this));
                
                $attachEvent(vG.div, $EventName.mousewheel, function(event) {
                    if(this.wheelZooming) return;
                    
                    event = event || window.event;
                    cancelBubble(event);
                    this.wheelZooming = true;
                    setTimeout(function(){this.wheelZooming = null}.bind(this), 500);
                    if (event.wheelDelta > 0 || event.detail < 0)
                        this.setZoom(this.zoom - 1);
                    else
                        this.setZoom(this.zoom + 1);
                }.bindWithEvent(this));
                
                GEvent.addListener(vG, "mousemove", function(latlng) {
                    this.pointer = latlng;
                    if(this.tipShowed) {
                        var point = this.fromLatLngToDivPixel(latlng);
                        this.tip.style.left = point.x + 14 + "px";
                        this.tip.style.top = point.y + "px";
                    }
                    if(this.logoSpan)
                        this.logoSpan.innerHTML = getFmtGis(latlng);
                }.bind(vG));

                if(!this.unBindResize) {
                    $attachEvent(window, $EventName.resize, this.checkResize.bind(this));
                }
                
                $attachEvent(vG.eventLayer, $EventName.mouseup, vG.popMenuEvent.bindWithEvent(vG));
                
                GEvent.addListener(vG, "click", function(overlay, latlng) {
                    if(!document.hasFocus()) window.focus();
//                    this.oval.latlngs[this.oval.latlngs.length] = latlng;
//                    this.oval.redraw(true);
                }.bind(this));
            },
            startDrawEntity : function() {
                if(this.onDrawEntity) return;
                this.onDrawEntity = true;
                var polygon = new Draw.Polygon([], '#FFFA73', 1, 1, '#68D5F4', 0.2);
                this.vG.addOverlay(polygon);
                this.editor = new Draw.Polygon.Edit(this.vG);
                this.editor.start(polygon);
                this.vG.curPolygon = polygon;
            },
            showBusLine : function(b, d, s, e, t) {
                AjaxSend({busId : b, direction: d, startIndex: s, endIndex: e, toStationId: (t ? t : "")}, "Data.DataLoader.getBusLine", this.showBus.bind(this));
            },
            showBus : function(value, context) {
                if(value == "") return;
                var a = getValue(value, "line").split(";"), b, c, latlng, busEntity;
                var toStationId = getValue(value, "toStationId");
                
                for(var i=0,len=a.length; i<len; i++) {
                    b = a[i].split("|");
                    if(!this.bus[b[0]]) {
                        c = b[2].split(",");
                        latlng = new GLatLng(parseFloat(c[0]), parseFloat(c[1]));
                        this.bus[b[0]] = new Entity(latlng, "<img src='/images/bus.gif' width='19' height='29' alt='" + this.fmtBusNO(b[1]) + "' />", 10, 29);
                        this.busLayer.addOverlay(this.bus[b[0]]);
                    }
                    if((b[0] + "") == toStationId)
                        this.vG.setCenter(latlng);
                }
            },
            removeAllBus : function() {
                this.busLayer.removeAll();
                this.bus = [];
            },
            fmtBusNO : function(busNO) {
                if(busNO.match(/^\d+$/))
                    return busNO + "路";
                return busNO;
            },
            loadAD: function(ref) {
                var zoom = this.zoom, vG = this.vG;
                
                if(vG.mapType != "MODEL" || ref) {
                    var ad;
                    while(ad = vG.adList.pop()) {
                        vG.removeOverlay(ad);
                    }
                    if(vG.mapType != "MODEL")
                        return;
                }
                
                var bound = this.getBounds();
                AjaxSend(bound, "Data.DataLoader.getADList", function(value, context) {
                    var len;
                    var curList = ",";
                    if(value != "") {
                        var t = value.split("__|;"), p, div, ad, img;
                        len = t.length;
                        for(var i=0; i<len; i++) {
                            if(t[i] == "") break;
                            p = t[i].split("_;");
                            curList += p[0] + ",";
                            if($("ad_" + p[0])) continue;
                            div = $C("div", {id: "ad_" + p[0]});
                            div.style.position = "absolute"
                            if(p[1].match(/\.(jpg|jpeg|gif|bmp|png)\s*$/)) {
                                if(p[5] != "") {
                                    ad = $To($C("a", {href: p[5], target: "_blank"}), div);
                                    img = $To($C("img", {src: "/uploadfiles/guanggao/" + p[1]}), ad);
                                }else {
                                    ad = img = $To($C("img", {src: "/uploadfiles/guanggao/" + p[1]}), div);
                                }
                                
                                img.style.border = "none";
                                if(p[6] != "")
                                    img.title = p[6];
                                if(isNumber(p[3]))
                                    img.style.width = p[3] / Math.pow(2, zoom) + "px";
                                if(isNumber(p[4]))
                                    img.style.height = p[4] / Math.pow(2, zoom) + "px";
                            }else if(p[1].match(/\.swf\s*$/)){
                                div.innerHTML = getSwfStr("/uploadfiles/guanggao/" + p[1], (isNumber(p[3]) ? (p[3] / Math.pow(2, zoom)) : 100), (isNumber(p[4]) ? (p[4] / Math.pow(2, zoom)) : 100), p[0], true);
                                ad = div.getElementsByTagName("object")[0];
                            }
                            
                            if(p[2].match(/^-?\d+\.\d+,-?\d+\.\d+$/)) {
                                var l = p[2].split(",");
                                ad = new Entity(new GLatLng(parseFloat(l[0]), parseFloat(l[1])), div, 0, 0);
                                vG.addOverlay(ad);
                                ad.rid = p[0];
                                vG.adList.push(ad);
                            }
                        }
                    }
                    len = vG.adList.length;
                    for(var i=0; i<len;) {
                        if(curList.indexOf(',' + vG.adList[i].rid + ',') < 0) {
                            vG.removeOverlay(vG.adList[i]);
                            vG.adList.splice(i, 1);
                            len --;
                        }else i ++;
                    }
                });
            },
            getBounds : function() {
                var bound = {}, vG = this.vG;
                bound.mapType = vG.mapType;
                if(vG.mapType == "MODEL") {
                    var width = vG.mapSize.width, height = vG.mapSize.height;
                    bound.left = vG.fromContainerPixelToLatLng(new GPoint(0, 0)).lng();
                    bound.top = vG.fromContainerPixelToLatLng(new GPoint(width, 0)).lat();
                    bound.right = vG.fromContainerPixelToLatLng(new GPoint(width, height)).lng();
                    bound.bottom = vG.fromContainerPixelToLatLng(new GPoint(0, height)).lat();
                }else {
                    var bounds = vG.getBounds();
	                var southWest = bounds.getSouthWest();
                    var northEast = bounds.getNorthEast();
                    bound.left=southWest.lng();
                    bound.bottom=southWest.lat();
                    bound.right=northEast.lng();
                    bound.top=northEast.lat();
                }
                
                return bound;
            }
        }
    }
}

Def_AutoGis();
