
Mundia.namespaces("controls");Mundia.controls.MapControl=new Class({Extends:Mundia.controls.Control,initialize:function(elementId,eventTypeNames){this.parent(elementId);this._map=null;this._icon="<img src='"+Page.Url.staticContent('/images/person-ico.png')+"' />";this._smallIcon="<img src='"+Page.Url.staticContent('/images/ico/mapdot.png')+"' />";this._innerInfoContent="<img src='{0}' height='39' alt='image' /><div class='PopupLinkHolder' ><a href='{1}'> {2}</a><br />{4}<br />{3}<br /><a href='{5}'>{6}</a></div>";this._infobox='<div id="clusterPin"><b class="top"><i></i><b></b></b><b class="left"><i></i></b><div class="content">{0}<br /></div><b class="right"></b><b class="bottom"><i></i><b></b></b></div>';this._clusteredPinInfoBox='<div id="clusterPin"><b class="top"><i></i><b></b></b><b class="left"><i></i></b><div class="content">{0}</div><b class="right"></b><b class="bottom"><i></i><b></b></b></div>';this._birthEventId=null;this._eventTypeNames=eventTypeNames;this.zoomLimit=1;this.mapStyle;this.gridSize=30;this.maxDiscription=3;this.recentActivityList=null;this.intervalId=0;this.MAX_NODE_COUNT_UPDATE_INTERVAL=4000;this.MIN_NODE_COUNT_UPDATE_INTERVAL=2000;this.ESTIMATED_NODE_COUNT_ADJUSTMENT=0.5;this._surnameView=true;this._boundUpdatePin=this._boundUpdatePin.bind(this);this._timeoutId=0;this._currentActivity=0;},ZoomIntoCountry:function(){var stringRequest=new Request.JSON({url:Mundia.WebContext.getServiceUrl('MapService.GetLocalCountry'),onSuccess:this.FindLocation_success.bind(this),onFailure:this.FindLocation_failed.bind(this)}).get();},_render:function(){if(typeof(VEMapOptions)=="undefined"){return;}
this._veMapOptions=new VEMapOptions();this._veMapOptions.EnableBirdseye=false;this._veMapOptions.EnableDashboardLabels=true;this._layer=new VEShapeLayer();this.clusterLayer=new VEShapeLayer();this.parent();this._map=new VEMap(this.get_elementId());this._map.LoadMap(new VELatLong(0,0),2,VEMapStyle.Road,false,VEMapMode.Mode2D,false,1,this._veMapOptions);this._map.AddShapeLayer(this._layer);this._map.AddShapeLayer(this.clusterLayer);this._map.ClearInfoBoxStyles();this._map.SetClientToken($('token').value);if(!Page.properties.EventTypeNames){this._surname=document.getElementById('surname').value;var stringRequest=new Request.JSON({method:'GET',headers:{'Content-Type':'application/json'},urlEncoded:false,url:Mundia.WebContext.getServiceUrl('MapService.GetRecentActivity'),onSuccess:this.GetRecentActivity_success.bind(this),onFailure:this.GetRecentActivity_failed.bind(this)}).get({surname:this._surname});}
else{this._map.HideControl($('MSVE_navAction_RoadMapStyle'));this._map.HideControl($('MSVE_navAction_AerialMapStyle'));this._map.HideControl($('MSVE_navAction_separator1'));this._map.HideControl($('MSVE_navAction_ObliqueMapView'));this._map.HideControl($('MSVE_navAction_showLabels'));this._map.HideControl($('MSVE_navAction_styleGroup'));this._map.HideControl($('MSVE_navAction_topBar'));this._map.HideControl($('MSVE_navAction_compassContainer'));this._map.HideControl($('MSVE_navAction_compassBackground'));this._map.HideControl($('MSVE_navAction_topBackground'));this._map.HideScalebar();this.ShowLifeEvents(Page.ViewData["EventList"]);}
this._map.AttachEvent('onchangeview',this.cluster.bind(this));this._map.AttachEvent('onresize',this.cluster.bind(this));},_attachEvents:function(){},AddPushpin:function(lat,lon,markerContent,icon){var latlong=new VELatLong();latlong.Latitude=lat;latlong.Longitude=lon;var shape=new VEShape(VEShapeType.Pushpin,latlong);shape.SetCustomIcon(icon);shape.SetDescription(markerContent);this._layer.AddShape(shape);if(markerContent.indexOf("Birth")>-1){this._birthEventId=shape.GetID();}},GetRecentActivity_success:function(responseJson,responseText){this._layer.DeleteAllShapes();this.recentActivityList=responseJson.GetRecentActivityResult;this._timeoutId=setTimeout(this._boundUpdatePin,0);},GetRecentActivity_failed:function(e){},_boundUpdatePin:function(){clearTimeout(this._timeoutId);if(this._currentActivity<this.recentActivityList.length){var activity=this.recentActivityList[this._currentActivity];var locationString=activity.LocationString;var tid=activity.NodeTid;var pid=activity.NodePid;this._currentActivity++;var stringRequest=new Request.JSON({url:Mundia.WebContext.getServiceUrl('MapService.GetActivityDetails'),onSuccess:this.GetActivityDetails_success.bind(this),onFailure:this.GetActivityDetails_failed.bind(this)}).get({pid:pid,tid:tid,location:locationString});}},_getUpdateInterval:function(){return this.MIN_NODE_COUNT_UPDATE_INTERVAL+Math.floor(Math.random()*((this.MAX_NODE_COUNT_UPDATE_INTERVAL-this.MAX_NODE_COUNT_UPDATE_INTERVAL)+1));},stop:function(){clearTimeout(this._timeoutId);},GetActivityDetails_success:function(responseJson,responseText){if(this._currentActivity<=5){setTimeout(this._boundUpdatePin,100);}
else{setTimeout(this._boundUpdatePin,1000);}
var recentActivity=responseJson.GetActivityDetailsResult;var string=recentActivity.Location.replace('(','');string=string.replace(')','');var location=string.split(' ');var markerContent=this._infobox.replace('{0}',this._innerInfoContent);markerContent=markerContent.replace('{0}',recentActivity.ImageUrl);markerContent=markerContent.replace('{1}',Page.Url.standardAction("Index","Person",{tid:recentActivity.NodeTid,pid:recentActivity.NodePid}));markerContent=markerContent.replace('{2}',recentActivity.GivenName+' '+recentActivity.Surname);markerContent=markerContent.replace('{3}',recentActivity.LocationString);markerContent=markerContent.replace('{4}',recentActivity.YearRange);markerContent=markerContent.replace('{5}',Page.Url.standardAction('Index','Person',{pid:recentActivity.NodePid,tid:recentActivity.NodeTid}));markerContent=markerContent.replace('{6}',Mundia.Localizer.getString('Shared.ViewProfile'));this.AddPushpin(location[2],location[1],markerContent,this._icon);if(this._currentActivity==1){this.SetDefaultViewFromMarkers();}
else if(this._currentActivity==5||this._currentActivity>=this.recentActivityList.length){if(this._currentActivity<=5){this.FindBestViewFromMarkers();}
this.cluster(false,this._icon);}},GetActivityDetails_failed:function(e){},AddRecentActivityToMap:function(){if(this.recentActivityList&&this.recentActivityList.length>0){for(var i=0;i<this.recentActivityList.length;i++){var string=this.recentActivityList[i].Location.replace('(','');string=string.replace(')','');var location=string.split(' ');var markerContent=this._infobox.replace('{0}',this._innerInfoContent);markerContent=markerContent.replace('{0}',this.recentActivityList[i].ImageUrl);markerContent=markerContent.replace('{1}',Page.Url.standardAction("Index","Person",{tid:string=this.recentActivityList[i].NodeTid,pid:string=this.recentActivityList[i].NodePid}));markerContent=markerContent.replace('{2}',this.recentActivityList[i].GivenName+' '+this.recentActivityList[i].Surname);markerContent=markerContent.replace('{3}',this.recentActivityList[i].LocationString);this.AddPushpin(location[2],location[1],markerContent,this._icon);}
this.FindBestViewFromMarkers();this.cluster(false,this._icon);}},GetRecentActivityForACountry_success:function(responseJson,responseText){this._layer.DeleteAllShapes();if($hasValue(responseJson.GetRecentActivityForACountryResult)){this.fireEvent('countryChanged',{countryId:responseJson.GetRecentActivityForACountryResult.CountryId});}
if($hasValue(responseJson.GetRecentActivityForACountryResult.RecentActivity)){this.recentActivityList=responseJson.GetRecentActivityForACountryResult.RecentActivity;this.AddRecentActivityToMap();this.intervalId=this.AddRecentActivityToMap.periodical(1,this);}},GetRecentActivityForACountry_failed:function(e){},mapClick:function(e){var LL=this._map.PixelToLatLong(new VEPixel(e.mapX,e.mapY));var stringRequest=new Request.JSON({url:Mundia.WebContext.getServiceUrl('MapService.GetRecentActivityForACountry'),onSuccess:this.GetRecentActivityForACountry_success.bind(this),onFailure:this.GetRecentActivityForACountry_failed.bind(this)}).get({surname:this._surname,latitude:LL.Latitude,longitude:LL.Longitude});var stringRequest2=new Request.JSON({url:Mundia.WebContext.getServiceUrl('MapService.GetCountryName'),onSuccess:this.ChangeCountryName.bind(this)}).get({latitude:LL.Latitude,longitude:LL.Longitude});},FindResultsCallback:function(result,place,hasMore,error){},FindLocation_success:function(e){this._map.SetMapView(new VELatLongRectangle(new VELatLong(e.GetLocalCountryResult.resultsField[0].bestViewNortheastPointField.latitudeField,e.GetLocalCountryResult.resultsField[0].bestViewNortheastPointField.longitudeField),new VELatLong(e.GetLocalCountryResult.resultsField[0].bestViewSouthwestPointField.latitudeField,e.GetLocalCountryResult.resultsField[0].bestViewSouthwestPointField.longitudeField)));},FindLocation_failed:function(e){},ShowLifeEvents:function(Events){if(Events&&Events.length>0){for(var i=0;i<Events.length;i++){var event=Events[i];var date=event.Date?(event.UserIsTrusted?event.Date.LocalizedDisplayValue:event.Date.Year):'';var type='Event';if(event.Type!==0&&event.Type!=='0'&&this._eventTypeNames[event.Type]){type=this._eventTypeNames[event.Type];}
else if(!$isEmpty(event.TypeName)){type=event.TypeName;}
if(type=='Event'||type=='Custom'){if(event.CustomType){type=event.CustomType;}
else if(event.Title){type=event.Title;}
else if(event.DetailsStory){var detailsString=event.DetailsStory.Content;type=detailsString.substr(0,13)+"...";}
else{type="";}}
if(type!==""){this.AddPushpin(event.Latitude,event.Longitude,this._infobox.replace('{0}',"<strong>"+type+"</strong><br />"+event.Place+"<br />"+date),this._smallIcon);}}
this._surnameView=false;this.FindBestViewFromMarkers();this.cluster(true,this._smallIcon);}},FindBestViewFromMarkers:function(){var centerLat;var centerLong;if($hasValue(this._birthEventId)){var shape=this._layer.GetShapeByID(this._birthEventId);var latLongs=shape.GetPoints();centerLat=latLongs[0].Latitude;centerLong=latLongs[0].Longitude;}
else{var bounds=this._layer.GetBoundingRectangle();centerLat=(bounds.BottomRightLatLong.Latitude+bounds.TopLeftLatLong.Latitude)/2;centerLong=(bounds.BottomRightLatLong.Longitude+bounds.TopLeftLatLong.Longitude)/2;}
var latLong=new VELatLong(centerLat,centerLong,null,null);var view=new VEMapViewSpecification(latLong,this._map.GetZoomLevel(),1000,-90,0);this._map.SetMapView(view);},SetDefaultViewFromMarkers:function(){var centerLat=20.00;var centerLong=-18.00;var latLong=new VELatLong(centerLat,centerLong,null,null);var view=new VEMapViewSpecification(latLong,this._map.GetZoomLevel(),1000,-90,0);this._map.SetMapView(view);},ChangeCountryName:function(results){$('countryName').innerText=results.GetCountryNameResult.CountryName;},cluster:function(listAllEvents,icon){if((this._surnameView===true||this._surnameView==='true')&&!icon){icon=this._icon;}
else if(!icon){icon=this._smallIcon;}
if(this.mapStyle!=this._map.GetMapStyle()){this.mapStyle=this._map.GetMapStyle();return true;}
this.clusterLayer.DeleteAllShapes();var mapView=this._map.GetMapView();var bottomRight=this._map.LatLongToPixel(mapView.BottomRightLatLong);var mapWidth=parseInt(Math.ceil(bottomRight.x),10);var mapHeight=parseInt(Math.ceil(bottomRight.y),10);var numXCells=parseInt(Math.ceil(mapWidth/this.gridSize),10);var numYCells=parseInt(Math.ceil(mapHeight/this.gridSize),10);var gridCells=new Array(numXCells*numYCells);for(var xIndex=0;xIndex<numXCells;xIndex++){for(var yIndex=0;yIndex<numYCells;yIndex++){gridCells[xIndex+yIndex*numXCells]={latlong:new VELatLong(0,0),title:"",description:"",length:0};}}
for(var cnt=0;cnt<this._layer.GetShapeCount();cnt++){var shape=this._layer.GetShapeByIndex(cnt);var latLong=(shape.GetPoints())[0];var pixel=this._map.LatLongToPixel(latLong);var xPixel=pixel.x;var yPixel=pixel.y;if(mapWidth>=xPixel&&mapHeight>=yPixel&&xPixel>=0&&yPixel>=0){var i=Math.floor(xPixel/this.gridSize);var j=Math.floor(yPixel/this.gridSize);var key=i+j*numXCells;if(gridCells[key].length===0){gridCells[key].latlong=latLong;gridCells[key].title=shape.GetTitle();gridCells[key].description=shape.GetDescription();}
gridCells[key].length++;if(gridCells[key].length>1&&this._map.GetZoomLevel()>=this.zoomLimit){if(gridCells[key].length==2){var txt=gridCells[key].description;var div=$(document.createElement('div'));div.set('html',txt);var element=div.lastChild.childNodes[2];gridCells[key].description=element.innerHTML+"";}
if((listAllEvents===true&&listAllEvents==='true')||gridCells[key].length<=this.maxDiscription){var text=shape.GetDescription();var divEl=$(document.createElement('div'));divEl.set('html',text);var childElement=divEl.lastChild.childNodes[2];gridCells[key].description=gridCells[key].description+childElement.innerHTML+"";}
else{if(gridCells[key].length==(this.maxDiscription+1)){gridCells[key].description=gridCells[key].description+Mundia.Localizer.getString('Shared.ClusteredPins.MoreThan3')+"<br /><br />";}}}}}
for(var keyIndex=0;keyIndex<gridCells.length;keyIndex++){if(gridCells[keyIndex].length>1){gridCells[keyIndex].description=this._clusteredPinInfoBox.replace('{0}',gridCells[keyIndex].description+"");if(gridCells[keyIndex].length>=this.maxDiscription){gridCells[keyIndex].description=gridCells[keyIndex].description;}}
if(gridCells[keyIndex].length>0){var clusterShape=new VEShape(VEShapeType.Pushpin,gridCells[keyIndex].latlong);clusterShape.SetTitle(gridCells[keyIndex].title);clusterShape.SetDescription(gridCells[keyIndex].description);if(gridCells[keyIndex].length==1){clusterShape.SetCustomIcon(icon);}
else{clusterShape.SetCustomIcon(icon);}
this.clusterLayer.AddShape(clusterShape);}}
return false;}});