// JavaScript Document
//<![CDATA[
function load() {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map"));
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.addControl(new GLargeMapControl());
    setMapType(hybrid);
    // map.setCenter(new GLatLng(-40.900557, 174.885971), 4);
    map.setCenter(new GLatLng(-41.279998, 174.779998), 5);
    geocoder = new GClientGeocoder();
  }
  // showLocation();
}

window.onload = function start() {
  geocoder = new GClientGeocoder();
  var add_id = document.getElementById("address")
  address = add_id.value;
  
  var long_id = document.getElementById("long")
  var lat_id = document.getElementById("lat")
  
  
  if (long_id.value != "" && lat_id.value != ""){
    loadV1();
  }else{
    if (address =="" )address='wellington, new zealand';
    //var address = document.forms[0].q.value;
    geocoder.getLocations(address, loadV2);
  }
}

function loadV1() {
  var n_id = document.getElementById("notes")
  var long_id = document.getElementById("long")
  var lat_id = document.getElementById("lat")
  
  point = new GLatLng(lat_id.value,
    long_id.value);
  
  var map = new GMap2(document.getElementById("map"));
  map.addControl(new GLargeMapControl());
  map.addControl(new GMapTypeControl());
  map.addControl(new GOverviewMapControl());
  map.addControl(new GScaleControl());
  //		map.addControl(new enableSidebar());
  map.setCenter(new GLatLng(lat_id.value,long_id.value), 10);
  function createMarker(marker) {
    GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml("<strong>Location notes:</strong><br />" + n_id.value + "</b>");
    });
    return marker;
  }
  marker = new GMarker(point);
  if (n_id.value != ''){
    map.addOverlay(createMarker(marker));
  }else{
    map.addOverlay(marker);
  } // if (n_id != ''){
  map.setMapType(G_HYBRID_TYPE);

}

function loadV2(response) {
  var n_id = document.getElementById("notes")
  
  if (!response || response.Status.code != 200) {
    alert("Sorry, we were unable to geocode that address");
  } else {
    place = response.Placemark[0];
    point = new GLatLng(place.Point.coordinates[1],
    place.Point.coordinates[0]);
    
    var map = new GMap2(document.getElementById("map"));
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.addControl(new GOverviewMapControl());
    map.addControl(new GScaleControl());
    //		map.addControl(new enableSidebar());
    map.setCenter(new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]), 10);
    function createMarker(marker) {
      GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml("<strong>Location notes:</strong><br />" + n_id.value + "</b>");
      });
      return marker;
    }
    marker = new GMarker(point);
    if (n_id.value != ''){
      map.addOverlay(createMarker(marker));
    }else{
      map.addOverlay(marker);
    } // if (n_id != ''){
  }
  map.setMapType(G_HYBRID_TYPE);

}

// addAddressToMap() is called when the geocoder returns an
// answer.  It adds a marker to the map with an open info window
// showing the nicely formatted version of the address and the country code.
function addAddressToMap(response) {
  //  var map = new GMap2(document.getElementById("map"));
  // map.clearOverlays();
  if (!response || response.Status.code != 200) {
    alert("Sorry, we were unable to geocode that address");
  } else {
    place = response.Placemark[0];
    
    point = new GLatLng(place.Point.coordinates[1],
    place.Point.coordinates[0]);
    alert(point);
    marker = new GMarker(point);
    map.addOverlay(marker);
    marker.openInfoWindowHtml(place.address + '<br>' +
      '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode + ' long lat'+point);
  }
}

// showLocation() is called when you click on the Search button
// in the form.  It geocodes the address entered into the form
// and adds a marker to the map at that location.
function showLocation() {
  var add_id = document.getElementById("address")
  var n_id = document.getElementById("notes")
  address = add_id.value;
  //var address = document.forms[0].q.value;
  geocoder.getLocations(address, bollox);
}

// findLocation() is used to enter the sample addresses into the form.
function findLocation(address, notes) {
  var add_id = document.getElementById("address")
  var n_id = document.getElementById("notes")
  add_id.value = address ;
  n_id.value = notes ;
  geocoder.getLocations(add_id.value, loadV2);
}

//]]>
