
net.watchee.OpenLayersMapManager=function(watchee,mapFrame){if(!org.xmlBlaster.util.isDefined(watchee))
throw"IllegalArgument: Can't create net.watchee.OpenLayersMapManager without watchee";this.isShutdown=false;this.watchee=watchee;this.mapInfoGui=null;this.log=org.xmlBlaster.util.log;this.mapFrame=(org.xmlBlaster.util.isDefined(mapFrame))?mapFrame:null;this.poiHashtable=new org.xmlBlaster.util.Hashtable();this.mapsrvTilesBaseUrl="http://tiles.mapsrv02.netwake.com";net.watchee.checkInterface(this,net.watchee.I_PoiMapManager);this.initializeMap=function(){var lazy=true;if(lazy){var script=["js/OpenLayers.js","js/OpenStreetMap.js"];LazyLoad.js(script,function(arg){if(this.isShutdown){return;}
this.__initializeWatcheeMapnikProxy();this.__initializeWatcheeOsmarenderProxy();this.__initializeMap();},"someArg",this);}
else{this.__initializeWatcheeMapnikProxy();this.__initializeWatcheeOsmarenderProxy();this.__initializeMap();}}
this.__initializeWatcheeMapnikProxy=function(){var manager=this;OpenLayers.Layer.OSM.WatcheeMapnik=OpenLayers.Class(OpenLayers.Layer.OSM,{initialize:function(name,options){var url=[manager.mapsrvTilesBaseUrl+"/tiles/${z}/${x}/${y}.png"];options=OpenLayers.Util.extend({numZoomLevels:19},options);var newArguments=[name,url,options];OpenLayers.Layer.OSM.prototype.initialize.apply(this,newArguments);},CLASS_NAME:"OpenLayers.Layer.OSM.WatcheeMapnik"});}
this.__initializeWatcheeOsmarenderProxy=function(){var manager=this;OpenLayers.Layer.OSM.OsmarenderLocalProxy=OpenLayers.Class(OpenLayers.Layer.OSM,{initialize:function(name,options){var url=[manager.mapsrvTilesBaseUrl+"/tiles.php?z=${z}&x=${x}&y=${y}&r=osma"];options=OpenLayers.Util.extend({numZoomLevels:18},options);var newArguments=[name,url,options];OpenLayers.Layer.OSM.prototype.initialize.apply(this,newArguments);},CLASS_NAME:"OpenLayers.Layer.OSM.OsmarenderLocalProxy"});}
this.__initializeMap=function(){if(this.mapFrame!=null){var mapCenterLat=xbProperties.getDouble("mapCenterLat",52.497832);var mapCenterLng=xbProperties.getDouble("mapCenterLng",13.395081);var mapZoom=xbProperties.getInt("mapZoom",9);var mapType=xbProperties.getStr("mapType","");this.mapUseMercator=xbProperties.getBoolean("mapUseMercator",true);this.useMapWatcheeMapnikLayer=xbProperties.getBoolean("useMapWatcheeMapnikLayer",true);this.useMapMapnikLayer=xbProperties.getBoolean("useMapMapnikLayer",true);this.useMapWatcheeOsmarenderLayer=xbProperties.getBoolean("useMapWatcheeOsmarenderLayer",true);this.useMapOsmarenderLayer=xbProperties.getBoolean("useMapOsmarenderLayer",false);this.useMapGoogleLayer=xbProperties.getBoolean("useMapGoogleLayer",false);this.useMapVirtualEarthLayer=xbProperties.getBoolean("useMapVirtualEarthLayer",false);this.useMapYahooLayer=xbProperties.getBoolean("useMapYahooLayer",false);this.useMapWmsLayer=xbProperties.getBoolean("useMapWmsLayer",false);net.watchee.util.log.info("OpenLayersMapManager "+OpenLayers.VERSION_NUMBER+" useMapWatcheeMapnikLayer="+this.useMapWatcheeMapnikLayer+" useMapMapnikLayer="+this.useMapMapnikLayer);this.baseIcon="images/marker/green-arrow.png";this.zeppelinBaseIcon="images/marker/green-arrow.png";this.dynamicBaseIcon80x80="/watchee/marker/_80_80_40_2_135_0000FF_FF0000_r270.png";this.currentPopup=null;var eventListeners={"moveend":this.__mapEvent,"zoomend":this.__mapEvent,"changelayer":this.__mapLayerChanged,"changebaselayer":this.__mapBaseLayerChanged};var options=new Object();if(this.mapUseMercator){options={projection:new OpenLayers.Projection("EPSG:900913"),displayProjection:new OpenLayers.Projection("EPSG:4326"),units:"m",numZoomLevels:19,eventListeners:eventListeners,maxResolution:156543.0339,maxExtent:new OpenLayers.Bounds(-20037508,-20037508,20037508,20037508.34)};}
else{options={'maxExtent':new OpenLayers.Bounds(-180,-90,180,90),'maxResolution':"auto",numZoomLevels:18,units:"degrees",displayProjection:new OpenLayers.Projection("EPSG:4326")};}
this.map=new OpenLayers.Map("myMapFrame",options);if(this.useMapWatcheeMapnikLayer){var mapnik=new OpenLayers.Layer.OSM.WatcheeMapnik("OSM-WatcheeMapnikrender");this.map.addLayer(mapnik);}
if(this.useMapMapnikLayer){var mapnik=new OpenLayers.Layer.OSM.Mapnik("OpenStreetMap-Mapnikrender");this.map.addLayer(mapnik);}
if(this.useMapWatcheeOsmarenderLayer){var osmarender=new OpenLayers.Layer.OSM.OsmarenderLocalProxy("OSM-WatcheeOsmarender");this.map.addLayer(osmarender);}
if(this.useMapOsmarenderLayer){var osmarender=new OpenLayers.Layer.OSM.Osmarender("OpenStreetMap-Osmarender");this.map.addLayer(osmarender);}
if(this.useMapGoogleLayer){var gmap=new OpenLayers.Layer.Google("Google Streets",{'sphericalMercator':this.mapUseMercator});var gsat=new OpenLayers.Layer.Google("Google Satellite",{type:G_SATELLITE_MAP,'sphericalMercator':this.mapUseMercator,numZoomLevels:22});var ghyb=new OpenLayers.Layer.Google("Google Hybrid",{type:G_HYBRID_MAP,'sphericalMercator':this.mapUseMercator});this.map.addLayers([gmap,gsat,ghyb]);}
if(this.useMapVirtualEarthLayer){var veroad=new OpenLayers.Layer.VirtualEarth("Virtual Earth Roads",{'type':VEMapStyle.Road,'sphericalMercator':this.mapUseMercator});var veaer=new OpenLayers.Layer.VirtualEarth("Virtual Earth Aerial",{'type':VEMapStyle.Aerial,'sphericalMercator':this.mapUseMercator});var vehyb=new OpenLayers.Layer.VirtualEarth("Virtual Earth Hybrid",{'type':VEMapStyle.Hybrid,'sphericalMercator':this.mapUseMercator});this.map.addLayers([veroad,veaer,vehyb]);}
if(this.useMapYahooLayer){var yahoo=new OpenLayers.Layer.Yahoo("Yahoo Street",{'sphericalMercator':this.mapUseMercator});var yahoosat=new OpenLayers.Layer.Yahoo("Yahoo Satellite",{'type':YAHOO_MAP_SAT,'sphericalMercator':this.mapUseMercator});var yahoohyb=new OpenLayers.Layer.Yahoo("Yahoo Hybrid",{'type':YAHOO_MAP_HYB,'sphericalMercator':this.mapUseMercator});this.map.addLayers([yahoo,yahoosat,yahoohyb]);}
if(this.useMapWmsLayer){var wmsLayer=new OpenLayers.Layer.WMS("OpenLayers WMS","http://labs.metacarta.com/wms/vmap0?",{layers:'basic','sphericalMercator':this.mapUseMercator});this.map.addLayer(wmsLayer);}
var vector=new OpenLayers.Layer.Vector("Editable Vectors");this.map.addLayer(vector);var countMapProviders=0;if(this.useMapMapnikLayer)countMapProviders++;if(this.useMapOsmarenderLayer)countMapProviders++;if(this.useMapGoogleLayer)countMapProviders++;if(this.useMapVirtualEarthLayer)countMapProviders++;if(this.useMapYahooLayer)countMapProviders++;if(this.useMapWmsLayer)countMapProviders++;if(countMapProviders>1){this.map.addControl(new OpenLayers.Control.LayerSwitcher());}
this.map.addControl(new OpenLayers.Control.MousePosition());this.map.addControl(new OpenLayers.Control.Navigation());this.map.addControl(new OpenLayers.Control.PanZoomBar());this.map.addControl(new OpenLayers.Control.Attribution());this.map.addControl(new OpenLayers.Control.ScaleLine({geodesic:!0}));this.map.addControl(new OpenLayers.Control.OverviewMap());this.segmentsLayer=new OpenLayers.Layer.Vector("Segments");this.map.addLayer(this.segmentsLayer);this.markersLayer=new OpenLayers.Layer.Vector("Markers",{rendererOptions:{zIndexing:true}});this.map.addLayer(this.markersLayer);this.__poiMarkersLayer=new OpenLayers.Layer.Vector("POI Markers",{rendererOptions:{yOrdering:true,zIndexing:true}});this.map.addLayer(this.__poiMarkersLayer);this.highlightLayer=new OpenLayers.Layer.Vector("HighlightLayer");this.map.addLayer(this.highlightLayer);if(!this.map.getCenter()){this.map.setCenter(this.createLonLat(mapCenterLng,mapCenterLat),mapZoom);}
this.infoWindowTrackName=null;var arr=xbProperties.getObject("mapStaticMarkerArray",null);if(arr!=null){for(var i=0;i<arr.length;i++){var myOptions=arr[i];var latLng=this.createLonLat(myOptions.lng,myOptions.lat);var point=new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(latLng.lon,latLng.lat));point.style={externalGraphic:myOptions.icon,pointRadius:12}
this.markersLayer.addFeatures([point]);}}
if(false){var latLng=this.createLonLat(9.18,47.78);var point=new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(latLng.lon,latLng.lat));point.style={externalGraphic:"images/marker/watchee-point.png",pointRadius:120}
point.mapManager=this;this.markersLayer.addFeatures([point]);}
this.__poiMarkersLayer.events.register("featureselected",this.__poiMarkersLayer,this.__onPoiMarkerSelect);this.__poiMarkersLayer.events.register("featureunselected",this.__poiMarkersLayer,this.__onPoiMarkerSelect);this.markersLayer.events.register("featureselected",this.markersLayer,this.__onMarkerSelect);this.markersLayer.events.register("featureunselected",this.markersLayer,this.__onMarkerSelect);this.segmentsLayer.events.register("featureselected",this.segmentsLayer,this.__onSegmentSelect);var options={multiple:true};this.selectMarkersControl=new OpenLayers.Control.SelectFeature([this.markersLayer,this.segmentsLayer,this.__poiMarkersLayer],options);this.map.addControl(this.selectMarkersControl);this.selectMarkersControl.activate();if(false){var control=new OpenLayers.Control();var myManager=this;OpenLayers.Util.extend(control,{draw:function(){this.box=new OpenLayers.Handler.Box(control,{"done":this.notice},{keyMask:OpenLayers.Handler.MOD_SHIFT});this.box.activate();},notice:function(bounds,blabla){myManager.shiftActivated=true;OpenLayers.Console.userError(bounds);return false;}});this.map.addControl(control);}}
else{this.map=null;}}
this.createGeofenceDrawer=function(callbackFp,callbackThisArg,callbackObj){return new net.watchee.openlayers.GeofenceDrawer(this,callbackFp,callbackThisArg,callbackObj);}
this.getZoomFactor=function(){return this.map.getZoom();}
this.setCenterAndZoom=function(lat,lon,zoom){var zoom=zoom||this.map.getZoom();this.map.setCenter(this.createLonLat(lon,lat),zoom);}
this.getCenterCoordinates=function(){var c=this.__toCoordinate(this.map.getCenter());return c;}
this.__toCoordinate=function(lonLat){var lonLatDegree=OpenLayers.Layer.SphericalMercator.inverseMercator(lonLat.lon,lonLat.lat);var height=0;var c=new net.watchee.Coordinate(lonLatDegree.lon,lonLatDegree.lat,height);return c;}
this.createLonLat=function(lonDeg,latDeg){if(this.map.baseLayer.projection.projCode=="EPSG:900913"){return new OpenLayers.LonLat(lonDeg,latDeg).transform(new OpenLayers.Projection("EPSG:4326"),this.map.getProjectionObject());}
else{return new OpenLayers.LonLat(lonDeg,latDeg);}}
this.fromMercatorToDegreeIfNeeded=function(x,y){if(this.map.baseLayer.projection.projCode=="EPSG:900913"){return this.fromMercatorToDegree(x,y);}
else{return new OpenLayers.LonLat(x,y);}}
this.fromDegreeToMercator=function(lon,lat){return OpenLayers.Layer.SphericalMercator.forwardMercator(lon,lat);}
this.fromMercatorToDegree=function(x,y){return OpenLayers.Layer.SphericalMercator.inverseMercator(x,y);}
this.__Point=function(lonDeg,latDeg,track,gpsData){var point=null;if(this.map.baseLayer.projection.projCode=="EPSG:900913"){var lonLat=this.createLonLat(lonDeg,latDeg);point=new OpenLayers.Geometry.Point(lonLat.lon,lonLat.lat);}
else{point=new OpenLayers.Geometry.Point(lonDeg,latDeg);}
point.track=track;point.gpsData=gpsData;point.mapManager=this;return point;}
this.__Lon=function(lonDeg){var lonLat=this.createLonLat(lonDeg,0);return lonLat.lon;}
this.__Lat=function(latDeg){var lonLat=this.createLonLat(0,latDeg);return lonLat.lat;}
this.__mapEvent=function(event){}
this.__mapLayerChanged=function(event){}
this.__mapBaseLayerChanged=function(event){}
this.__onMarkerSelect=function(evt,blabla){var marker=evt.feature;var track=marker.track;var gpsData=marker.gpsData;var mapManager=marker.mapManager;var mapInfoDrawerCb=marker.mapInfoDrawerCb||mapManager.mapInfoGui;var sourceEvent=evt.feature.currentSourceEvent||null;var ctrl=(sourceEvent!=null&&sourceEvent.ctrlKey==true);net.watchee.util.log.info("OpenLayersMapManager __onMarkerSelect="+evt.type+" ctrl="+ctrl);var isSelected=(evt.type=="featureselected");var isUnselected=(evt.type=="featureunselected");if(mapInfoDrawerCb!=null&&!mapInfoDrawerCb.processMarkerSelection(isSelected,sourceEvent,track,gpsData))
return;if(isSelected){net.watchee.util.log.info("marker clicked track="+track+" gpsData="+gpsData);var device=null;mapManager.__showInfoWindow(track,gpsData,true,device,mapInfoDrawerCb);track.watchee.mapMarkerClicked(track,gpsData,evt.feature);}}
this.__onSegmentSelect=function(evt){if(evt.feature.geometry.components.length>0){var point=evt.feature.geometry.components[0];var track=point.track;var gpsData=point.gpsData;var mapManager=point.mapManager;track.watchee.mapLineClicked(track,gpsData,track.historyPolyline);}}
this.registerMapInfoGui=function(mapInfoGui){this.mapInfoGui=mapInfoGui;}
this.getNativeMapHandle=function(){return this.map;}
this.hideInfoWindow=function(track){if(org.xmlBlaster.util.isDefined(track)){if(this.infoWindowTrackName!=track.getTrackName())
return;}
this.disableInfoWindow();this.enableInfoWindow();}
this.disableInfoWindow=function(){if(this.currentPopup!=null){this.map.removePopup(this.currentPopup);this.currentPopup=null;}
this.infoWindowTrackName=null;}
this.enableInfoWindow=function(){}
this.updateMap=function(track,gpsData,forceShowing,mapInfoDrawerCb){var mapInfoDrawerCb=mapInfoDrawerCb||this.mapInfoGui;var device=track.getDevice();var autoTrackingOptions=device.getAutoTrackingOptions();var gpsData=org.xmlBlaster.util.isDefined(gpsData)?gpsData:track.getFifoGpsQueue().peekNewest();if(gpsData==null)
return;var forceShowing=org.xmlBlaster.util.toBoolean(forceShowing,false);var hasPicture=gpsData.getPictureId(null)!=null;var showDirectly=track.showInfoWindowForLatestPosition();var hasUsefulGpsData=gpsData.isValid()&&gpsData.hasLatLonNotNull();if(hasUsefulGpsData){if(gpsData.hasOnlyCellId()&&!hasPicture){return;}
var currentLatLng=this.getLatLng(gpsData);var bounds=this._centerMap(gpsData.latitudeDecimal(),gpsData.longitudeDecimal(),autoTrackingOptions);showDirectly=bounds.contains(currentLatLng)?showDirectly:false;var icon=this.__getIcon(track,gpsData);this.__addAndDrawPositionIcon(track,gpsData,icon,mapInfoDrawerCb);this.appendToHistoryLine(track,gpsData);net.watchee.util.log.debug("GPSData drawn on map trackName="+track.getTrackName());}
if(hasUsefulGpsData||hasPicture){if(forceShowing)showDirectly=true;this.__showInfoWindow(track,gpsData,showDirectly,device,mapInfoDrawerCb);}}
this.createIconPointStyle=function(iconUrl){var graphicHeight=20;if(iconUrl.indexOf("watchee-point.png")!=-1)
graphicHeight=37;else if(iconUrl.indexOf("zeppelin")!=-1)
graphicHeight=28;else if(iconUrl.indexOf("/watchee/marker/")!=-1)
graphicHeight=80;else if(iconUrl.indexOf("http://")!=-1)
graphicHeight=34;else
graphicHeight=32;var graphicYOffset=(graphicHeight-1)*(-1);if(iconUrl.indexOf("/watchee/marker/")!=-1){graphicYOffset=(graphicHeight/2+1)*(-1);}
else if(iconUrl.indexOf("images/marker/dynamic/center")!=-1){graphicYOffset=(graphicHeight/2)*(-1);}
var zIndex=0;if(iconUrl.indexOf("999.png")>-1){zIndex=1;}
var style={graphicZIndex:zIndex,externalGraphic:iconUrl,graphicHeight:graphicHeight,graphicYOffset:graphicYOffset}
return style;}
this.__newIcon=function(iconDisplayInfo){var latLng=this.getLatLng(iconDisplayInfo.gpsData);var point=new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(latLng.lon,latLng.lat));point.style=this.createIconPointStyle(iconDisplayInfo.getUrlEncoded());if(iconDisplayInfo.hasOwnProperty("gpsData")&&iconDisplayInfo.gpsData.hasOwnProperty("_poiReportTableRowIndex")){var iconLabel=""+iconDisplayInfo.gpsData._poiReportTableRowIndex;point.style.label=iconLabel;point.style.labelYOffset=40;point.style.fontColor="black";point.style.fontSize="22px";}
return point;}
this.__getIcon=function(track,gpsData){var iconDisplayInfo=track.getIconDisplayInfoDefault(gpsData);iconDisplayInfo=track.getIconDisplayInfoCalculated(iconDisplayInfo,track.getTrackName());if(iconDisplayInfo.isIconNameStatic()){var icon=this.__newIcon(iconDisplayInfo);return icon;}
else{var dynamicIcon=this.__newIcon(iconDisplayInfo);return dynamicIcon;}}
this._centerMap=function(lati,longi,autoTrackingOptions){var panningFenceLatPercent=org.xmlBlaster.util.toNumber(autoTrackingOptions.panningFenceLatPercent,0.9);var panningFenceLngPercent=org.xmlBlaster.util.toNumber(autoTrackingOptions.panningFenceLngPercent,0.9);var center=org.xmlBlaster.util.toBoolean(autoTrackingOptions.center,true);if(!center||panningFenceLatPercent==-1||panningFenceLngPercent==-1)
return this.map.getExtent();var currentLatLng=this.createLonLat(longi,lati);if(panningFenceLatPercent==0&&panningFenceLngPercent==0){this.map.setCenter(currentLatLng);return this.map.getExtent();}
var bounds=this.map.getExtent();var arr=bounds.toArray();var southWestLng=arr[0];var southWestLat=arr[1];var northEastLng=arr[2];var northEastLat=arr[3];var lngSpan=northEastLng-southWestLng;var latSpan=northEastLat-southWestLat;var latDelta=latSpan*((1-panningFenceLatPercent)/2);var lngDelta=lngSpan*((1-panningFenceLngPercent)/2);var fenceSW=new OpenLayers.LonLat(southWestLng+lngDelta,southWestLat+latDelta);var fenceNE=new OpenLayers.LonLat(northEastLng-lngDelta,northEastLat-latDelta);var panningFence=new OpenLayers.Bounds();panningFence.extend(fenceSW);panningFence.extend(fenceNE);var centerIcon=(panningFence.containsLonLat(currentLatLng))?false:true;if(centerIcon){this.map.setCenter(currentLatLng);return this.map.getExtent();}
return bounds;}
this.showAndCenterLocationByDevice=function(device,gpsData,mapInfoDrawerCb){var mapInfoDrawerCb=mapInfoDrawerCb||this.mapInfoGui;var gpsDataArr=new Array();gpsDataArr.push(gpsData);this.zoomAndCenterMap(gpsDataArr);this.__showInfoWindow(null,gpsData,null,device,mapInfoDrawerCb);}
this.showAndCenterLocation=function(track,gpsData,zoomIn,mapInfoDrawerCb){var zoomIn=(zoomIn==undefined)?true:zoomIn;var mapInfoDrawerCb=mapInfoDrawerCb||this.mapInfoGui;var gpsDataArr=new Array();gpsDataArr.push(gpsData);var zoomLevel=(zoomIn)?-1:this.map.getZoom();this.zoomAndCenterMap(gpsDataArr,zoomLevel);var showDirectly=true;var device=null;this.__showInfoWindow(track,gpsData,showDirectly,device,mapInfoDrawerCb);}
this.zoomAndCenterMap=function(gpsDataArr,forceZoomLevel){if(!org.xmlBlaster.util.isDefined(this.map))
return;if(!org.xmlBlaster.util.isDefined(gpsDataArr))
return;if(gpsDataArr.length==0)
return;var forceZoomLevel=forceZoomLevel||-1;var swLat=null;var swLng=null;var neLat=swLat;var neLng=swLng;for(var i=0;i<gpsDataArr.length;i++){var gpsData=gpsDataArr[i];if(gpsData!=null&&gpsData.isValid()&&gpsData.hasLatLonNotNull()){if(swLat==null){swLat=gpsData.latitudeDecimal();swLng=gpsData.longitudeDecimal();neLat=swLat;neLng=swLng;}
else{swLat=(gpsData.latitudeDecimal()<swLat)?gpsData.latitudeDecimal():swLat;swLng=(gpsData.longitudeDecimal()<swLng)?gpsData.longitudeDecimal():swLng;neLat=(gpsData.latitudeDecimal()>neLat)?gpsData.latitudeDecimal():neLat;neLng=(gpsData.longitudeDecimal()>neLng)?gpsData.longitudeDecimal():neLng;}}}
if(swLat==null)
return;var sw=this.createLonLat(swLng,swLat);var ne=this.createLonLat(neLng,neLat);var bounds=new OpenLayers.Bounds();bounds.extend(sw);bounds.extend(ne);var zoom=(forceZoomLevel>0)?forceZoomLevel:this.map.getZoomForExtent(bounds);var maxZoom=xbProperties.getInt("mapMaxZoomOnShow",15);if(zoom>maxZoom)zoom=maxZoom;this.map.setCenter(bounds.getCenterLonLat(),zoom);}
this.drawPositionIcons=function(track,drawingDoneFp,drawingDoneThisArg,mapInfoDrawerCb){this.drawingDoneFp=drawingDoneFp||null;this.drawingDoneThisArg=drawingDoneThisArg||null;track.hideHistoryIcons();var maxIcons=track.getNumIconsToShow();var gpsDataArr=track.getFifoGpsQueue().getArray();var countMarkers=gpsDataArr.length;if(countMarkers==0)return;var i=countMarkers-maxIcons;if(i<0)i=0;this.zoomAndCenterMap(gpsDataArr);var stepWidth=40;this.iterData={gpsDataArr:gpsDataArr,icurr:i,stepWidth:stepWidth,inext:i+stepWidth,maxIcons:maxIcons,track:track,myself:this,takeARest:1};this._drawPositionIconsIterate(mapInfoDrawerCb);}
this._drawPositionIconsDone=function(track){this._showLastetPositionInfoWindow(track);if(org.xmlBlaster.util.isDefined(this.drawingDoneFp)){this.drawingDoneFp.call(this.drawingDoneThisArg,track);this.drawingDoneFp=null;this.drawingDoneThisArg=null;}}
this._drawPositionIconsIterate=function(mapInfoDrawerCb){var me=this;var iterData=me.iterData;for(;iterData.icurr<iterData.inext&&iterData.icurr<iterData.gpsDataArr.length;iterData.icurr++){var gpsData=iterData.gpsDataArr[iterData.icurr];if(gpsData!=null&&gpsData.isValid()&&gpsData.hasLatLonNotNull()){var icon=me.__getIcon(iterData.track,gpsData);me.__addAndDrawPositionIcon(iterData.track,gpsData,icon,mapInfoDrawerCb);}}
if(iterData.icurr==iterData.gpsDataArr.length){me._drawPositionIconsDone(iterData.track);return;}
iterData.inext+=iterData.stepWidth;org.xmlBlaster.setTimeout(me._drawPositionIconsIterate,me,iterData.takeARest,mapInfoDrawerCb);}
this._showLastetPositionInfoWindow=function(track,mapInfoDrawerCb){var mapInfoDrawerCb=mapInfoDrawerCb||this.mapInfoGui;var gpsData=track.getLatestValid();if(gpsData!=null){var device=track.getDevice();var autoTrackingOptions=device.getAutoTrackingOptions();var bounds=this._centerMap(gpsData.latitudeDecimal(),gpsData.longitudeDecimal(),autoTrackingOptions);this.__showInfoWindow(track,gpsData,track.showInfoWindowForLatestPosition(),device,mapInfoDrawerCb);}}
this.getLatLng=function(gpsData){return this.createLonLat(gpsData.longitudeDecimal(),gpsData.latitudeDecimal());}
this.__addAndDrawPositionIcon=function(track,gpsData,icon,mapInfoDrawerCb){var marker=icon;if(track.getHistoryIcons().maxEntries>0){marker.track=track;marker.gpsData=gpsData;marker.mapManager=this;marker.mapInfoDrawerCb=mapInfoDrawerCb;var oldMarker=track.getHistoryIcons().push(marker);if(oldMarker!=null){this.removeMarker(track,oldMarker);}
this.__addMarkerOverlay(marker);}}
this.__addMarkerOverlay=function(marker){if(!org.xmlBlaster.util.isDefined(marker))return;this.markersLayer.addFeatures([marker]);}
this.removeMarker=function(track,marker){this.markersLayer.removeFeatures([marker]);}
this.removeMarkerOverlay=function(track){var markerArray=track.getHistoryIcons().getArray();var len=markerArray.length;for(var jj=0;jj<len;jj++){this.removeMarker(track,markerArray[jj]);}}
this.__addLineOverlay=function(polyLine){if(!org.xmlBlaster.util.isDefined(polyLine))return;this.segmentsLayer.addFeatures([polyLine]);}
this.removeLineOverlay=function(polyLine){if(!org.xmlBlaster.util.isDefined(polyLine))return;this.segmentsLayer.destroyFeatures([polyLine]);}
this.appendToHistoryLine=function(track,gpsData){this.drawHistoryLine(track);}
this.drawHistoryLine=function(track){var arr=track.getFifoGpsArraySorted();var coord=new Array();var segmentDisplayInfo=track.getSegmentDisplayInfoDefault();var start=arr.length-segmentDisplayInfo.getNumSegmentsToShow();if(start<0)start=0;for(var jj=start;jj<arr.length;jj++){var gpsData=arr[jj];if(gpsData==null)
break;if(!gpsData.isValid())
continue;if(gpsData.hasLatLonNull())
continue;coord.push(this.__Point(gpsData.longitudeDecimal(),gpsData.latitudeDecimal(),track,gpsData));}
if(track.historyPolyline!=null){this.removeLineOverlay(track.historyPolyline);track.historyPolyline=null;}
var segmentDisplayInfo=track.getSegmentDisplayInfoDefault();var styleOfLine={strokeColor:segmentDisplayInfo.getSegmentColor(),strokeOpacity:segmentDisplayInfo.getSegmentOpacity(),strokeWidth:segmentDisplayInfo.getSegmentWidth(),pointRadius:6,pointerEvents:"visiblePainted"};track.historyPolyline=new OpenLayers.Feature.Vector(new OpenLayers.Geometry.LineString(coord),null,styleOfLine);this.__addLineOverlay(track.historyPolyline);return track.historyPolyline;}
this.__createInfoWindowTabArr=function(track,gpsData,device,mapInfoDrawerCb){var track=track||null;var device=device||null;var mapInfoDrawerCb=mapInfoDrawerCb||this.mapInfoGui;if(device==null&&track!=null)
device=track.getDevice();var info=gpsData.getInfo("i","")+"  "+gpsData.getInfo("t","");if(info.length==0)
info="On tour";var pictureInfo=net.watchee.toPictureInfo(this.watchee,gpsData);var photoMarkup=(pictureInfo==null)?"":"<a href='"+pictureInfo.url+"' target='picture' border='0'><img class='locationPicture' src='"+pictureInfo.url+"' align='middle' alt='Photo "+pictureInfo.pictureId+"' width='300'/></a>";var trackLength=" ";if(!gpsData.isValid()){trackLength="INVALID";}
else if(track!=null&&track.historyPolyline!=null){trackLength=org.xmlBlaster.util.toNiceDistance(track.historyPolyline.geometry.getLength());trackLength+=" m";if(trackLength.indexOf("undef")!=-1)
trackLength="---";}
var footer="";if(device!=null){footer=device.getBuddyTO().getAliasAndLoginName();if(track!=null&&track.isCurrentTrack())
footer+=" <i>live</i>";else
footer+=" <i>history</i>";}
var gpsHtml=(mapInfoDrawerCb==null)?"":mapInfoDrawerCb.getGpsCompleteHtml(gpsData,info,trackLength,footer,photoMarkup,null,device,track);var infoTabs=[gpsHtml];return infoTabs;}
this.__showInfoWindow=function(track,gpsData,showDirectly,device,mapInfoDrawerCb){var track=track||null;var showDirectly=org.xmlBlaster.util.toBoolean(showDirectly,true);var device=device||null;var mapInfoDrawerCb=mapInfoDrawerCb||this.mapInfoGui;if(gpsData==undefined){return;}
var gLatLng=gpsData.isValid()?this.getLatLng(gpsData):this.map.getCenter();if(!gpsData.isValid()&&track!=null){var locationPictureId=gpsData.getPictureId(null);if(locationPictureId==null){return null;}}
if(showDirectly){var infoTabs=this.__createInfoWindowTabArr(track,gpsData,device,mapInfoDrawerCb);var thisObj=this;function onPopupClose(evt){thisObj.disableInfoWindow();}
this.disableInfoWindow();var popup=new OpenLayers.Popup.FramedCloud("GpsInfo",gLatLng,null,infoTabs[0],null,true,onPopupClose);this.currentPopup=popup;this.map.addPopup(popup);this.infoWindowTrackName=(track!=null)?track.getTrackName():gpsData.getTrackName();}
return infoTabs;}
this.showXsmsInfoWindow=function(xsms,gpsData,mapInfoDrawerCb){var mapInfoDrawerCb=mapInfoDrawerCb||this.mapInfoGui;if(mapInfoDrawerCb==null){return null;}
var info="XSMS";if(gpsData!=null){info="XSMS "+gpsData.getInfo("i","")+"  "+gpsData.getInfo("t","");}
var xsmsHtml=mapInfoDrawerCb.getGpsCompleteHtml(gpsData,info,null,null,null,xsms,null,null);var gLatLng=(gpsData!=null&&gpsData.isValid())?this.getLatLng(gpsData):this.map.getCenter();var thisObj=this;function onPopupClose(evt){thisObj.disableInfoWindow();}
this.disableInfoWindow();var popup=new OpenLayers.Popup.FramedCloud("XSMS Info",gLatLng,null,xsmsHtml,null,true,onPopupClose);this.currentPopup=popup;this.map.addPopup(popup);this.infoWindowTrackName=null;return xsmsHtml;}
this.shutdown=function(){this.isShutdown=true;if(this.map!=null){var manually=false;if(manually){try{this.hideInfoWindow(null);this.__poiMarkersLayer.removeAllFeatures();this.__poiMarkersLayer.destroy();this.markersLayer.removeAllFeatures();this.markersLayer.destroy();this.highlightLayer.removeAllFeatures();this.highlightLayer.destroy();this.segmentsLayer.destroy();var controlsArr=this.map.controls;for(var i=0,l=controlsArr.length;i<l;i++)
this.map.removeControl(controlsArr[i]);var layerArr=this.map.layers;for(var i=0,l=layerArr.length;i<l;i++)
this.map.removeLayer(layerArr[i]);}
catch(e){alert("OpenLayersManager.shutdown(): "+e);}}
try{this.map.destroy();}
catch(e){alert("OpenLayersManager.shutdown(): "+e);}
this.map=null;}}}
net.watchee.OpenLayersMapManager.prototype.addPoi=function(gpsData,drawingAttributes){if(!net.watchee.I_PoiMapManager.addPoi(gpsData,drawingAttributes)){return;}
var locationId=gpsData.getUniqueId();if(this.poiHashtable.get(locationId)!=null)return;drawingAttributes.gpsData=gpsData;this.poiHashtable.put(locationId,drawingAttributes);var latLng=this.getLatLng(gpsData);var pointFeature=new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(latLng.lon,latLng.lat));pointFeature.style={graphicZIndex:0.5,externalGraphic:drawingAttributes.getIconUrl(),graphicHeight:32,graphicYOffset:-32,label:""+drawingAttributes.getGpsData()._poiReportTableRowIndex,labelYOffset:40,fontColor:"black",fontSize:"22px"};pointFeature.gpsData=gpsData;pointFeature.mapManager=this;pointFeature.drawingAttributes=drawingAttributes;drawingAttributes.pointFeature=pointFeature;this.__poiMarkersLayer.addFeatures([pointFeature]);if(drawingAttributes.getCenter()){this.map.setCenter(latLng,drawingAttributes.getZoomTo());}
else if(drawingAttributes.getZoomTo()!=null){this.map.zoomTo(drawingAttributes.getZoomTo());}}
net.watchee.OpenLayersMapManager.prototype.highlightPoi=function(gpsData,drawingAttributes){var drawingAttributes=drawingAttributes||new net.watchee.DrawingAttributes();if(!net.watchee.I_PoiMapManager.highlightPoi(gpsData,drawingAttributes)){return;}
var latLng=this.__drawHighlight(gpsData.latitudeDecimal(),gpsData.longitudeDecimal(),drawingAttributes.getRadiusPixel());if(drawingAttributes.getCenter()){this.map.setCenter(latLng,drawingAttributes.getZoomTo());}
else if(drawingAttributes.getZoomTo()!=null){this.map.zoomTo(drawingAttributes.getZoomTo());}
return true;}
net.watchee.OpenLayersMapManager.prototype.__drawHighlight=function(lat,lon,radiusPixel){var radiusPixel=radiusPixel||24;var latLng=this.createLonLat(lon,lat);var point=new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(latLng.lon,latLng.lat));var zIndex=1;var graphicHeight=radiusPixel*2;point.style={graphicZIndex:zIndex,externalGraphic:"images/marker/crosshair"+graphicHeight+".png",graphicHeight:graphicHeight,graphicYOffset:(graphicHeight/2)*(-1)};this.highlightLayer.addFeatures([point]);return latLng;}
net.watchee.OpenLayersMapManager.prototype.removeHighlightPoi=function(){this.highlightLayer.destroyFeatures();}
net.watchee.OpenLayersMapManager.prototype.removePoi=function(locationId,attributes){var locationId=locationId||null;if(locationId==null){var count=this.poiHashtable.clear();this.__poiMarkersLayer.destroyFeatures();return count>0;}
var drawingAttributes=this.poiHashtable.remove(locationId);if(drawingAttributes==null)
return false;if(drawingAttributes.pointFeature==undefined)
return false;this.__poiMarkersLayer.destroyFeatures([drawingAttributes.pointFeature]);delete drawingAttributes.pointFeature;drawingAttributes=null;return true;}
net.watchee.OpenLayersMapManager.prototype.__getPois=function(){var gpsDataArr=new Array();var arr=this.poiHashtable.getValues();for(var i=0,len=arr.length;i<len;i++){var drawingAttributes=arr[i];if(drawingAttributes.gpsData==undefined)
continue;gpsDataArr.push(drawingAttributes.gpsData);}
return gpsDataArr;}
net.watchee.OpenLayersMapManager.prototype.centerPois=function(zoomLevel){var forceZoomLevel=zoomLevel||-1;this.zoomAndCenterMap(this.__getPois(),forceZoomLevel);}
net.watchee.OpenLayersMapManager.prototype.__onPoiMarkerSelect=function(evt){var pointFeature=evt.feature;var gpsData=pointFeature.gpsData;var mapManager=pointFeature.mapManager;if(pointFeature.drawingAttributes==undefined){alert("pointFeature.drawingAttributes is undefined");return;}
var drawingAttributes=pointFeature.drawingAttributes;var isSelected=(evt.type=="featureselected");var isUnselected=(evt.type=="featureunselected");var cb=drawingAttributes.getOnMarkerClickCb();if(cb!=null){var ret=true;if(drawingAttributes.getOnMarkerClickThisP()!=null)
ret=cb.call(drawingAttributes.getOnMarkerClickThisP(),isSelected,gpsData,drawingAttributes.getOnMarkerClickReturnObj());else
ret=cb(isSelected,gpsData,drawingAttributes.getOnMarkerClickReturnObj());if(ret==false){return;}}
if(isSelected){net.watchee.util.log.info("marker clicked gpsData="+gpsData);mapManager.__showInfoWindow(null,gpsData,true,null,null);}}
