// This file adds a new circle overlay to GMaps2
// it is really a many-pointed polygon, but look smooth enough to be a circle.
var CircleOverlay = function(latLng, radius, strokeColor, strokeWidth, strokeOpacity, fillColor, fillOpacity) {
	this.latLng = latLng;
	this.radius = radius;
	this.strokeColor = strokeColor;
	this.strokeWidth = strokeWidth;
	this.strokeOpacity = strokeOpacity;
	this.fillColor = fillColor;
	this.fillOpacity = fillOpacity;
}

// Implements GOverlay interface
CircleOverlay.prototype = GOverlay;

CircleOverlay.prototype.initialize = function(map) {
	this.map = map;
}

CircleOverlay.prototype.clear = function() {
	if(this.polygon != null && this.map != null) {
		this.map.removeOverlay(this.polygon);
	}
}

// Calculate all the points and draw them
CircleOverlay.prototype.redraw = function(force) {
	var d2r = Math.PI / 180;
	circleLatLngs = new Array();
	var circleLat = this.radius * 0.014483;  // Convert statute miles into degrees latitude
	var circleLng = circleLat / Math.cos(this.latLng.lat() * d2r);
	var numPoints = 40;
	
	// 2PI = 360 degrees, +1 so that the end points meet
	for (var i = 0; i < numPoints + 1; i++) { 
		var theta = Math.PI * (i / (numPoints / 2)); 
		var vertexLat = this.latLng.lat() + (circleLat * Math.sin(theta)); 
		var vertexLng = this.latLng.lng() + (circleLng * Math.cos(theta));
		var vertextLatLng = new GLatLng(vertexLat, vertexLng);
		circleLatLngs.push(vertextLatLng); 
	}
	
	this.clear();
	this.polygon = new GPolygon(circleLatLngs, this.strokeColor, this.strokeWidth, this.strokeOpacity, this.fillColor, this.fillOpacity);
	this.map.addOverlay(this.polygon);
}

CircleOverlay.prototype.remove = function() {
	this.clear();
}

CircleOverlay.prototype.containsLatLng = function(latLng) {
	// Polygon Point in poly 
	if(this.polygon.containsLatLng) {
		return this.polygon.containsLatLng(latLng);
	}
}

CircleOverlay.prototype.setRadius = function(radius) {
	this.radius = radius;
}

CircleOverlay.prototype.setLatLng = function(latLng) {
	this.latLng = latLng;
}

    // A Rectangle is a simple overlay that outlines a lat/lng bounds on the
    // map. It has a border of the given weight and color and can optionally
    // have a semi-transparent background color.
   var Rectangle = function(latLng, radius,opt_weight, opt_color) {
      this.latLng = latLng;
      this.radius = radius;

      this.bounds_ = null;
      this.weight_ = opt_weight || 2;
      this.color_ = opt_color || "#888888";      
    }

    Rectangle.prototype = new GOverlay;

    // Creates the DIV representing this rectangle.
    Rectangle.prototype.initialize = function(map) {
   // Create the DIV representing our rectangle
      var div = document.createElement("div");
      div.style.border = this.weight_ + "px solid " + this.color_;
      div.style.position = "absolute";

      // Our rectangle is flat against the map, so we add our selves to the
      // MAP_PANE pane, which is at the same z-index as the map itself (i.e.,
      // below the marker shadows)
      map.getPane(G_MAP_MAP_PANE).appendChild(div);

      this.map_ = map;
      this.div_ = div;

    }

    // Copy our data to a new Rectangle
    Rectangle.prototype.copy = function() {
      return new Rectangle(this.bounds_, this.weight_, this.color_,
                           this.backgroundColor_, this.opacity_);
    }

    // Redraw the rectangle based on the current projection and zoom level
    Rectangle.prototype.redraw = function(force) {
     
      var longitud = this.latLng.lng();
      var latitud  = this.latLng.lat();
	
      var lonmin  = (longitud*1) - this.radius ;
      var latmin  = (latitud*1) - this.radius ;
      var lonmax  = (longitud*1) + this.radius ;
      var latmax  = (latitud*1) + this.radius ;

      this.bounds_ = new GLatLngBounds(
		          new GLatLng(latmin,lonmin),
		          new GLatLng(latmax,lonmax));

      // We only need to redraw if the coordinate system has changed
      if (!force) return;

      // Calculate the DIV coordinates of two opposite corners of our bounds to
      // get the size and position of our rectangle
      var c1 = this.map_.fromLatLngToDivPixel(this.bounds_.getSouthWest());
      var c2 = this.map_.fromLatLngToDivPixel(this.bounds_.getNorthEast());

      // Now position our DIV based on the DIV coordinates of our bounds
      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";
    }

  Rectangle.prototype.clear = function() {
     if(this.div_!= null && this.map != null) {
      this.div_.parentNode.removeChild(this.div_);
     }
   }

Rectangle.prototype.setRadius = function(radius) {
  this.radius = radius;
}

Rectangle.prototype.setLatLng = function(latLng) {
  this.latLng = latLng;
}
Rectangle.prototype.remove = function() {
 this.clear();
}