
var EsnGmap = new Class({
  initialize: function(container,center_lat,center_long){
    this.container=container;
    this.center_lat=center_lat;
    this.center_long=center_long;

    if (GBrowserIsCompatible()) { 
      this.gmap = new GMap2(container);
      this.gmap.addControl(new GLargeMapControl());
      this.gmap.setCenter(new GLatLng(center_lat,center_long),10);
    } else alert("Sorry, the Google Maps API is not compatible with this browser");

  },  
    
  createMarker: function(lat,long,current,html) {
    var Icon = new GIcon();
    if(current) Icon.image = '/images/google-marker-esn-logo.png';
    else Icon.image = '/images/google-marker-esn-logo.png';
    Icon.iconSize = new GSize(60,75);
    Icon.shadow = '/images/google-marker-esn-logo-shadow.png';
    Icon.shadowSize = new GSize(125,75);
    Icon.iconAnchor = new GPoint(28,75);
    Icon.infoWindowAnchor = new GPoint(28,75);

    var marker = new GMarker(new GLatLng(lat,long),{ icon:Icon });
    GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml(html);
    });
    this.gmap.addOverlay(marker);
  },

  drawCircle: function(circleRadius,circleUnits,bordercol,innercol,opacity){
  	if (this.circle) this.gmap.removeOverlay(circle);  
  	var center = this.gmap.getCenter();
  	var bounds = new GLatLngBounds();
  	var circlePoints = Array();
  	with (Math) {
  		if (circleUnits == 'KM') var d = circleRadius/6378.8;	// radians
  		else var d = circleRadius/3963.189;	// radians
   		var lat1 = (PI/180)* center.lat(); // radians
  		var lng1 = (PI/180)* center.lng(); // radians
  
  		for (var a = 0 ; a < 361 ; a++ ) {
  			var tc = (PI/180)*a;
  			var y = asin(sin(lat1)*cos(d)+cos(lat1)*sin(d)*cos(tc));
  			var dlng = atan2(sin(tc)*sin(d)*cos(lat1),cos(d)-sin(lat1)*sin(y));
  			var x = ((lng1-dlng+PI) % (2*PI)) - PI ; // MOD function
  			var point = new GLatLng(parseFloat(y*(180/PI)),parseFloat(x*(180/PI)));
  			circlePoints.push(point);
  			bounds.extend(point);
  		}
  
  		if (d < 1.5678565720686044) this.circle = new GPolygon(circlePoints, bordercol, 2, 1, innercol,opacity);	
  		else this.circle = new GPolygon(circlePoints,  bordercol, 2, 1);	

  		this.gmap.addOverlay(this.circle); 
  		this.gmap.setZoom(this.gmap.getBoundsZoomLevel(bounds));
  	}
  }

})    
