var ml=ml||{};function bingMapReady(){if(ml.map.bingMapReady=!0,ml.map.documentReady){var event=document.createEvent("Event");event.initEvent("bingMapsLoaded",!0,!0),document.dispatchEvent(event)}}$(document).ready((function(){if(ml.map.documentReady=!0,ml.map.bingMapReady){var event=document.createEvent("Event");event.initEvent("bingMapsLoaded",!0,!0),document.dispatchEvent(event)}})),ml.map=function(elem){this.mapElement=elem},ml.map.prototype={constructor:ml.map,isDrawing:!1,drawPolygon:function(cb){var line,isDrawing=!1,_this=this;$("#"+this.mapElement).css("cursor","crosshair");var startDrawingHandler=Microsoft.Maps.Events.addHandler(this.map,"mousedown",(function(e){try{var point=new Microsoft.Maps.Point(e.getX(),e.getY()),loc=e.target.tryPixelToLocation(point);(line=new Microsoft.Maps.Polyline([loc,loc])).setOptions({strokeColor:"#B40101",strokeThickness:2}),_this.geoLayer.add(line),isDrawing=!0,_this.map.setOptions({disablePanning:!0,disableZooming:!0})}catch(x){console.error(x)}})),stopDrawingHandler=Microsoft.Maps.Events.addHandler(this.map,"mouseup",(function(){var locs=line.getLocations();locs.push(locs[0]),line.setLocations(locs),isDrawing=!1,_this.map.setOptions({disablePanning:!1,disableZooming:!1}),$("#"+_this.mapElement).css("cursor","default"),Microsoft.Maps.Events.removeHandler(startDrawingHandler),Microsoft.Maps.Events.removeHandler(activeDrawingHandler),Microsoft.Maps.Events.removeHandler(stopDrawingHandler),locs.push(locs[0]);var shapeString="POLYGON (("+locs.map((function(loc){return loc.longitude+" "+loc.latitude})).join()+"))";cb(shapeString,locs)})),activeDrawingHandler=Microsoft.Maps.Events.addHandler(this.map,"mousemove",(function(e){if(isDrawing)try{var point=new Microsoft.Maps.Point(e.getX(),e.getY()),loc=e.target.tryPixelToLocation(point),locs=line.getLocations();locs.push(loc),line.setLocations(locs)}catch(x){}}))},freezeMap:function(){this.map.setOptions({disablePanning:!0,disableZooming:!0})},unfreezeMap:function(){this.map.setOptions({disablePanning:!1,disableZooming:!1})},initMap:function(initInfoBox,options){(options=options||{}).credentials=mlVars.mapCredentials;try{this.map=new Microsoft.Maps.Map(document.getElementById(this.mapElement),options)}catch(e){console.error("Instantiation of new Microsoft.Maps.Map() failed"),console.error(e)}this.pins=[],this.clusterLayer=null;try{this.pinLayer=new Microsoft.Maps.Layer}catch(e){console.error("Instantiation of new Microsoft.Maps.Layer() failed"),console.error(e)}initInfoBox&&(this.infobox=new Microsoft.Maps.Infobox(this.map.getCenter(),{showCloseButton:!1,visible:!1,typeName:"mini"}),this.infobox.setMap(this.map))},markerIcon:function(fill,stroke,text,showHeart){var width;if(showHeart){switch(text.length){case 4:width=54;break;case 5:case 6:width=60;break;default:width=50}text="♥ "+text}else switch(text.length){case 4:width=42;break;case 5:width=48;break;case 6:width=54;break;default:width=36}var pointerLeft=width/2-4,pointerRight=width/2+4,pointerX=width/2,center=width/2,c=document.createElement("canvas");c.width=60,c.height=28;var ctx=c.getContext("2d");return ctx.beginPath(),ctx.moveTo(5,0),ctx.arcTo(width,0,width,20,5),ctx.arcTo(width,20,pointerRight,20,5),ctx.lineTo(pointerRight,20),ctx.lineTo(pointerX,25),ctx.lineTo(pointerLeft,20),ctx.arcTo(0,20,0,0,5),ctx.arcTo(0,0,width,0,5),ctx.closePath(),ctx.fillStyle=fill,ctx.fill(),ctx.strokeStyle=stroke,ctx.stroke(),ctx.font="12px Arial",ctx.fillStyle="#ffffff",ctx.textAlign="center",ctx.fillText(text,center,14),{anchor:{x:center,y:20},url:c.toDataURL()}},addPin:function(id,position,options,metadata,events){var location=new Microsoft.Maps.Location(position[0],position[1]),pin=new Microsoft.Maps.Pushpin(location,options);if(metadata&&(pin.metaData=metadata),events)for(var keys=Object.keys(events),i=0,len=keys.length;i<len;i++)Microsoft.Maps.Events.addHandler(pin,keys[i],events[keys[i]]);return this.pins.push(pin),pin},clearPins:function(){this.pins=[],this.clusterLayer&&this.clusterLayer.clear(),this.pinLayer&&this.pinLayer.clear()},showAllPins:function(){this.pinLayer=new Microsoft.Maps.Layer,this.pinLayer.add(this.pins),this.map.layers.insert(this.pinLayer)},getBoundingBox:function(){var elemHeight=this.map.getHeight(),elemWidth=this.map.getWidth(),topLeftPixel=new Microsoft.Maps.Point(25,25),bottomRightPixel=new Microsoft.Maps.Point(elemWidth,elemHeight),topLeft=this.map.tryPixelToLocation(topLeftPixel,Microsoft.Maps.PixelReference.control),bottomRight=this.map.tryPixelToLocation(bottomRightPixel,Microsoft.Maps.PixelReference.control);return{LULat:topLeft.latitude,LULong:topLeft.longitude,RLLat:bottomRight.latitude,RLLong:bottomRight.longitude}},fitToBounds:function(LULat,LULong,RLLat,RLLong){var bounds=Microsoft.Maps.LocationRect.fromEdges(LULat,LULong,RLLat,RLLong);this.map.setView({bounds:bounds})},zoomToPins:function(){var options={},pKeys=Object.keys(this.pins),l=pKeys.length;if(l>1){for(var maxLat=-85,minLat=85,maxLon=-180,minLon=180,locs=[],i=0;i<l;i++){var loc=this.pins[pKeys[i]].getLocation();locs.push(loc),maxLat=Math.max(loc.latitude,maxLat),minLat=Math.min(loc.latitude,minLat),maxLon=Math.max(loc.longitude,maxLon),minLon=Math.min(loc.longitude,minLon)}var bounds=Microsoft.Maps.LocationRect.fromLocations(locs);this.map.setView({bounds:bounds})}else options.center=this.pins[pKeys[0]].getLocation(),this.map.setView(options)},zoomToPolygon:function(geo){var l=geo.length,bounds=null;if(l>1){for(var maxLat=-85,minLat=85,maxLon=-180,minLon=180,i=0;i<l;i++){var lat=Array.isArray(geo[i])?geo[i][1]:geo[i].latitude,lon=Array.isArray(geo[i])?geo[i][0]:geo[i].longitude;maxLat=Math.max(lat,maxLat),minLat=Math.min(lat,minLat),maxLon=Math.max(lon,maxLon),minLon=Math.min(lon,minLon)}bounds=Microsoft.Maps.LocationRect.fromEdges(maxLat,minLon,minLat,maxLon)}this.map.setView({bounds:bounds})},getPolygonBounds:function(geo){return Microsoft.Maps.LocationRect.fromShapes(Microsoft.Maps.GeoJson.read({type:"Polygon",coordinates:[geo]}))},createGeoLayer:function(cb){var _this=this;Microsoft.Maps.loadModule("Microsoft.Maps.GeoJson",(function(){_this.geoLayer=new Microsoft.Maps.Layer,_this.map.layers.insert(_this.geoLayer),cb&&cb()}))},updateGeoLayer:function(geoObject){var shape=Microsoft.Maps.GeoJson.read(geoObject,{polygonOptions:{fillColor:"transparent",strokeColor:"#B40101",strokeThickness:2}});this.geoLayer.add(shape)},clearGeoLayer:function(){this.geoLayer&&this.geoLayer.clear()},createClusterLayer:function(usePins,callback){var _this=this;Microsoft.Maps.loadModule("Microsoft.Maps.Clustering",(function(){_this.clusterLayer=new Microsoft.Maps.ClusterLayer(_this.pins,{gridSize:80,clusteredPinCallback:_this.createCustomClusteredPinCallback}),_this.map.layers.insert(_this.clusterLayer),usePins&&_this.clusterLayer.setPushpins(_this.pins),callback&&"function"==typeof callback&&callback()}))},updateClusterLayer:function(){this.clusterLayer?this.clusterLayer.setPushpins(this.pins):this.createClusterLayer(!0)},createCustomClusteredPinCallback:function(cluster){if(cluster){Microsoft.Maps.Events.addHandler(cluster,"click",(function(e){var zoomLevel=e.target.layer._map.getZoom()+1,location=e.target.getLocation();e.target.layer._map.setView({zoom:zoomLevel,center:location})}));var radius,clusterSize=cluster.containedPushpins.length,svg=['<svg xmlns="http://www.w3.org/2000/svg" width="',2*(radius=clusterSize<=100?17:clusterSize<=50?22:clusterSize<=100?27:clusterSize<=150?32:37),'" height="',2*radius,'">','<circle cx="',radius,'" cy="',radius,'" r="',radius,'" fill="',"rgba(20, 180, 20, 0.5)",'"/>','<circle cx="',radius,'" cy="',radius,'" r="',radius-7,'" fill="',"rgba(20, 180, 20, 0.5)",'"/>',"</svg>"];cluster.setOptions({icon:svg.join(""),anchor:new Microsoft.Maps.Point(radius,radius),textOffset:new Microsoft.Maps.Point(0,radius-8)})}},coordinateStringToGeoJSON:function(str){var coords,geoObject={type:str.split(" ")[0]};"MULTIPOLYGON"===geoObject.type?(geoObject.coordinates=[[]],(coords=(coords=str.split(geoObject.type)[1].trim()).substring(1,coords.length-1).trim()).split("),").forEach((function(polygon){polygon=polygon.replace(/[{()}]/g,"").trim();var polygonCoords=[];polygon.split(",").forEach((function(point){var coords=point.trim().split(" ");polygonCoords.push([coords[0],coords[1]])})),geoObject.coordinates[0].push(polygonCoords)}))):(geoObject.coordinates=[],(coords=(coords=str.split(geoObject.type)[1].trim()).substring(1,coords.length-1).trim()).split("),").forEach((function(polygon){polygon=polygon.replace(/[{()}]/g,"").trim();var polygonCoords=[];polygon.split(",").forEach((function(point){var coords=point.trim().split(" ");polygonCoords.push([coords[0],coords[1]])})),geoObject.coordinates.push(polygonCoords)})));return geoObject}};