
if(typeof net.watchee.openlayers=="undefined"||net.watchee.openlayers==null){net.watchee.openlayers=new Object();}
net.watchee.openlayers.GeofenceDrawer=function(mapManager,callbackFp,callbackThisArg,callbackObj){this.mapManager=mapManager;this.map=mapManager.getNativeMapHandle();this.callbackFp=callbackFp||null;this.callbackThisArg=callbackThisArg||this;this.callbackObj=callbackObj||null;this.geofenceMarkersLayer=new OpenLayers.Layer.Vector("Geofence Marker Layer");this.geofenceLayer=new OpenLayers.Layer.Vector("Geofence Layer");this.map.addLayers([this.geofenceMarkersLayer,this.geofenceLayer]);this.geofenceSquare=null;this.markerNW=null;this.markerSE=null;var _this=this;this.__onDragPointComplete=function(feature,pixel){var point=feature;var dragFeature=this;var latLngSE=null;var latLngNW=null;var x=feature.geometry.x;var y=feature.geometry.y;if(point.userData=="NW"){if(y<=_this.markerSE.geometry.y||x>=_this.markerSE.geometry.x){point.move(point.lastLonLat);return;}
latLngSE=_this.createLonLat(_this.markerSE.geometry.x,_this.markerSE.geometry.y);latLngNW=_this.createLonLat(x,y);}
else{if(y>=_this.markerNW.geometry.y||x<=_this.markerNW.geometry.x){point.move(point.lastLonLat);return;}
latLngSE=_this.createLonLat(feature.geometry.x,feature.geometry.y);latLngNW=_this.createLonLat(_this.markerNW.geometry.x,_this.markerNW.geometry.y);}
point.lastLonLat=_this.createLonLat(point.geometry.x,point.geometry.y);_this.__drawGeofenceSquare2(latLngSE,latLngNW);}
this.markersDragFeature=new OpenLayers.Control.DragFeature(this.geofenceMarkersLayer,{onComplete:this.__onDragPointComplete});this.map.addControl(this.markersDragFeature);this.__createIcon=function(lon,lat,userData){var point=new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(lon,lat));point.style=this.mapManager.createIconPointStyle("images/marker/watchee-point.png");this.geofenceMarkersLayer.addFeatures([point]);point.userData=userData;point.lastLonLat=this.createLonLat(point.geometry.x,point.geometry.y);return point;}
this.createLonLatFromDegrees=function(lonDeg,latDeg){return this.mapManager.createLonLat(lonDeg,latDeg);}
this.createLonLat=function(lon,lat){return new OpenLayers.LonLat(lon,lat);}
this.__drawGeofenceSquare=function(lngW,latS,lngE,latN){this.__clearGeofenceSquare();var poly=new OpenLayers.Bounds(lngW,latS,lngE,latN).toGeometry();var style_red={strokeColor:"#FF0000",strokeOpacity:0.2,strokeWidth:1,fillColor:"#FF0000",fillOpacity:0.2};this.geofenceSquare=new OpenLayers.Feature.Vector(poly,null,style_red);this.geofenceLayer.addFeatures([this.geofenceSquare]);if(this.callbackFp!=null){this.callbackFp.call(this.callbackThisArg,this,this.callbackObj);}
return this.geofenceSquare;}
this.__drawGeofenceSquare2=function(latLngSE,latLngNW){return this.__drawGeofenceSquare(latLngNW.lon,latLngSE.lat,latLngSE.lon,latLngNW.lat);}
this.getKmlNW=function(){var lonLat=this.mapManager.fromMercatorToDegreeIfNeeded(this.markerNW.geometry.x,this.markerNW.geometry.y);return net.watchee.Coordinate.round(lonLat.lon)+","+net.watchee.Coordinate.round(lonLat.lat);}
this.getKmlSE=function(){var lonLat=this.mapManager.fromMercatorToDegreeIfNeeded(this.markerSE.geometry.x,this.markerSE.geometry.y);return net.watchee.Coordinate.round(lonLat.lon)+","+net.watchee.Coordinate.round(lonLat.lat);}
this.getKmlStr=function(){return this.getKmlNW()+" "+this.getKmlSE();}
this.draw=function(coordinates){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 lngW=southWestLng+(northEastLng-southWestLng)/4;var lngE=southWestLng+(northEastLng-southWestLng)*3/4;var latS=southWestLat+(northEastLat-southWestLat)/4;var latN=southWestLat+(northEastLat-southWestLat)*3/4;if(coordinates!=null&&coordinates.size()>=2){var coordinate0=coordinates.getCoordinate(0);var lonLatNW=this.createLonLatFromDegrees(coordinate0.getLng(),coordinate0.getLat());lngW=lonLatNW.lon;latN=lonLatNW.lat;var coordinate1=coordinates.getCoordinate(1);var lonLatSE=this.createLonLatFromDegrees(coordinate1.getLng(),coordinate1.getLat());lngE=lonLatSE.lon;latS=lonLatSE.lat;var bounds=new OpenLayers.Bounds(lngW-(lngE-lngW),latS-(latN-latS),lngE+(lngE-lngW),latN+(latN-latS));var zoom=this.map.getZoomForExtent(bounds,true);this.map.setCenter(this.createLonLat((lngW+lngE)/2,(latN+latS)/2),zoom,false,false);}
this.markersDragFeature.activate();this.markerNW=this.__createIcon(lngW,latN,"NW");this.markerSE=this.__createIcon(lngE,latS,"SE");this.__drawGeofenceSquare(lngW,latS,lngE,latN);}
this.__clearGeofenceSquare=function(){if(this.geofenceLayer!=null){this.geofenceLayer.destroyFeatures();this.geofenceSquare=null;}}
this.clearMarkers=function(){if(this.geofenceMarkersLayer==null)
return;if(this.markerNW!=null)
this.geofenceMarkersLayer.removeFeatures([this.markerNW]);this.markerNW=null;if(this.markerSE!=null)
this.geofenceMarkersLayer.removeFeatures([this.markerSE]);this.markerSE=null;}
this.clearListeners=function(){if(this.markersDragFeature!=null){this.markersDragFeature.deactivate();this.map.removeControl(this.markersDragFeature);this.markersDragFeature=null;}}
this.clearAll=function(){if(this.geofenceLayer==null)
return;this.clearMarkers();this.clearListeners();this.__clearGeofenceSquare();this.map.removeLayer(this.geofenceLayer);this.geofenceLayer=null;this.map.removeLayer(this.geofenceMarkersLayer);this.geofenceMarkersLayer=null;}}
