
net.watchee.GoogleMapManager=function(watchee,mapFrame){if(!org.xmlBlaster.util.isDefined(watchee))
throw"IllegalArgument: Can't create net.watchee.GoogleMapManager without watchee";this.watchee=watchee;this.mapInfoGui=null;this.log=org.xmlBlaster.util.log;this.mapFrame=(org.xmlBlaster.util.isDefined(mapFrame))?mapFrame:null;this.initializeMap=function(){var lazy=false;if(lazy){var url="http://maps.google.com/maps?file=api&v=2&key=";if(isGpsOem){url+="";}
else if(isWatchee){url+="ABQIAAAAUMweNLF5X13w6CgbgdxYcxScemQMM2xjuIWTFWTwpxQ4WBHUoBTHJDfuDKkhcjQlp6dqm79DGEbR3w";}
else if(isGpsVision){url+="ABQIAAAAUMweNLF5X13w6CgbgdxYcxR653_U0g10sw5LdW9WGYPLzDh6YxTK572mu3WgCS4YgHTw7DI1vQLpdg";}
else{url+="ABQIAAAAUMweNLF5X13w6CgbgdxYcxR_n4M2bKRuSktX_sE7-at4hKziDRSeMozln4gHo6CHoZ73vYmg06uNdA";}
var script=[url];LazyLoad.js(script,function(arg){alert(script+' has been loaded');this.__initializeMap();},"someArg",this);}
else{this.__initializeMap();}}
this.__initializeMap=function(){if(typeof GBrowserIsCompatible=="undefined"){alert("GoogleMapManager.js: "+"Google map is not configured to be used");return;}
if(!GBrowserIsCompatible()){alert("Your browser is not compatible for Google Maps");return;}
else{net.watchee.util.log.info("GoogleMapManager.js: Browser is compatible for google maps");}
if(this.mapFrame!=null){var mapCenterLat=xbProperties.getDouble("mapCenterLat",47.6420697);var mapCenterLng=xbProperties.getDouble("mapCenterLng",9.35708);var mapZoom=xbProperties.getInt("mapZoom",10);var mapType=xbProperties.getStr("mapType","");this.map=new GMap2(this.mapFrame);this.map.addControl(new GSmallMapControl());this.map.addControl(new GMapTypeControl());this.map.setCenter(new GLatLng(mapCenterLat,mapCenterLng),mapZoom);this.map.enableScrollWheelZoom();if(mapType=="G_NORMAL_MAP")
this.map.setMapType(G_NORMAL_MAP);else if(mapType=="G_SATELLITE_MAP")
this.map.setMapType(G_SATELLITE_MAP);else if(mapType=="G_HYBRID_MAP")
this.map.setMapType(G_HYBRID_MAP);else if(mapType=="G_PHYSICAL_MAP")
this.map.setMapType(G_PHYSICAL_MAP);this.baseIcon=new GIcon(G_DEFAULT_ICON);this.baseIcon.iconAnchor=new GPoint(9,34);this.baseIcon.infoWindowAnchor=new GPoint(9,2);this.baseIcon.infoShadowAnchor=new GPoint(18,25);{var dWidth=80;var dHeight=80;this.dynamicBaseIcon80x80=new GIcon();this.dynamicBaseIcon80x80.iconSize=new GSize(dWidth,dHeight);this.dynamicBaseIcon80x80.iconAnchor=new GPoint(dWidth/2,dHeight/2);this.dynamicBaseIcon80x80.infoWindowAnchor=new GPoint(dWidth/2+5,dHeight/2-10);this.dynamicBaseIcon80x80.transparent="/watchee/images/transparent.png";this.dynamicBaseIcon80x80.imageMap=[30,20,50,20,50,50,30,50,30,20];}
{var dWidth=70;var dHeight=28;this.zeppelinBaseIcon=new GIcon();this.zeppelinBaseIcon.iconSize=new GSize(dWidth,dHeight);this.zeppelinBaseIcon.iconAnchor=new GPoint(dWidth/2,dHeight/2);this.zeppelinBaseIcon.infoWindowAnchor=new GPoint(dWidth/2+5,dHeight/2-10);}
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=new GLatLng(myOptions.lat,myOptions.lng);var markerOptions={};if(org.xmlBlaster.util.isFilled(myOptions.icon)){var gicon=null;if(myOptions.icon.indexOf("images/")==0){gicon=new GIcon(this.baseIcon);}
else{gicon=new GIcon(G_DEFAULT_ICON);}
gicon.image=myOptions.icon;markerOptions.icon=gicon;}
if(org.xmlBlaster.util.isFilled(myOptions.title)){markerOptions.title=myOptions.title;}
this.map.addOverlay(new GMarker(latLng,markerOptions));}}}
else{this.map=null;}}
this.createGeofenceDrawer=function(callbackFp,callbackThisArg,callbackObj){return new net.watchee.google.GeofenceDrawer(this.getNativeMapHandle(),callbackFp,callbackThisArg,callbackObj);}
this.registerMapInfoGui=function(mapInfoGui){this.mapInfoGui=mapInfoGui;}
this.getNativeMapHandle=function(){return this.map;}
this.getZoomFactor=function(){return this.map.getZoom();}
this.setCenterAndZoom=function(lat,lon,zoom){var zoom=zoom||this.map.getZoom();this.map.setCenter(new GLatLng(lat,lon),zoom);}
this.getCenterCoordinates=function(){var c=this.__toCoordinate(this.map.getCenter());return c;}
this.__toCoordinate=function(gLatLng){var height=0;var c=new net.watchee.Coordinate(gLatLng.lng(),gLatLng.lat(),height);return c;}
this.hideInfoWindow=function(track){if(org.xmlBlaster.util.isDefined(track)){if(this.infoWindowTrackName!=track.getTrackName())
return;}
this.disableInfoWindow();this.enableInfoWindow();}
this.disableInfoWindow=function(){this.map.disableInfoWindow();this.infoWindowTrackName=null;}
this.enableInfoWindow=function(){this.map.enableInfoWindow();}
this.updateMap=function(track,gpsData,forceShowing){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);var marker=this.addAndDrawPositionIcon(track,currentLatLng,gpsData,icon);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);}}
this.getIcon=function(track,gpsData){var gpsData_=org.xmlBlaster.util.isDefined(gpsData)?gpsData:null;var iconDisplayInfo=track.getIconDisplayInfoDefault(gpsData_);iconDisplayInfo=track.getIconDisplayInfoCalculated(iconDisplayInfo,track.getTrackName());if(iconDisplayInfo.isIconNameStatic()){var base=this.baseIcon;if(iconDisplayInfo.getIconName().indexOf("zeppelin")!=-1){base=this.zeppelinBaseIcon;}
var icon=new GIcon(base);icon.image=iconDisplayInfo.getUrlEncoded();return icon;}
else{var dynamicIcon=new GIcon(this.dynamicBaseIcon80x80);dynamicIcon.image=iconDisplayInfo.getUrlEncoded();return dynamicIcon;}}
this._centerMap=function(lati,longi,autoTrackingOptions){var panningFenceLatPercent=autoTrackingOptions.panningFenceLatPercent||0.9;var panningFenceLngPercent=autoTrackingOptions.panningFenceLngPercent||0.9;var center=org.xmlBlaster.util.toBoolean(autoTrackingOptions.center,true);if(!center||panningFenceLatPercent==-1||panningFenceLngPercent==-1)
return this.map.getBounds();var currentLatLng=new GLatLng(lati,longi);var bounds=this.map.getBounds();var southWest=bounds.getSouthWest();var northEast=bounds.getNorthEast();var lngSpan=northEast.lng()-southWest.lng();var latSpan=northEast.lat()-southWest.lat();var latDelta=latSpan*((1-panningFenceLatPercent)/2);var lngDelta=lngSpan*((1-panningFenceLngPercent)/2);var fenceSW=new GLatLng(southWest.lat()+latDelta,southWest.lng()+lngDelta);var fenceNE=new GLatLng(northEast.lat()-latDelta,northEast.lng()-lngDelta);var panningFence=new GLatLngBounds(fenceSW,fenceNE);var centerIcon=(panningFence.contains(currentLatLng))?false:true;if(centerIcon){this.map.setCenter(currentLatLng);return this.map.getBounds();}
return bounds;}
this.showAndCenterLocation=function(track,gpsData,zoomIn){var zoomIn=(zoomIn==undefined)?true:zoomIn;var gpsDataArr=new Array();gpsDataArr.push(gpsData);var zoomLevel=(zoomIn)?-1:this.map.getZoom();this.zoomAndCenterMap(gpsDataArr,zoomLevel);this.showInfoWindow(track,gpsData);}
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=new GLatLng(swLat,swLng);var ne=new GLatLng(neLat,neLng);var bounds=new GLatLngBounds(sw,ne);var zoom=(forceZoomLevel>0)?forceZoomLevel:this.map.getBoundsZoomLevel(bounds);var maxZoom=xbProperties.getInt("mapMaxZoomOnShow",15);if(zoom>maxZoom)zoom=maxZoom;this.map.setCenter(bounds.getCenter(),zoom);}
this.drawPositionIcons=function(track,drawingDoneFp,drawingDoneThisArg){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();}
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(){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);var latLng=me.getLatLng(gpsData);var marker=me.addAndDrawPositionIcon(iterData.track,latLng,gpsData,icon);}}
if(iterData.icurr==iterData.gpsDataArr.length){me._drawPositionIconsDone(iterData.track);return;}
iterData.inext+=iterData.stepWidth;qx.event.Timer.once(me._drawPositionIconsIterate,me,iterData.takeARest);}
this._showLastetPositionInfoWindow=function(track){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());}}
this.getLatLng=function(gpsData){return new GLatLng(gpsData.latitudeDecimal(),gpsData.longitudeDecimal());}
this.addAndDrawPositionIcon=function(track,currentLatLng,gpsData,icon){if(track.getHistoryIcons().maxEntries>0){var marker=(icon==undefined)?new GMarker(currentLatLng):new GMarker(currentLatLng,icon);var oldMarker=track.getHistoryIcons().push(marker);if(oldMarker!=null){this.removeMarker(track,oldMarker);}
this.addMarkerOverlay(marker);var mapManager=this;var gEventListener=GEvent.addListener(marker,"click",function(overlay,point){net.watchee.util.log.info("marker clicked track="+track+" gpsData="+gpsData);var marker=this;mapManager.showInfoWindow(track,gpsData,true);track.watchee.mapMarkerClicked(track,gpsData,marker);});return marker;}
return null;}
this.addMarkerOverlay=function(marker){if(!org.xmlBlaster.util.isDefined(marker))return;this.map.addOverlay(marker);}
this.removeMarker=function(track,marker){this.map.removeOverlay(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.map.addOverlay(polyLine);}
this.removeLineOverlay=function(polyLine){if(!org.xmlBlaster.util.isDefined(polyLine))return;this.map.removeOverlay(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(new GLatLng(gpsData.latitudeDecimal(),gpsData.longitudeDecimal()));}
if(track.historyPolyline!=null){this.removeLineOverlay(track.historyPolyline);track.historyPolyline=null;}
track.historyPolyline=new GPolyline(coord,segmentDisplayInfo.getSegmentColor(),segmentDisplayInfo.getSegmentWidth());this.addLineOverlay(track.historyPolyline);var gEventListener=GEvent.addListener(track.historyPolyline,"click",function(overlay,point){var gPolyline=this;track.watchee.mapLineClicked(track,gpsData,gPolyline);var device=track.getDevice();var eventManager=device.getEventManager();});return track.historyPolyline;}
this.createInfoWindowTabArr=function(track,gpsData){var deviceId=track.getDeviceId();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)?"":"<img class='locationPicture' src='"+pictureInfo.url+"' align='middle' alt='Photo "+pictureInfo.pictureId+"' width='"+pictureInfo.width+"' height='"+pictureInfo.height+"'/>";var trackLength=" ";if(!gpsData.isValid()){trackLength="INVALID";}
else if(track.historyPolyline!=null){trackLength=org.xmlBlaster.util.toNiceDistance(track.historyPolyline.getLength());trackLength+=" m";if(trackLength.indexOf("undef")!=-1)
trackLength="---";}
var footer=track.getDevice().getBuddyTO().getAliasAndLoginName();if(track.isCurrentTrack())
footer+=" <i>live</i>";else
footer+=" <i>history</i>";var gpsHtml=this.getGpsHtml(gpsData,info,trackLength,footer,photoMarkup);var gpsDetailHtml=this.getGpsDetailHtml(gpsData,track);var infoTabs=[new GInfoWindowTab("Info",gpsHtml),new GInfoWindowTab("Detail",gpsDetailHtml)];return infoTabs;}
this.showInfoWindow=function(track,gpsData,showDirectly){var showDirectly=org.xmlBlaster.util.toBoolean(showDirectly,true);var infoTabs=this.createInfoWindowTabArr(track,gpsData)
var gLatLng=gpsData.isValid()?this.getLatLng(gpsData):this.map.getCenter();if(!gpsData.isValid()){var locationPictureId=gpsData.getPictureId(null);if(locationPictureId==null){return null;}}
if(showDirectly){this.map.openInfoWindowTabsHtml(gLatLng,infoTabs);this.infoWindowTrackName=track.getTrackName();}
return infoTabs;}
this.getXsmsHtml=function(xsms){if(this.mapInfoGui!=null)
return this.mapInfoGui.getXsmsHtml(xsms);return null;}
this.getGpsHtml=function(gpsData,header,trackLength,footer,photoMarkup){if(this.mapInfoGui!=null)
return this.mapInfoGui.getGpsHtml(gpsData,header,trackLength,footer,photoMarkup);return null;}
this.getGpsDetailHtml=function(gpsData,track){if(this.mapInfoGui!=null)
return this.mapInfoGui.getGpsDetailHtml(gpsData,track);return null;}
this.showXsmsInfoWindow=function(xsms,gpsData){var xsmsHtml=this.getXsmsHtml(xsms);var gpsHtml=this.getGpsHtml(gpsData,null,null,null,null);var gpsDetailHtml=this.getGpsDetailHtml(gpsData,null);var gpsHtml="<table><tr><td>"+gpsHtml+"</td><td>"+gpsDetailHtml+"</td></tr></table>";var infoTabs=[new GInfoWindowTab("xSMS",xsmsHtml),new GInfoWindowTab("GPS",gpsHtml)];var gLatLng=gpsData.isValid()?this.getLatLng(gpsData):this.map.getCenter();if(!gpsData.isValid()||gpsData.hasLatLonNull()){return null;}
this.map.openInfoWindowTabsHtml(gLatLng,infoTabs);return infoTabs;}
this.shutdown=function(){if(this.map!=null){GUnload();this.map=null;}}}