// 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 = new 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;
}

function initialize() {
  var map;
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map_canvas"));
    map.addControl(new GLargeMapControl());
    
    map.setCenter(new GLatLng(-44.005657, 170.480518), 14);
    
	var accommIcon = new GIcon(G_DEFAULT_ICON);
	accommIcon.image = "http://www.newzealandtravelinsider.com/images/accomm.png";

	var blackIcon = new GIcon(G_DEFAULT_ICON);
	blackIcon.image = "http://www.newzealandtravelinsider.com/images/black05.png";
	blackIcon.iconSize = new GSize(32, 37);

	var blueIcon = new GIcon(G_DEFAULT_ICON);
	blueIcon.image = "http://www.newzealandtravelinsider.com/images/blue04.png";
	blueIcon.iconSize = new GSize(32, 37);

	var orangeIcon = new GIcon(G_DEFAULT_ICON);
	orangeIcon.image = "http://www.newzealandtravelinsider.com/images/orange03.png";
	orangeIcon.iconSize = new GSize(32, 37);
	                
	moHotel1 = { icon:blueIcon };
	moHotel1.title = "Lake Tekapo Scenic Resort";
	var h1Marker = new GMarker(new GLatLng(-44.004175, 170.476913), moHotel1);
    map.addOverlay(h1Marker);
    GEvent.addListener(h1Marker, "click", function() {
	    h1Marker.openInfoWindowHtml('<strong>Lake Tekapo Scenic Resort</strong><br/><a target="blank" href="http://www.laketekapo.com/">http://www.laketekapo.com</a>');
	  });

	moHotel2 = { icon:blackIcon };
	moHotel2.title = "Peppers Bluewater Resort";
	var h2Marker = new GMarker(new GLatLng(-44.003187, 170.47348), moHotel2);
    map.addOverlay(h2Marker);
    GEvent.addListener(h2Marker, "click", function() {
	    h2Marker.openInfoWindowHtml('<strong>Peppers Bluewater Resort</strong><br/><a target="blank" href="http://www.peppers.co.nz/bluewater/">http://www.peppers.co.nz/bluewater</a>');
	  });

	moHotel3 = { icon:accommIcon };
	moHotel3.title = "The Godley Hotel";
	var h3Marker = new GMarker(new GLatLng(-44.005425, 170.479145), moHotel3);
    map.addOverlay(h3Marker);
    GEvent.addListener(h3Marker, "click", function() {
	    h3Marker.openInfoWindowHtml('<strong>The Godley Hotel</strong><br/><a target="blank" href="http://www.tekapo.co.nz/">http://www.tekapo.co.nz</a>');
	  });

	moHotel4 = { icon:accommIcon };
	moHotel4.title = "The Chalet Boutique Motel";
	var h4Marker = new GMarker(new GLatLng(-44.004499, 170.484681), moHotel4);
    map.addOverlay(h4Marker);
    GEvent.addListener(h4Marker, "click", function() {
	    h4Marker.openInfoWindowHtml('<strong>The Chalet Boutique Motel</strong><br/><a target="blank" href="http://www.tekapotourism.co.nz/accomm/chalet.html">http://www.tekapotourism.co.nz/accomm/chalet.html</a>');
	  });

	var circleRadius2 = 1.86; //miles = 3 km
	circle2 = new CircleOverlay(map.getCenter(), circleRadius2, "#004EFF", 1, 0.15, "#004EFF", 0.15);	
	map.addOverlay(circle2);

	var circleRadius1 = 1.24; //miles = 2 km
	circle1 = new CircleOverlay(map.getCenter(), circleRadius1, "#FFFC00", 1, 0.4, "#FFFC00", 0.4);	
	map.addOverlay(circle1);

	var circleRadius = 0.62; //miles = 1 km
	circle = new CircleOverlay(map.getCenter(), circleRadius, "#0CFF00", 1, 0.25, "#0CFF00", 0.25);	
	map.addOverlay(circle);

    map.setMapType(G_NORMAL_MAP);        
  
	} 
}

