///////////////////////////////////////
// 
// Circle overlay extension for Google Maps
// App Delegate Inc <http://appdelegateinc.com> 2010
//
// This file adds a new CircleOverlay to GMaps2 to draw 
// a circle on a map with stroke and fill. 
// It was extended for OUL to interact with elements outside
// the map. 
//
// source: http://appdelegateinc.com/blog/2010/05/17/google-maps-circle-overlay/
// modified: Christoph Peschel <cp@hirnstrom.de>
//
//////////////////////////////////////

// Constructor
var CircleOverlay = function(latLng, radius, strokeColor, strokeWidth, strokeOpacity, fillColor, fillOpacity, numPoints, outsideElement) {
  this.latLng = latLng;
  this.radius = radius;
  this.strokeColor = strokeColor;
  this.strokeWidth = strokeWidth;
  this.strokeOpacity = strokeOpacity;
  this.fillColor = fillColor;
  this.fillOpacity = fillOpacity;
  this.originalFillOpacity = fillOpacity;
  this.originalStrokeOpacity = strokeOpacity;
  this.outsideElement = outsideElement;
  
  // Set resolution of polygon
  if (typeof(numPoints) == 'undefined') {
    this.numPoints = 40
  } else {
    this.numPoints = numPoints;
  }
}

// Inherit from GOverlay
CircleOverlay.prototype = GOverlay;

// GMaps initialize callback
CircleOverlay.prototype.initialize = function(map) {
  this.map = map;
}

// Reset overlay
CircleOverlay.prototype.clear = function() {
  if(this.polygon != null && this.map != null) {
    this.map.removeOverlay(this.polygon);
  }
}

// Calculate all the points of the circle and draw them
CircleOverlay.prototype.redraw = function(force) {
  var d2r = Math.PI / 180;
  circleLatLngs = new Array();
  
  // Convert statute miles into degrees latitude
  var circleLat = this.radius * 0.014483;
  var circleLng = circleLat / Math.cos(this.latLng.lat() * d2r);
  
  // Create polygon points (extra point to close polygon)
  for (var i = 0; i < this.numPoints + 1; i++) { 
    // Convert degrees to radians
    var theta = Math.PI * (i / (this.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.polygon.outsideElement = this.outsideElement;
  this.polygon.overlay = this;
  
  GEvent.addListener(this.polygon, "click", function() {
	  elem = document.getElementById('tr-sendereichweiten-' + this.outsideElement);
	  if(elem.className == 'sendereichweiten-highlight') elem.className = 'sendereichweiten-hover';
	  else
	  {
		  //clear all 
		  $('.sendereichweiten-highlight').removeClass();
		  
		  //highlight this one
		  elem.className = 'sendereichweiten-highlight';
		  location.hash = 'tabelle';
	  }
  });
  
  GEvent.addListener(this.polygon, "mouseover", function() {
	  elem = document.getElementById('tr-sendereichweiten-' + this.outsideElement);
	  if(elem.className != 'sendereichweiten-highlight') elem.className = 'sendereichweiten-hover';
	  
	  this.overlay.hover();
  });
 
  GEvent.addListener(this.polygon, "mouseout", function() {
	  elem = document.getElementById('tr-sendereichweiten-' + this.outsideElement);
	  if(elem.className != 'sendereichweiten-highlight') elem.className = ''; 
	  
	  this.overlay.unhover();
  });

  
  this.map.addOverlay(this.polygon);
}

// Remove circle method
CircleOverlay.prototype.remove = function() {
  this.clear();
}

// Can use this method if the library at is included at http://appdelegateinc.com/blog/2010/05/16/point-in-polygon-checking/
CircleOverlay.prototype.containsLatLng = function(latLng) {
  if(this.polygon.containsLatLng) {
    return this.polygon.containsLatLng(latLng);
  }
}

// Set radius of circle
CircleOverlay.prototype.setRadius = function(radius) {
  this.radius = radius;
}

// Set center of circle
CircleOverlay.prototype.setLatLng = function(latLng) {
  this.latLng = latLng;
}

CircleOverlay.prototype.hover = function() {
	this.fillOpacity = this.fillOpacity * 1.4
	this.strokeOpacity = this.strokeOpacity * 1.4
	this.redraw();
}

CircleOverlay.prototype.unhover = function() {
	this.fillOpacity = this.originalFillOpacity;
	this.strokeOpacity = this.originalStrokeOpacity;
	this.redraw();
}

