
var VEShapeToolSelected='VEShapeToolSelected';function VEShapeToolPluginManager(){var me=this;me.map=null;me.currentFilterShape=null;me.VEShapeToolCurrent=null;me.CurrentShapeType=null;me.DontDeleteShapeOnComplete=false;me.ConfDeleteOnAttach=true;me.CurrentShapeToolChange=function(result){pluginManager.AttachTool(result);};GlobalLookup.setSingleton('CurrentShapeTool',true);GlobalLookup.addListener('CurrentShapeTool',this.CurrentShapeToolChange);me.SetCurrentShape=function(type,theShape){theShape.HideIcon(true);theShape.SetFillColor(ShapeFillColor());theShape.SetLineColor(ShapeLineColor());var lineWidth=Math.round(2);theShape.SetLineWidth(lineWidth);pluginManager.CurrentShapeType=type;pluginManager.currentFilterShape=theShape;};me.InitializeCurrentShape=function(type,result){if(result){pluginManager.SetCurrentShape(type,result);pluginManager.map.AddShape(result);}};me.ClearCurrentShape=function(){ShapEditControlUpdateShape(new Array(),'',VEUpdateShapeCallback);if(me.currentFilterShape){me.map.DeleteShape(pluginManager.currentFilterShape);me.currentFilterShape=null;}
me.DetachTool();if(pluginManager.VEShapeToolCurrent){me.DetachTool();}
me.OnClearCurrentShape();};me.OnClearCurrentShape=function(){};me.Initialize=function(theVEMap,InitialShapeType,InitialShape){me.map=theVEMap;me.InitializeCurrentShape(InitialShapeType,InitialShape);};me.AddButtonsToToolbar=function(theVEMap,InitialShapeType,InitialShape){$('#MSVE_navAction_ObliqueMapView').css({'width':'auto'});$('#MSVE_navAction_showLabels').hide();$('#MSVE_navAction_separator2').hide();$('#MSVE_navAction_AerialMapStyle').css({'width':'auto'});$('#MSVE_navAction_RoadMapStyle').css({'width':'auto'});$('#MSVE_navAction_topBar').append('<div id="PenTool" class="MSVE_MapStyle PluginTool PenTool DrawTool"  style="width:auto;"   title="Select a group of properties by holding the mouse button down while drawing.  Click Done when finished."><a onclick="GlobalLookup.addValue(\'CurrentShapeTool\', new VEShapeToolPen());">Pen</a></div>');$('#MSVE_navAction_topBar').append('<div id="CircleTool" class="MSVE_MapStyle PluginTool CircleTool DrawTool"  style="width:auto;"  title="Select a group of properties by entering a radius value and click on the map to draw a circle.  Click Done when finished."><a onclick="GlobalLookup.addValue(\'CurrentShapeTool\', new VEShapeToolCircle());">Circle</a></div>');$('#MSVE_navAction_topBar').append('<div id="MoveTool" class="MSVE_MapStyle PluginTool MoveTool"  style="width:auto;"  title="Drag and move the drawn shape in the map area.  Click Done when finished."><a onclick="GlobalLookup.addValue(\'CurrentShapeTool\', new VEShapeToolMove());">Move</a></div>');$('#MSVE_navAction_topBar').append('<div id="ClearTool" class="MSVE_MapStyle PluginTool ClearTool"  style="width:auto;"  title="Clear the drawn shape and restore map."><a onclick="pluginManager.ClearCurrentShape();">Clear</a></div>');$('#MSVE_navAction_topBar').append('<div id="DoneTool" class="MSVE_MapStyle PluginTool DoneTool"  style="width:auto;"  title="Click to activate drawing changes."><a onclick="pluginManager.DrawingComplete();">Done</a></div>');$('#MSVE_navAction_topBar div.PluginTool').tooltip({track:true,delay:600,showURL:false,opacity:1,fixPNG:true,extraClass:"bubble",top:-15,left:5});$('#DoneTool').hide();if(!InitialShape){$('#ClearTool').hide();$('#MoveTool').hide();}
else{}};this.AddSubButtonsToToolbar=function(theVEMap,InitialShapeType,InitialShape){$('#MSVE_navAction_ObliqueMapView').css({'width':'auto'});$('#MSVE_navAction_showLabels').hide();$('#MSVE_navAction_separator2').hide();$('#MSVE_navAction_AerialMapStyle').css({'width':'auto'});$('#MSVE_navAction_RoadMapStyle').css({'width':'auto'});var VEToolbar=$('#MSVE_navAction_topBar')
VEToolbar.append('<div id="MoveTool" class="PluginTool MoveTool"  style="width:auto;"  title="Drag and move the drawn shape in the map area. "><input class="PadZingButton MoveTool" type="button"  onclick="GlobalLookup.addValue(\'CurrentShapeTool\', new VEShapeToolMove(true));" value="Move Current Area"/></div>');var OptionsTools=$('#AreaButtons')
OptionsTools.append('<div class="PluginTool MoveTool PadZingButton"  style=""  title="Drag and move the drawn shape in the map area."><input class="PadZingButton MoveTool" type="button"  onclick="GlobalLookup.addValue(\'CurrentShapeTool\', new VEShapeToolMove(true));" value="Move Current Area"/></div>');$('#MSVE_navAction_topBar div.PluginTool').tooltip({track:true,delay:600,showURL:false,opacity:1,fixPNG:true,extraClass:"bubble",top:-15,left:5});$('.DoneTool').hide();if(!InitialShape){$('.ClearTool').hide();$('.MoveTool').hide();}
else{}};this.Status=function(result){$('#VWShapeStatus').html(result);};me.DrawingComplete=function(){pluginManager.DetachTool();if(this.currentFilterShape!=null){if(this.currentFilterShape.GetType()==VEShapeType.Polygon){var arr=pluginManager.currentFilterShape.GetPoints();ShapEditControlUpdateShape(PointsToArray(arr),pluginManager.CurrentShapeType,VEUpdateShapeCallback);}}};me.CreateVEShape=function(theVEShapeType,arr,theShapeToolType){pluginManager.ClearCurrentShape();var theShape=new VEShape(theVEShapeType,arr);theShape.HideIcon(true);theShape.SetFillColor(ShapeFillColor());theShape.SetLineColor(ShapeLineColor());var lineWidth=Math.round(2);theShape.SetLineWidth(lineWidth);pluginManager.InitializeCurrentShape(theShapeToolType,theShape);return theShape;};me.ToolChanged=function(newTool){pluginManager.VEShapeToolCurrent=newTool;}
me.DetachTool=function(){if(pluginManager.VEShapeToolCurrent){if(pluginManager.VEShapeToolCurrent.ToolUnselected){pluginManager.VEShapeToolCurrent.ToolUnselected();}}
$('.VEShapeToolDone').attr("disabled",true);this.map.DetachEvent('ondoubleclick',this.VEDoubleClick);this.map.DetachEvent('onmousedown',this.VEMouseDown);this.map.DetachEvent('onmouseup',this.VEMouseUp);this.map.DetachEvent('onmousemove',this.VEMouseMove);$('#CustomToolOptions').remove();$('.DrawTool').show();if(this.currentFilterShape!=null){$('.ClearTool').show();$('.MoveTool').show();}
else{$('.ClearTool').hide();$('.MoveTool').hide();}
$('.DoneTool').hide();me.VEShapeToolCurrent=null;}
me.AttachTool=function(tool){if(this.VEShapeToolCurrent){this.DetachTool();}
$('.VEShapeToolDone').removeAttr("disabled");if(me.currentFilterShape!=null&&!me.DontDeleteShapeOnComplete){if(!tool.CanEditShapeType(me.CurrentShapeType)){if(me.ConfDeleteOnAttach){alert("The current shape will be deleted");}
me.ClearCurrentShape();}}
$('#MSVE_navAction_topBar').append('<div id="CustomToolOptions" class=""  style="width:auto;"  title="Options"></div>');tool.ToolSelected(this.map);pluginManager.VEShapeToolCurrent=tool;me.map.AttachEvent('ondoubleclick',me.VEDoubleClick);me.map.AttachEvent('onmousedown',me.VEMouseDown);me.map.AttachEvent('onmouseup',me.VEMouseUp);me.map.AttachEvent('onmousemove',me.VEMouseMove);$('.DrawTool').hide();$('.MoveTool').hide();$('.ClearTool').show();$('.DoneTool').show();};me.VEDoubleClick=function(e){pluginManager.VEShapeToolCurrent.VEDoubleClick(e);};me.VEMouseUp=function(e){pluginManager.VEShapeToolCurrent.VEMouseUp(e);};me.VEMouseDown=function(e){pluginManager.VEShapeToolCurrent.VEMouseDown(e);return true;};me.VEMouseMove=function(e){pluginManager.VEShapeToolCurrent.VEMouseMove(e);};};var pluginManager=new VEShapeToolPluginManager();function VEUpdateShapeCallback(s){ClientShapeNotificationFunction(s);}
MaxRadius=50;function VEToolRadiusChanged(e){var currentRadius=$('#CurrentRadius').val();if(currentRadius){if(Number(currentRadius)>MaxRadius){currentRadius=MaxRadius;$('#CurrentRadius').attr("value",currentRadius.toFixed(1));}
if(currentRadius>0){pluginManager.VEShapeToolCurrent.DrawCircle(pluginManager.VEShapeToolCurrent.center,currentRadius);}}};function VEShapeToolCircle(){this.map=null;this.center=null;this.ToolUnselected=function(){var el=document.getElementById("CircleCustomToolOptions");this.map.DeleteControl(el);};this.ToolSelected=function(currentVEMap){this.map=currentVEMap;var el=document.createElement("div");$(el).addClass('MSVE_MapStyle');$(el).css({'top':'0px','left':'310px','border':'0px','position':'absolute','width':'auto','font-size':'7.5pt','color':'white'});if(whichBrs()=='Mozilla'||whichBrs()=='Firefox'){$(el).css({'margin-top':'2px'});}
if(whichBrs()=='Safari'){$(el).css({'margin-top':'4px'});}
el.id="CircleCustomToolOptions";el.innerHTML='Radius: <input onkeyup="VEToolRadiusChanged();" style="width: 30px; height:1.1em;" type="text" name="CurrentRadius" id="CurrentRadius" value="3"/>';this.map.AddControl(el);};this.CanEditShapeType=function(theShapeType){return false;};this.TrackingMouse=false;this.currentPoints=new Array();this.CurrentVEShape=null;this.VEDoubleClick=function(e){return null;};this.VEMouseUp=function(e){this.TrackingMouse=false;};this.VEMouseDown=function(e){this.TrackingMouse=true;this.lastX=e.mapX;this.lastY=e.mapY;this.center=this.map.PixelToLatLong(new VEPixel(e.mapX,e.mapY));this.DrawCircle(this.center,$('#CurrentRadius').val());return true;};this.lastX=null;this.lastY=null;this.Radius=function(e){var current=this.map.PixelToLatLong(new VEPixel(e.mapX,e.mapY));return d=GeoCodeCalc.CalcDistance(this.center.Latitude,this.center.Longitude,current.Latitude,current.Longitude,GeoCodeCalc.EarthRadiusInMiles);};this.DrawCircle=function(theCenter,theRadius){var locs=new Array();if(theCenter){var latin=theCenter.Latitude;var lonin=theCenter.Longitude;var lat1=latin*Math.PI/180.0;var lon1=lonin*Math.PI/180.0;var d=theRadius/3956;var x;for(x=0;x<=360;x++){var tc=(x/90)*Math.PI/2;var lat=Math.asin(Math.sin(lat1)*Math.cos(d)+Math.cos(lat1)*Math.sin(d)*Math.cos(tc));lat=180.0*lat/Math.PI;var lon;if(Math.cos(lat1)==0){lon=lonin;}
else{lon=((lon1-Math.asin(Math.sin(tc)*Math.sin(d)/Math.cos(lat1))+Math.PI)%(2*Math.PI))-Math.PI;}
lon=180.0*lon/Math.PI;var loc=new VELatLong(lat,lon);locs.push(loc);}
if(this.CurrentVEShape==undefined){this.CurrentVEShape=new VEShape(VEShapeType.Polygon,locs);this.CurrentVEShape.HideIcon(true);this.CurrentVEShape.Center=theCenter;pluginManager.SetCurrentShape('Circle',this.CurrentVEShape);var lineColor=new VEColor(0,0,0,1);this.CurrentVEShape.SetLineColor(lineColor);var lineWidth=Math.round(2);this.CurrentVEShape.SetLineWidth(lineWidth);this.map.AddShape(this.CurrentVEShape);}
else{this.CurrentVEShape.SetPoints(locs);}}};this.lastDrawnRadius=0;function VEShapeToolTemplate(){this.map=null;this.ToolSelected=function(currentVEMap){this.map=currentVEMap;};this.currentPoints=new Array();this.CurrentVEShape=null;this.VEDoubleClick=function(e){};this.VEMouseUp=function(e){};this.VEMouseDown=function(e){};this.VEMouseMove=function(e){};};this.drawDistance=.1;this.VEMouseMove=function(e){if(this.TrackingMouse){var length=this.Radius(e);if(length>MaxRadius){length=MaxRadius;}
if((this.lastDrawnRadius+this.drawDistance)<length||length<(this.lastDrawnRadius-this.drawDistance)){$('#CurrentRadius').attr("value",length.toFixed(1));this.lastDrawnRadius=length;this.DrawCircle(this.center,length);}}};};var GeoCodeCalc={};GeoCodeCalc.EarthRadiusInMiles=3956.0;GeoCodeCalc.EarthRadiusInKilometers=6367.0;GeoCodeCalc.ToRadian=function(v){return v*(Math.PI/180);};GeoCodeCalc.DiffRadian=function(v1,v2){return GeoCodeCalc.ToRadian(v2)-GeoCodeCalc.ToRadian(v1);};GeoCodeCalc.CalcDistance=function(lat1,lng1,lat2,lng2,radius){return radius*2*Math.asin(Math.min(1,Math.sqrt((Math.pow(Math.sin((GeoCodeCalc.DiffRadian(lat1,lat2))/2.0),2.0)+Math.cos(GeoCodeCalc.ToRadian(lat1))*Math.cos(GeoCodeCalc.ToRadian(lat2))*Math.pow(Math.sin((GeoCodeCalc.DiffRadian(lng1,lng2))/2.0),2.0)))));};function VEShapeToolSubscription(){var me=this;me.map=null;me.center=null;me.ToolUnselected=function(){me.CurrentVEShape=null;me.currentPoints=new Array();};me.ToolSelected=function(currentVEMap){me.map=currentVEMap;};me.CanEditShapeType=function(theShapeType){return false;};me.TrackingMouse=false;me.currentPoints=new Array();me.CurrentVEShape=null;me.VEDoubleClick=function(e){return null;};me.VEMouseUp=function(e){this.TrackingMouse=false;};me.VEMouseDown=function(e){me.TrackingMouse=true;me.lastX=e.mapX;me.lastY=e.mapY;me.center=me.map.PixelToLatLong(new VEPixel(e.mapX,e.mapY));me.DrawCircle(me.center);pluginManager.DrawingComplete();return true;};me.lastX=null;me.lastY=null;me.Radius=function(){var SubRadius2=$('#SubscriptionOptions span.Radius2 input')[0];var SubRadius4=$('#SubscriptionOptions span.Radius4 input')[0];var radius=4;return radius;};me.DrawCircle=function(theCenter){me.DrawCircleWRadius(theCenter,me.Radius())};me.DrawCircleWRadius=function(theCenter,theRadius){var locs=new Array();if(theCenter){var latin=theCenter.Latitude;var lonin=theCenter.Longitude;var lat1=latin*Math.PI/180.0;var lon1=lonin*Math.PI/180.0;var d=theRadius/3956;var x;for(x=0;x<=360;x++){var tc=(x/90)*Math.PI/2;var lat=Math.asin(Math.sin(lat1)*Math.cos(d)+Math.cos(lat1)*Math.sin(d)*Math.cos(tc));lat=180.0*lat/Math.PI;var lon;if(Math.cos(lat1)==0){lon=lonin;}
else{lon=((lon1-Math.asin(Math.sin(tc)*Math.sin(d)/Math.cos(lat1))+Math.PI)%(2*Math.PI))-Math.PI;}
lon=180.0*lon/Math.PI;var loc=new VELatLong(lat,lon);locs.push(loc);}
if(me.CurrentVEShape==undefined){me.CurrentVEShape=new VEShape(VEShapeType.Polygon,locs);me.CurrentVEShape.HideIcon(false);me.CurrentVEShape.SetCustomIcon("<div>Test<div>");me.CurrentVEShape.Center=theCenter;me.CurrentVEShape.Radius=theRadius;pluginManager.SetCurrentShape('Circle',me.CurrentVEShape);var lineColor=new VEColor(0,0,0,1);me.CurrentVEShape.SetLineColor(lineColor);var lineWidth=Math.round(2);me.CurrentVEShape.SetLineWidth(lineWidth);me.map.AddShape(this.CurrentVEShape);}
else{me.CurrentVEShape.Center=theCenter;me.CurrentVEShape.Radius=theRadius;me.CurrentVEShape.SetPoints(locs);}}};me.VEMouseMove=function(e){return true;};};function VEShapeToolPen(){this.map=null;this.ToolSelected=function(currentVEMap){this.map=currentVEMap;};this.CanEditShapeType=function(theShapeType){return false;};this.TrackingMouse=false;this.currentPoints=new Array();this.CurrentVEShape=null;this.VEDoubleClick=function(e){pluginManager.DrawingComplete();};this.VEMouseUp=function(e){this.TrackingMouse=false;};this.VEMouseDown=function(e){this.TrackingMouse=true;var ll=this.map.PixelToLatLong(new VEPixel(e.mapX,e.mapY));var lat=ll.Latitude;var lon=ll.Longitude;if(this.CurrentVEShape==undefined){this.currentPoints=[ll,ll,ll];this.CurrentVEShape=new VEShape(VEShapeType.Polygon,[ll,ll,ll]);this.CurrentVEShape.HideIcon(true);pluginManager.SetCurrentShape('Polygon',this.CurrentVEShape);var lineColor=new VEColor(0,0,0,1);this.CurrentVEShape.SetLineColor(lineColor);var lineWidth=Math.round(2);this.CurrentVEShape.SetLineWidth(lineWidth);this.map.AddShape(this.CurrentVEShape);this.SetLastPoint(e);}
else{this.currentPoints[this.currentPoints.length]=ll;this.CurrentVEShape.SetPoints(this.currentPoints);this.SetLastPoint(e);}
return true;};this.lastX=-1000;this.lastY=-1000;this.SetLastPoint=function(e){this.lastX=e.mapX;this.lastY=e.mapY;};this.VEMouseMove=function(e){if(this.TrackingMouse){if(this.CurrentVEShape!=undefined){var difX=e.mapX-this.lastX;var difY=e.mapY-this.lastY;var length=Math.sqrt(Math.pow(difX,2)+Math.pow(difY,2));if(length>3){var ll=this.map.PixelToLatLong(new VEPixel(e.mapX,e.mapY));this.currentPoints[this.currentPoints.length]=ll;this.CurrentVEShape.SetPoints(this.currentPoints);this.SetLastPoint(e);}}}};};function VEShapeToolMove(FinishOnRelease){var me=this;me.map=null;me.FinishOnRelease=FinishOnRelease;me.vwMouseTracking=false;me.ShapeDragStartPoint=null;me.ToolSelected=function(currentVEMap){this.map=currentVEMap;};me.CanEditShapeType=function(theShapeType){if(theShapeType=="Polygon"){return true;}else if(theShapeType=="Circle"){return true;}else{return false;}};me.VEMoveToolHandleGrabs=function(x,y){var sec_vemap=this.map;var ll=sec_vemap.PixelToLatLong(new VEPixel(x,y));if(PointInShape(pluginManager.currentFilterShape,ll)){this.ShapeDragStartPoint=ll;}};me.currentPoints=new Array();me.CurrentVEShape=null;me.VEDoubleClick=function(e){};me.VEMouseUp=function(e){if(me.ShapeDragStartPoint!=null&&me.FinishOnRelease){pluginManager.DrawingComplete();}
me.ShapeDragStartPoint=null;};me.VEMouseDownMapX=null;me.VEMouseDownMapY=null;me.VEMouseDown=function(e){VEMouseDownMapX=e.mapX;VEMouseDownMapY=e.mapY;if(e.leftMouseButton){me.VEMoveToolHandleGrabs(e.mapX,e.mapY);}
return true;};me.VEMouseMove=function(e){if(me.ShapeDragStartPoint!=null){var ll=me.map.PixelToLatLong(new VEPixel(e.mapX,e.mapY));var dLat=me.ShapeDragStartPoint.Latitude-ll.Latitude;var dLong=me.ShapeDragStartPoint.Longitude-ll.Longitude;var arr=pluginManager.currentFilterShape.GetPoints();for(var n=0;n<arr.length;n++){arr[n].Latitude=arr[n].Latitude-dLat;arr[n].Longitude=arr[n].Longitude-dLong;}
if(pluginManager.currentFilterShape.Center){pluginManager.currentFilterShape.Center.Latitude=pluginManager.currentFilterShape.Center.Latitude-dLat;pluginManager.currentFilterShape.Center.Longitude=pluginManager.currentFilterShape.Longitude-dLong;}
me.ShapeDragStartPoint=ll;pluginManager.currentFilterShape.SetPoints(arr);}};};function VEShapeToolTest(){var me=this;me.map=null;me.ToolSelected=function(currentVEMap){this.map=currentVEMap;};me.VEDoubleClick=function(e){return false;};me.VEMouseUp=function(e){return false;};me.VEMouseDown=function(e){var test;return false;};me.VEMouseMove=function(e){return false;};};function PointInShape(shape,ll){var arr=shape.GetPoints();var minLat=arr[0].Latitude;var maxLat=arr[0].Latitude;var minLong=arr[0].Longitude;var maxLong=arr[0].Longitude;for(var n=1;n<arr.length-1;n++){if(arr[n].Latitude<minLat){minLat=arr[n].Latitude;}
if(arr[n].Latitude>maxLat){maxLat=arr[n].Latitude;}
if(arr[n].Longitude<minLong){minLong=arr[n].Longitude;}
if(arr[n].Longitude>maxLong){maxLong=arr[n].Longitude;}}
return(ll.Latitude>=minLat&&ll.Latitude<=maxLat&&ll.Longitude>=minLong&&ll.Longitude<=maxLong);}
function PointsToArray(latLongArr){var arr=new Array();for(var n=0;n<latLongArr.length;n++){arr[arr.length]=latLongArr[n].Longitude;arr[arr.length]=latLongArr[n].Latitude;}
return arr;}