var map = null;
var dictMarkers = null;
var dictMarkersTrack = null;
var dictLine = null;
var isMarkerClick = false;  // 是否是点击标注（点击标注[先触发]也会引发点击地图的事件）
var rightClickMarker = null;
var langTxt = null;
var disTool = null;  // 测距工具
var loaded = false; // 是否已加载地图

function reportExpMsg(e)
{
    window.external.BMap_Exp("行号：" + e.number + "\n消息：" + e.message + "\n描述：" + e.description);
}

function setCookie(name, value)
{
    try
    {
        var expdate = new Date(2099, 12, 31);
        document.cookie = name + "=" + value.toString() + ";expires=" + expdate.toGMTString();
    }
    catch (err)
    {
    }
}
function getCookie(name)
{
    var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
    if (arr != null)
    {
        return unescape(arr[2]);
    }
    return null;
}
function createDevMenu()
{    
    var menuDev = new BMap.ContextMenu();
    var menuiZoomIn = new BMap.MenuItem(langTxt.zoomIn, devMenuiZoomIn);
    var menuiZoomOut = new BMap.MenuItem(langTxt.zoomOut, devMenuiZoomOut);
    var menuiRemove = new BMap.MenuItem(langTxt.remove, devMenuiRemove);
    menuDev.addItem(menuiZoomIn);
    menuDev.addItem(menuiZoomOut);
    menuDev.addItem(menuiRemove);

    menuDev.addEventListener("open", markerMenuOpening);
    return menuDev;
}
function markerMenuOpening(e)
{
    //event{type, target, point, pixel}
    var menu = e.target;
    menu.getItem(0).setText(langTxt.zoomIn);
    menu.getItem(1).setText(langTxt.zoomOut);
    menu.getItem(2).setText(langTxt.remove);
        if (rightClickMarker != null)
        {
            if (rightClickMarker.markerType == "Device")
            {
                menu.getItem(2).enable();
            }
            else 
            {
                if (rightClickMarker.canEdit)
                {
                    menu.getItem(2).enable();
                }
                else
                {
                    menu.getItem(2).disable();
                }
            }
        }     
}
function createMapMenu()
{
    var menu = new BMap.ContextMenu();
    var menui = new BMap.MenuItem(langTxt.removeAllCar, mapMenuiClearCar);
    menu.addItem(menui);
    menu.addEventListener("open", function ()
    {
        menu.getItem(0).setText(langTxt.removeAllCar);
    });
    return menu;
}
function addAdvMarker(opt)
{
    var marOpt = { map: map,markerType:opt.markerType, haveIcon: opt.haveIcon, iconFile: opt.iconFile, iconSize: opt.size,
        lnglat: opt.lnglat, clickable: opt.clickable,
        title: opt.title, canMove: opt.moveable, withLabel: opt.withLabel, offset: opt.offset, withTrack: opt.withTrack
    };
    var lblOpt = { text: opt.text, offset: opt.txtOffset, lnglat: opt.lnglat, style: opt.txtStyle };
    var umarker = new LabelMarker(opt.id, marOpt, lblOpt);
    return umarker;
}
 
function showInfoWin(infoHTML, lng, lat)
{
    var infoWin = new BMap.InfoWindow(infoHTML);
    infoWin.enableCloseOnClick();
    map.openInfoWindow(infoWin, new BMap.Point(parseFloat(lng), parseFloat(lat)));
    return infoWin;
}

function map_click(e)   // e:{type, target, point, pixel, overlay}
{
    if (isMarkerClick)
    {
        isMarkerClick = false;
        return;
    }
    window.external.BMap_Click("click", e.point.lng.toString(), e.point.lat.toString());
}
function map_rightClick(e)  // e:{type, target, point, pixel, overlay}
{
    window.external.BMap_RightClick("click",e.point.lng.toString(), e.point.lat.toString());
}

function marker_click(marker)
{
    isMarkerClick = true;
    window.external.BMap_MarkerClick(marker._id.toString(),marker.markerType, marker.getPosition().lng,marker.getPosition().lat);
}
function marker_rightClick(marker)
{
    rightClickMarker = marker;
    window.external.BMap_MarkerRightClick(marker._id.toString(), marker.markerType, marker.getPosition().lng, marker.getPosition().lat);
}
function marker_mouseover(marker)
{
    window.external.BMap_MarkerMouseover(marker._id.toString(), marker.markerType, marker.getPosition().lng, marker.getPosition().lat);
}
function marker_mouseout(marker)
{
    window.external.BMap_MarkerMouseout(marker._id.toString(), marker.markerType, marker.getPosition().lng, marker.getPosition().lat);
}
function marker_dragEnd(marker)
{
    window.external.BMap_MarkerDragEnd(marker._id.toString(), marker.markerType, marker.getPosition().lng, marker.getPosition().lat);
}

function devMenuiZoomIn()
{
    var zoom = map.getZoom() + 3
    if (zoom > 19)
    {
        zoom = 19;
    }
    map.setZoom(zoom);
}
function devMenuiZoomOut()
{
    var zoom = map.getZoom() - 3
    if (zoom < 0)
    {
        zoom = 0;
    }
    map.setZoom(zoom);
}
function devMenuiRemove()
{ 
    bmap_markerRemove(rightClickMarker._id);
    window.external.BMap_MarkerRemove(rightClickMarker._id, rightClickMarker.markerType);
}
function mapMenuiClearCar()
{
    bmap_markerClear();
    
    window.external.BMap_MarkerClear("Device");
}

function CHClick(markerID, CHIndex)
{
    try { }
    catch (e)
    {
        throw e;
    }
    window.external.BMap_UserDefineEvent("OpenVideo", markerID.toString(), CHIndex.toString());
}

function LocationWeb(lng, lat)
{
    try { }
    catch (e)
    {
        throw e;
    }
    window.external.BMap_UserDefineEvent("LocationWeb", lng.toString(), lat.toString());
}

function lineStartMarClick(line)
{
    bmap_lineRemove(line.id);
    window.external.BMap_LineRemoved(line.id);
}
function lineStartMarRClick(line)
{
}
 