// 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(-43.487864, 172.540026), 13);
    
	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);

	moHotel0 = { icon:accommIcon };
	moHotel0.title = "Aarburg Airport Motel";
	var h0Marker = new GMarker(new GLatLng(-43.496768, 172.563457), moHotel0);
    map.addOverlay(h0Marker);
    GEvent.addListener(h0Marker, "click", function() {
	    h0Marker.openInfoWindowHtml("<strong>Aarburg Airport Motel</strong><br/><a target='blank' href='http://www.aarburgmotel.co.nz/'>http://www.aarburgmotel.co.nz</a>");
	  });
	                
	moHotel1 = { icon:blueIcon };
	moHotel1.title = "Airport Christchurch Luxury Motel & Apartments";
	var h1Marker = new GMarker(new GLatLng(-43.498636, 172.561483), moHotel1);
    map.addOverlay(h1Marker);
    GEvent.addListener(h1Marker, "click", function() {
	    h1Marker.openInfoWindowHtml('<strong>Airport Christchurch Luxury Motel & Apartments</strong><br/><a target="blank" href="http://www.airportchristchurch.co.nz/">http://www.airportchristchurch.co.nz</a>');
	  });

	moHotel2 = { icon:blueIcon };
	moHotel2.title = "Copthorne Hotel Commodore Christchurch Airport ";
	var h2Marker = new GMarker(new GLatLng(-43.49739, 172.556248), moHotel2);
    map.addOverlay(h2Marker);
    GEvent.addListener(h2Marker, "click", function() {
	    h2Marker.openInfoWindowHtml('<strong>Copthorne Hotel Commodore Christchurch Airport </strong><br/><a target="blank" href="http://www.commodore.net.nz/">http://www.commodore.net.nz</a>');
	  });

	moHotel3 = { icon:blueIcon };
	moHotel3.title = "Airport Gateway Motor Lodge";
	var h3Marker = new GMarker(new GLatLng(-43.499258, 172.560711), moHotel3);
    map.addOverlay(h3Marker);
    GEvent.addListener(h3Marker, "click", function() {
	    h3Marker.openInfoWindowHtml('<strong>Airport Gateway Motor Lodge</strong><br/><a target="blank" href="http://www.airportgateway.co.nz/">http://www.airportgateway.co.nz</a>');
	  });

	moHotel4 = { icon:blueIcon };
	moHotel4.title = "Chardonnay Motor Lodge";
	var h4Marker = new GMarker(new GLatLng(-43.452545, 172.610567), moHotel4);
    map.addOverlay(h4Marker);
    GEvent.addListener(h4Marker, "click", function() {
	    h4Marker.openInfoWindowHtml('<strong>Chardonnay Motor Lodge</strong><br/><a target="blank" href="http://www.chardonnaymotorlodge.co.nz/">http://www.chardonnaymotorlodge.co.nz</a>');
	  });

	moHotel5 = { icon:orangeIcon };
	moHotel5.title = "Belle Bonne Motel";
	var h5Marker = new GMarker(new GLatLng(-43.528638, 172.560883), moHotel5);
    map.addOverlay(h5Marker);
    GEvent.addListener(h5Marker, "click", function() {
	    h5Marker.openInfoWindowHtml('<strong>Belle Bonne Motel</strong><br/><a target="blank" href="http://www.bellebonne.co.nz/">http://www.bellebonne.co.nz</a>');
	  });

	moHotel6 = { icon:accommIcon };
	moHotel6.title = "Aloha Motel Christchurch";
	var h6Marker = new GMarker(new GLatLng(-43.534238, 172.563286), moHotel6);
    map.addOverlay(h6Marker);
    GEvent.addListener(h6Marker, "click", function() {
	    h6Marker.openInfoWindowHtml('<strong>Aloha Motel Christchurch</strong><br/><a target="blank" href="http://www.alohamotel.co.nz/">http://www.alohamotel.co.nz</a>');
	  });

	moHotel7 = { icon:accommIcon };
	moHotel7.title = "Denton Park Motel";
	var h7Marker = new GMarker(new GLatLng(-43.542451, 172.520199), moHotel7);
    map.addOverlay(h7Marker);
    GEvent.addListener(h7Marker, "click", function() {
	    h7Marker.openInfoWindowHtml('<strong>Denton Park Motel</strong><br/><a target="blank" href="http://www.dentonpark.co.nz/">http://www.dentonpark.co.nz</a>');
	  });

	moHotel8 = { icon:accommIcon };
	moHotel8.title = "Middlepark Motel";
	var h8Marker = new GMarker(new GLatLng(-43.536976, 172.557793), moHotel8);
    map.addOverlay(h8Marker);
    GEvent.addListener(h8Marker, "click", function() {
	    h8Marker.openInfoWindowHtml('<strong>Middlepark Motel</strong><br/><a target="blank" href="http://www.middleparkmotel.co.nz/">http://www.middleparkmotel.co.nz</a>');
	  });

	moHotel9 = { icon:orangeIcon };
	moHotel9.title = "Sudima Hotel Christchurch Airport";
	var h9Marker = new GMarker(new GLatLng(-43.489918, 172.545776), moHotel9);
    map.addOverlay(h9Marker);
    GEvent.addListener(h9Marker, "click", function() {
	    h9Marker.openInfoWindowHtml('<strong>Sudima Hotel Christchurch Airport</strong><br/><a target="blank" href="http://www.sudimachristchurch.co.nz/">http://www.sudimachristchurch.co.nz</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);        
  
	} 
}

