﻿
// Builds an array of geocode responses for the 5 cities.
var city = [
      {
          name: "Netherlands",
          Status: { code: 200, request: "geocode" },
          Placemark: [{ address: "6500 AB Nijmegen Westkanaaldijk 439 6545 AA Nijmegen The Netherlands",
              Point: { coordinates: [5.8136567, 51.8446042, 0]}}]
          },
      {
          name: "Gretna, LA",
          Status: { code: 200, request: "geocode" },
          Placemark: [{ address: "2550 Belle Chasse hwy Gretna, LA 70053",
              Point: { coordinates: [-90.046332, 29.899726, 0]}}]
          },
      {
          name: "Duncanville, AL",
          Status: { code: 200, request: "geocode" },
          Placemark: [{ address: "10920 hwy 82 east Duncanville, AL 35456",
              Point: { coordinates: [-87.4763, 33.1169, 0]}}]
          },
      {
          name: "Harvey, LA",
          Status: { code: 200, request: "geocode" },
          Placemark: [{ address: "2230 Peters Rd Harvey, LA 70058",
              Point: { coordinates: [-90.0772944, 29.9035387, 0]}}]
          },
      {
          name: "Acworth, GA",
          Status: { code: 200, request: "geocode" },
          Placemark: [{ address: "4260 Industrial Center LN Suite 300 Acworth, GA 30101",
              Point: { coordinates: [-84.6475369, 34.0583039, 0]}}]
          },
      {
          name: "Vidalia, LA",
          Status: { code: 200, request: "geocode" },
          Placemark: [{ address: "5110 Highway 84 West Vidalia, LA 71373",
              Point: { coordinates: [-91.4594553, 31.5742442, 0]}}]
          },
      {
          name: "Pompano, FL",
          Status: { code: 200, request: "geocode" },
          Placemark: [{ address: "3100 Gateway Drive Pompano Beach, FL 33069",
              Point: { coordinates: [-80.166318, 26.212585, 0]}}]
          },
      {
          name: "Morgan City, LA",
          Status: { code: 200, request: "geocode" },
          Placemark: [{ address: "9580 Highway 90 East Morgan City, LA 70380",
              Point: { coordinates: [-91.1342841, 29.6724488, 0]}}]
          },
      {
          name: "Heidelberg, MS",
          Status: { code: 200, request: "geocode" },
          Placemark: [{ address: "Highway 528 West Heidelberg, MS 39439",
              Point: { coordinates: [-88.9839419, 31.8868224, 0]}}]
          }
    ];


var map;
var geocoder;

// CapitalCitiesCache is a custom cache that extends the standard GeocodeCache.
// We call apply(this) to invoke the parent's class constructor.
function CapitalCitiesCache() {
    GGeocodeCache.apply(this);
}

// Assigns an instance of the parent class as a prototype of the
// child class, to make sure that all methods defined on the parent
// class can be directly invoked on the child class.
CapitalCitiesCache.prototype = new GGeocodeCache();

// Override the reset method to populate the empty cache with
// information from our array of geocode responses for capitals.
CapitalCitiesCache.prototype.reset = function() {
    GGeocodeCache.prototype.reset.call(this);
    for (var i in city) {
        this.put(city[i].name, city[i]);
    }
}

function initialize() {
    map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(29.899726, -90.046332), 5);
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());

    geocoder = new GClientGeocoder();
    geocoder.setCache(new CapitalCitiesCache());
}

function addAddressToMap(response) {
    map.clearOverlays();

    if (response && response.Status.code != 200) {
        alert("Unable to locate " + decodeURIComponent(response.name));
    } else {
        var place = response.Placemark[0];
        var point = new GLatLng(place.Point.coordinates[1],
                                place.Point.coordinates[0]);
        map.setCenter(point, 6);
        map.openInfoWindowHtml(point, "<b>Address:</b> " + place.address);
    }
}

function findCity(which) {
    if (which != 0) {
        geocoder.getLocations(city[which - 1].name, addAddressToMap);
    }
}