
﻿
function MapManager(){this.Map=null;this.DirectionsLocationId=null;this.SearchLayer=null;var that=this;this.GetMap=function(){var map=new VEMap('Map');var defaultCenter=(DEFAULT_COUNTRY=='Canada')?new VELatLong(56.1666,-100.683333):new VELatLong(40.71395582628604,-96.0205078125);map.LoadMap(defaultCenter,4,VEMapStyle.Road,false,VEMapMode.Mode2D,false);document.getElementById('MSVE_obliqueNotification').style.visibility="hidden";if(DEFAULT_COUNTRY!='USA')
map.SetScaleBarDistanceUnit(VEDistanceUnit.Kilometers);map.AttachEvent("onchangeview",new Function("mapManager.AddLocationPins(searchManager.Locations);"));map.AttachEvent("onclick",searchManager.GetDetails);this.SearchLayer=new VEShapeLayer();map.AddShapeLayer(this.SearchLayer);this.Map=map;if(clientToken!="null")
map.SetClientToken(clientToken);}
this.FindLocation=function(address){var style=this.Map.GetMapStyle();if(style==VEMapStyle.Birdseye||style==VEMapStyle.BirdseyeHybrid){this.Map.SetMapStyle(VEMapStyle.Road)}
this.Map.Find(null,address,null,null,null,null,null,null,null,false,searchManager.VEFindResultsCallback);}
VEShape.prototype.Locations=null;this.AddLocationPins=function(locations){if(locations!=null){var locationShapes=new Array();for(var i=0;i<locations.length;i++){if(locations[i].Location.Latitude==null||locations[i].Location.Longitude==null)
continue;var latlong=new VELatLong();latlong.Latitude=locations[i].Location.Latitude;latlong.Longitude=locations[i].Location.Longitude;var pin=new VEShape(VEShapeType.Pushpin,latlong);pin.Locations=new Array();var spec=new VECustomIconSpecification();if(locations[i].IsBranch!=null&&locations[i].IsBranch==0)
spec.Image=PUSHPIN_ICON2;else
spec.Image=PUSHPIN_ICON;pin.SetCustomIcon(spec);pin.Locations.push(locations[i]);pin.SetZIndex(996);locationShapes.push(pin);}
clusterManager.GenerateLayer(locationShapes);}}
this.SetMapView=function(locations){var latlongs=new Array();if(locations!=null){for(var i=0;i<locations.length;i++){if(locations[i].Location.Latitude==null||locations[i].Location.Longitude==null)
continue;var latlong=new VELatLong();latlong.Latitude=locations[i].Location.Latitude;latlong.Longitude=locations[i].Location.Longitude;latlongs.push(latlong);}
if(latlongs.length>0)
this.Map.SetMapView(latlongs);}}
this.GetDirections=function(origin,destination){var destinationLatLong=new VELatLong(destination.Location.Latitude,destination.Location.Longitude);var options=new VERouteOptions;if(DEFAULT_COUNTRY=="USA")
options.DistanceUnit=VERouteDistanceUnit.Mile;else
options.DistanceUnit=VERouteDistanceUnit.Kilometer;options.RouteCallback=DirectionsCallback;locations=new Array(origin,destinationLatLong);this.Map.GetDirections(locations,options);var destinationFields=new Array(destination.StreetNumber.toString()+" "+destination.Street.toString(),destination.City.toString(),destination.ProvinceState.toString());var destinationAddress=that.ArrayToString(destinationFields);document.getElementById("drivingFrom").innerHTML=origin;document.getElementById("drivingTo").innerHTML=destinationAddress;}
DirectionsCallback=function(route){if(route!=null&&route.RouteLegs[0]!=null){clusterManager.HideShapes();that.DirectionsLocationId=contactCardManager.GetLocationId();contactCardManager.HidePopup();document.getElementById("SearchForms").style.display="none";document.getElementById("resultsArea").style.display="none";document.getElementById("directionsResults").style.display="block";document.getElementById("legendIconDirections").style.display="block";document.getElementById("legendIconSearch").style.display="none";document.getElementById("legendIconLOB").style.display="none";ClearDirectionsList();that.SearchLayer.Hide();var routeTime=SecondsToTimeFormat(route.RouteLegs[0].Time);document.getElementById("drivingTime").innerHTML=routeTime;var distanceText=route.RouteLegs[0].Distance.toString();distanceText=distanceText.slice(0,4);if(DEFAULT_COUNTRY=="USA")
distanceText+=" mi";else
distanceText+=" km";document.getElementById("drivingDistance").innerHTML=distanceText;var itinerary=route.RouteLegs[0].Itinerary.Items;for(var i=0;i<itinerary.length;i++){AddDirectionsRow(i,itinerary.length-1,itinerary[i].Text,itinerary[i].Distance);}}}
AddDirectionsRow=function(num,maxNum,text,distance){var table=document.getElementById('directionsTable');var lastRow=table.rows.length;var row=table.insertRow(lastRow);if(num%2==0)
row.className="directionsRowOdd";else
row.className="directionsRowEven";var numCol=row.insertCell(0);numCol.className="directionsNumbers";if(num>0&&num<maxNum)
numCol.innerHTML=num+".";var textCol=row.insertCell(1);textCol.innerHTML=text;textCol.className="directionsText";var distanceText=distance.toString();distanceText=distanceText.slice(0,4);if(DEFAULT_COUNTRY=="USA")
distanceText+=" mi";else
distanceText+=" km";var distanceCol=row.insertCell(2);if(num<maxNum){distanceCol.innerHTML=distanceText;distanceCol.className="directionsDistance";}}
ClearDirectionsList=function(){var table=document.getElementById('directionsTable');var lastRow=table.rows.length;if(lastRow>1){for(var i=1;i<lastRow;i++){table.deleteRow(1);}}}
this.HideDirections=function(){clusterManager.ShowShapes();this.Map.DeleteRoute();document.getElementById("SearchForms").style.display="none";document.getElementById("resultsArea").style.display="block";document.getElementById("directionsResults").style.display="none";document.getElementById("legendIconDirections").style.display="none";if(searchManager.POI)
document.getElementById("legendIconSearch").style.display="block";document.getElementById("legendIconLOB").style.display="block";searchManager.GetLocationDetails(that.DirectionsLocationId);that.SearchLayer.Show();that.DirectionsLocationId=null;}
SecondsToTimeFormat=function(seconds){var days=parseInt((seconds)/86400);var hours=parseInt(((seconds)/3600)%24);var minutes=parseInt(((seconds)/60)%60);var dayText=(days>1)?(days+" days "):((days==1)?days+" day ":"");var hourText=(hours>1)?(hours+" hrs "):((hours==1)?hours+" hr ":"");var minuteText=(minutes>=1)?(minutes+" min"):"";return dayText+hourText+minuteText;}
this.ArrayToString=function(fields){var output="";if(fields){var fieldsArray=new Array();for(var j=0;j<fields.length;j++){if(fields[j]!=null&&fields[j]!="")
fieldsArray.push(fields[j]);}
for(var i=0;i<fieldsArray.length;i++){output+=fieldsArray[i];if(i!=fieldsArray.length-1)
output+=", ";}}
return output;}}
function ClusterManager(){var mapStyle=null;var mapWidth=null;var mapHeight=null;var gridCells=new Array();var that=this;var gridSize=GRID_SIZE;this.ClusterLayer=new VEShapeLayer();this.Shapes=new Array();this.MapInstance=null;this.Initialize=function(mapInstance){this.MapInstance=mapInstance;that.MapInstance.AddShapeLayer(this.ClusterLayer);mapWidth=that.MapInstance.GetWidth();mapHeight=that.MapInstance.GetHeight();mapStyle=mapInstance.GetMapStyle();var xCells=parseInt(Math.ceil(mapWidth/gridSize));var yCells=parseInt(Math.ceil(mapHeight/gridSize));gridCells=new Array(xCells*yCells);}
this.DeleteAllShapes=function(){this.ClearShapes();this.Shapes=new Array();}
this.ClearShapes=function(){var gridSize=gridCells.length;this.ClusterLayer.DeleteAllShapes();gridCells=null;gridCells=new Array(gridSize);}
this.HideShapes=function(){this.ClusterLayer.Hide();}
this.ShowShapes=function(){this.ClusterLayer.Show();}
this.GenerateLayer=function(shapes){var currentStyle=mapManager.Map.GetMapStyle();if(mapStyle!=currentStyle){mapStyle=currentStyle;return true;}
var zoomLevel=that.MapInstance.GetZoomLevel();var xCells=parseInt(Math.ceil(mapWidth/gridSize));that.ClearShapes();for(var i=0;i<shapes.length;i++){var shape=shapes[i];var latLong=shape.GetPoints()[0];var pixel=that.MapInstance.LatLongToPixel(latLong);if(pixel.x<=mapWidth&&pixel.y<=mapWidth&&pixel.x>=0&&pixel.y>=0){var gridX=Math.floor(pixel.x/gridSize);var gridY=Math.floor(pixel.y/gridSize);var index=gridX+gridY*xCells;if(index<gridCells.length){if(gridCells[index]==null)
gridCells[index]=new Array();gridCells[index].push(shape);}}}
var spec=new VECustomIconSpecification();for(var i=0;i<gridCells.length;i++){var cell=gridCells[i];var shape=null;var temp=new Array();var sortArray=new Array();if(cell!=null&&cell.length>1&&zoomLevel<=MIN_ZOOM_LIMIT){shape=new VEShape(VEShapeType.Pushpin,cell[0].GetPoints()[0]);shape.Locations=new Array();var perfectCluster=true;for(var k=1;k<cell.length;k++){var lobLatLong1=cell[k].Locations[0].Location;var lobLatLong2=cell[k-1].Locations[0].Location;if(lobLatLong1.Latitude!=lobLatLong2.Latitude||lobLatLong1.Longitude!=lobLatLong2.Longitude){perfectCluster=false;break;}}
var locationDescription=cell.length+" ";var abmRadio=document.getElementById("abmRadio");if(abmRadio&&abmRadio.checked){spec.Image=PUSHPIN_ICON_CLUSTER2;locationDescription+=LOBTypeName2Plural;}
else{spec.Image=PUSHPIN_ICON_CLUSTER;locationDescription+=LOBTypeNamePlural;}
locationDescription+="<br />";if(LOBType=="InvestmentAdvisor"){for(var j=0;j<cell.length;j++){var lobItem=cell[j].Locations[0];shape.Locations.push(lobItem);if(perfectCluster){if(lobItem.FirstName&&lobItem.LastName){var fullName=lobItem.LastName+" "+lobItem.FirstName;temp.push(fullName);}
else if(lobItem.Name){temp.push(lobItem.Name);}}}
temp.sort();var sortArray=new Array(temp.length);for(var j=0;j<cell.length;j++){var lobItem=cell[j].Locations[0];shape.Locations.push(lobItem);if(perfectCluster){if(lobItem.FirstName&&lobItem.LastName){var fullName=lobItem.LastName+" "+lobItem.FirstName;var displayName=lobItem.FirstName+" "+lobItem.LastName;for(var s=0;s<temp.length;s++){if(fullName==temp[s]){sortArray[s]='<br /><a href="javascript:searchManager.GetLocationDetails('+lobItem.UniqueId+');">'+displayName+'</a>';}}}
else if(lobItem.Name){for(var s=0;s<temp.length;s++){if(lobItem.Name==temp[s]){sortArray[s]='<br /><a href="javascript:searchManager.GetLocationDetails('+lobItem.UniqueId+');">'+fullName+'</a>';}}}}}
if(sortArray.length>29)
locationDescription+="<div style=\"height: 400px; overflow: auto\">";else
locationDescription+="<div>";for(var z=0;z<sortArray.length;z++){locationDescription+=sortArray[z];}
locationDescription+="</div>";}
else{locationDescription+="<br />";for(var j=0;j<cell.length;j++){var lobItem=cell[j].Locations[0];shape.Locations.push(lobItem);if(perfectCluster){if(lobItem.FirstName&&lobItem.LastName){var fullName=lobItem.FirstName+" "+lobItem.LastName;locationDescription+='<br /><a href="javascript:searchManager.GetLocationDetails('+lobItem.UniqueId+');">'+fullName+'</a>';}
else if(lobItem.Name){locationDescription+='<br /><a href="javascript:searchManager.GetLocationDetails('+lobItem.UniqueId+');">'+lobItem.Name+'</a>';}}}}
shape.SetCustomIcon(spec);shape.SetDescription(locationDescription);shape.SetZIndex(996);that.ClusterLayer.AddShape(shape);}
else{if(cell!=null&&cell.length>0){for(var shapeCount=0;shapeCount<cell.length;shapeCount++){that.ClusterLayer.AddShape(cell[shapeCount]);}}}}}}