var map;
var geocoder;
var lastResponse;
var marker;   // the address marker; valid only if bAddress_OK is true!!!

var bAddress_OK = false;

var bMapZoom = true;

function init(){
   if (GBrowserIsCompatible()) {
      map = new GMap2(document.getElementById("map"));
      map.addControl(new GSmallMapControl());
      //map.addControl(new GMapTypeControl());
      map.addControl  (new GScaleControl());
      map.setCenter(new GLatLng(34.0,-118.0), 9);
      geocoder = new GClientGeocoder();
      GEvent.addListener(map, "dragend", do_onMapMove);

      document.getElementById("country").selectedIndex = 211;     // USA?
      document.getElementById("country").onchange();
   }
   else
   {
      alert("Your browser is not compatible and is unable to show Google Maps!");
   }
}


function do_onMapMove(){
  if (document.getElementById("radius_map").checked){
    //alert("You moved the map!");
    bMapZoom = false;     // during this transaction don't change map zoom
    hnd_radius_type();    // everything seems to be implemented there for that case
    bMapZoom = true;
  }
}

// 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) {
  lastResponse=response;
  bAddress_OK = false;

  if (!response || response.Status.code != 200) {
    //alert("Sorry, we were unable to geocode that address");
    if (document.getElementById("radius_address").checked){
      document.getElementById("radius").disabled=true;
      document.getElementById("radius").selectedIndex=0;
    }
    return;
  } else {
    map.clearOverlays();
    bAddress_OK = true;
    document.getElementById("radius").disabled=false;
    var place = response.Placemark[0];
    aPoint = new GLatLng(place.Point.coordinates[1],
                        place.Point.coordinates[0]);
    document.getElementById("tlat").value = aPoint.y;
    document.getElementById("tlon").value = aPoint.x;
    document.getElementById("ty1").value = aPoint.distanceFrom(new GLatLng(aPoint.y+1,aPoint.x));
    document.getElementById("tx1").value = aPoint.distanceFrom(new GLatLng(aPoint.y,aPoint.x+1));

    baseIcon = new GIcon();
    baseIcon.image = "images/center.gif";
    baseIcon.iconSize = new GSize(15, 15);
    baseIcon.iconAnchor = new GPoint(7, 7);
    baseIcon.infoWindowAnchor = new GPoint(7, 7);

    var mapZoom = 6;
    switch (place.AddressDetails.Accuracy){
       case 0: mapZoom =  0; break  // Unknown location. (Since 2.59)
       case 1: mapZoom =  4; break  // Country level accuracy. (Since 2.59)
       case 2: mapZoom =  6; break  // Region (state, province, prefecture, etc.) level accuracy. (Since 2.59)
       case 3: mapZoom =  7; break  // Sub-region (county, municipality, etc.) level accuracy. (Since 2.59)
       case 4: mapZoom =  9; break  // Town (city, village) level accuracy. (Since 2.59)
       case 5: mapZoom = 12; break  // Post code (zip code) level accuracy. (Since 2.59)
       case 6: mapZoom = 13; break  // Street level accuracy. (Since 2.59)
       case 7: mapZoom = 14; break  // Intersection level accuracy. (Since 2.59)
       case 8: mapZoom = 14; break  // Address level accuracy. (Since 2.59)
    }
    //lastResponse.Placemark[0].AddressDetails.Accuracy
    /*
    switch (document.getElementById("RADIUS").selectedIndex){
       case 1: mapZoom = 13; break;  // no radius
       case 2: mapZoom = 16; break;  // 0.25 mi
       case 3: mapZoom = 15; break;  // 0.50 mi
       case 4: mapZoom = 14; break;  // 0.75 mi
       case 5: mapZoom = 14; break;  // 1.00 mi
       case 6: mapZoom = 14; break;  // 1.50 mi
       case 7: mapZoom = 13; break;  // 2.00 mi
    }
    */
    map.setZoom(mapZoom);
    map.setCenter(aPoint);

    marker = new GMarker(aPoint, baseIcon);
    map.addOverlay(marker);
    marker.openInfoWindowHtml(place.address + '<br>' +
      '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);

    document.getElementById("mStatus").innerHTML = "resolved as: "+ place.address;

    hnd_radius_change();

  }
}


function getLatLonFromAddress(){
  // designed for add_product_form.php
  // IDs: tlat tlon
  // country state other_state
  // location zip address
  // chkMappable

  var address = "";

  var t_country     = document.getElementById("country").options[document.getElementById("country").selectedIndex].text;
  var t_state_id    = document.getElementById("state").selectedIndex;
  var t_state       = document.getElementById("state").options[document.getElementById("state").selectedIndex].text;
  var t_other_state = document.getElementById("other_state").value;
  var t_zip         = document.getElementById("zip").value;
  var t_location    = document.getElementById("location").value;
  var t_address     = document.getElementById("address").value;

  if (t_country=="USA"){
    address = t_country;
    if (t_zip!=""){
      address = t_zip + ", " + address;
    }
    if (t_state_id){
      address = t_state + ", " + address;
    }
    if (t_location!=""){
      address = t_location + ", " + address;
    }
    if (t_address!=""){
      address = t_address + ", " + address;
    }
  }
  else{
    address = t_country;
    if (t_zip!=""){
      address = t_zip + ", " + address;
    }
    if (t_other_state!=""){
      address = t_other_state + ", " + address;
    }
    if (t_location!=""){
      address = t_location + ", " + address;
    }
    if (t_address!=""){
      address = t_address + ", " + address;
    }
  }

  geocoder.getLocations(address, addAddressToMap);
}


function hnd_radius_type(){
  map.clearOverlays();
  if (document.getElementById("radius_address").checked){
    getLatLonFromAddress();   // try to resolve the address ... and if so - recenter map and get X/Y
  }
  else{ // selected is radius_by_map_center
    aPoint = map.getCenter();
    document.getElementById("tlat").value = aPoint.y;
    document.getElementById("tlon").value = aPoint.x;
    document.getElementById("ty1").value = aPoint.distanceFrom(new GLatLng(aPoint.y+1,aPoint.x));
    document.getElementById("tx1").value = aPoint.distanceFrom(new GLatLng(aPoint.y,aPoint.x+1));

    document.getElementById("radius").disabled=false;
    hnd_radius_change();
  }
}

function hnd_radius_change(){
  map.clearOverlays();
  if ((document.getElementById("radius_address").checked)&&(bAddress_OK)) map.addOverlay(marker);

  if (document.getElementById("radius").selectedIndex==0) return; // no radius is selected, thus no circle on the map

  var points;
  var rr = 0;
  var xx = 0;
  var yy = 0;
  var mapZoom = 9;

  rr = parseFloat(document.getElementById("radius").value);
  if (rr==0) return;    // no radius is selected, thus no circle on the map

  switch (document.getElementById("radius").selectedIndex){
     case 1: mapZoom = 12; break;  //   1 mi
     case 2: mapZoom = 10; break;  //   5 mi
     case 3: mapZoom =  9; break;  //  10 mi
     case 4: mapZoom =  8; break;  //  30 mi
     case 5: mapZoom =  7; break;  //  50 mi
     case 6: mapZoom =  6; break;  // 100 mi
  }
  if (bMapZoom) map.setZoom(mapZoom);

  xx = parseFloat(document.getElementById("tlon").value);
  yy = parseFloat(document.getElementById("tlat").value);
  points = mapGetCirclePoints(xx,yy,rr)

  map.addOverlay(new GPolyline(points,"#FF0000", 5)); // red, 10px thick
}

function do_map_calc(){   // etc map calculations based on good x/y coordinates
}