
if(typeof net.watchee.google=="undefined"||net.watchee.google==null){net.watchee.google=new Object();}
function Rectangle(bounds,opt_weight,opt_color){this.bounds_=bounds;this.weight_=opt_weight||2;this.color_=opt_color||"#FF0000";this.showBackground=false;}
if(typeof GOverlay!="undefined"){Rectangle.prototype=new GOverlay();}
Rectangle.prototype.initialize=function(map){var div=document.createElement("div");div.style.position="absolute";this.showBackground=org.xmlBlaster.util.isFF||org.xmlBlaster.util.isOpera;if(this.showBackground){div.style.margin="0px";div.style.padding="0px";div.style.border="0px solid red";div.style.backgroundColor="red";div.style.mozOpacity=0.2;div.style.opacity=0.2;}
else{div.style.border=this.weight_+"px solid "+this.color_;}
map.getPane(G_MAP_MAP_PANE).appendChild(div);this.map_=map;this.div_=div;}
Rectangle.prototype.remove=function(){this.div_.parentNode.removeChild(this.div_);}
Rectangle.prototype.copy=function(){return new Rectangle(this.bounds_,this.weight_,this.color_,this.backgroundColor_,this.opacity_);}
Rectangle.prototype.redraw=function(force){if(!force)return;var c1=this.map_.fromLatLngToDivPixel(this.bounds_.getSouthWest());var c2=this.map_.fromLatLngToDivPixel(this.bounds_.getNorthEast());var marcelRectangleCorrection=true;if(marcelRectangleCorrection){if(this.showBackground){c1.x+=2;c1.y+=2;c2.x+=2;c2.y+=2;}
else{c1.y-=1;c2.y+=1;}}
this.div_.style.width=Math.abs(c2.x-c1.x)+"px";this.div_.style.height=Math.abs(c2.y-c1.y)+"px";this.div_.style.left=(Math.min(c2.x,c1.x)-this.weight_)+"px";this.div_.style.top=(Math.min(c2.y,c1.y)-this.weight_)+"px";}
net.watchee.google.GeofenceDrawer=function(map,callbackFp,callbackThisArg,callbackObj){this.map=map;this.callbackFp=callbackFp||null;this.callbackThisArg=callbackThisArg||this;this.callbackObj=callbackObj||null;this.geofencePolygon=null;this.gLatLngSE=null;this.gLatLngNW=null;this.markerNW=null;this.dragendListenerNW=null;this.markerSE=null;this.dragendListenerSE=null;this.addMarkerNW=function(marker){this.markerNW=marker;this.gLatLngNW=marker.getLatLng();this.gLatLngNWorig=this.gLatLngNW;var geofenceDrawer=this;this.dragendListenerNW=GEvent.addListener(this.markerNW,"dragend",function(){var newLatLngNW=geofenceDrawer.markerNW.getLatLng();if(newLatLngNW.lat()<=geofenceDrawer.gLatLngSE.lat()||newLatLngNW.lng()>=geofenceDrawer.gLatLngSE.lng()){geofenceDrawer.markerNW.setLatLng(geofenceDrawer.gLatLngNW);return;}
geofenceDrawer.gLatLngNW=newLatLngNW;geofenceDrawer.drawPolygon(geofenceDrawer);});this.map.addOverlay(this.markerNW);}
this.addMarkerSE=function(marker){this.markerSE=marker;this.gLatLngSE=marker.getLatLng();this.gLatLngSEorig=this.gLatLngSE;var geofenceDrawer=this;this.dragendListenerSE=GEvent.addListener(this.markerSE,"dragend",function(){var newLatLngSE=geofenceDrawer.markerSE.getLatLng();if(newLatLngSE.lat()>=geofenceDrawer.gLatLngNW.lat()||newLatLngSE.lng()<=geofenceDrawer.gLatLngNW.lng()){geofenceDrawer.markerSE.setLatLng(geofenceDrawer.gLatLngSE);return;}
geofenceDrawer.gLatLngSE=newLatLngSE;geofenceDrawer.drawPolygon(geofenceDrawer);});this.map.addOverlay(this.markerSE);}
this.drawPolygon=function(){this.clearPolygon();var gLatLngSW=new GLatLng(this.gLatLngSE.lat(),this.gLatLngNW.lng());var gLatLngNE=new GLatLng(this.gLatLngNW.lat(),this.gLatLngSE.lng());var rectBounds=new GLatLngBounds(gLatLngSW,gLatLngNE);this.geofencePolygon=new Rectangle(rectBounds);this.map.addOverlay(this.geofencePolygon);if(this.callbackFp!=null){this.callbackFp.call(this.callbackThisArg,this,this.callbackObj);}}
this.getKmlNW=function(){return net.watchee.Coordinate.round(this.gLatLngNW.lng())+","+net.watchee.Coordinate.round(this.gLatLngNW.lat());}
this.getKmlSE=function(){return net.watchee.Coordinate.round(this.gLatLngSE.lng())+","+net.watchee.Coordinate.round(this.gLatLngSE.lat());}
this.getKmlStr=function(){return this.getKmlNW()+" "+this.getKmlSE();}
this.clearAll=function(){this.clearMarkers();this.clearListeners();this.clearPolygon();}
this.clearMarkers=function(){if(this.markerNW!=null)
this.map.removeOverlay(this.markerNW);this.markerNW=null;if(this.markerSE!=null)
this.map.removeOverlay(this.markerSE);this.markerSE=null;}
this.clearListeners=function(){if(this.dragendListenerNW!=null)
GEvent.removeListener(this.dragendListenerNW);this.dragendListenerNW=null;if(this.dragendListenerSE!=null)
GEvent.removeListener(this.dragendListenerSE);this.dragendListenerSE=null;}
this.clearPolygon=function(){if(this.geofencePolygon!=null)
this.map.removeOverlay(this.geofencePolygon);this.geofencePolygon=null;}
this.draw=function(coordinates){var bounds=this.map.getBounds();var southWest=bounds.getSouthWest();var northEast=bounds.getNorthEast();var lngW=southWest.lng()+(northEast.lng()-southWest.lng())/4;var lngE=southWest.lng()+(northEast.lng()-southWest.lng())*3/4;var latS=southWest.lat()+(northEast.lat()-southWest.lat())/4;var latN=southWest.lat()+(northEast.lat()-southWest.lat())*3/4;if(coordinates!=null&&coordinates.size()>=2){var coordinate0=coordinates.getCoordinate(0);lngW=coordinate0.getLng();latN=coordinate0.getLat();var coordinate1=coordinates.getCoordinate(1);lngE=coordinate1.getLng();latS=coordinate1.getLat();var currentLatLng=new GLatLng((latN+latS)/2,(lngW+lngE)/2);this.map.setCenter(currentLatLng);}
this.addMarkerNW(new GMarker(new GLatLng(latN,lngW),{draggable:true}));this.addMarkerSE(new GMarker(new GLatLng(latS,lngE),{draggable:true}));this.drawPolygon();}}