
var map;
var marker;
var markerVisible;
var geocoder;
var mymaplocation;

// ******* ********* ********* ********* ********* ********* ********* *********

function mapsLoaded() {
  map = new google.maps.Map2(document.getElementById("map_canvas"));
  map.addControl(new google.maps.LargeMapControl());
  map.addControl(new google.maps.MapTypeControl());

  point = new google.maps.LatLng(40.41674, -3.70325);
  map.setCenter(point, 4);
  marker = new google.maps.Marker(point, {draggable:true});
  markerVisible = false;

  google.maps.Event.addListener(marker, "dragend", function() {
    var point = marker.getPoint();
    centrarInformarPunto(point);
  });
  google.maps.Event.addListener(map, "click", function(overlay, point) {
    showMarker(point);
    centrarInformarPunto(point);
  });
  geocoder = new google.maps.ClientGeocoder();

  geocoder.getLocations(mymaplocation, addAddressToMap);
}

// ******* ********* ********* ********* ********* ********* ********* *********

function showMarker(point) {
  marker.setPoint(point);
  if (!markerVisible) {
    map.addOverlay(marker);
    markerVisible = true;
  }
}

// ******* ********* ********* ********* ********* ********* ********* *********

function loadMaps() {
  google.load("maps", "2", {"callback" : mapsLoaded});
}

// ******* ********* ********* ********* ********* ********* ********* *********

function addAddressToMap(response) {
  if (!response || response.Status.code != 200) {
   // clearInfo();
    map.clearOverlays();
    markerVisible = false;
 //   alert("Sorry, we were unable to geocode that address");
  }
  else {
    place = response.Placemark[0];
    accuracy = place.AddressDetails.Accuracy;
    point = new google.maps.LatLng(place.Point.coordinates[1],
                        place.Point.coordinates[0]);
    showMarker(point);
   // info = formatPlace(place);
    newZoom = 8 + accuracy;
    map.setCenter(point, newZoom);
  }
}

// ******* ********* ********* ********* ********* ********* ********* *********

function centrarInformarPunto(point) {
  map.panTo(point);
  clearInfo();
  setElementValue("geo_lat", point.lat().toFixed(5));
  setElementValue("geo_lng", point.lng().toFixed(5));
}

// ******* ********* ********* ********* ********* ********* ********* *********

function formatPlace(place) {
  lat = place.Point.coordinates[1];
  lng = place.Point.coordinates[0];
  address = place.address;
  // Asignaciones por defecto de campos opcionales
  countryNameCode = '';
  administrativeAreaName = '';
  subAdministrativeAreaName = '';
  localityName = '';
  dependentLocalityName = '';
  thoroughfareName = '';
  postalCodeNumber = '';
  accuracy = '';

  if (addressDetailsObj = place.AddressDetails) {
    accuracy = addressDetailsObj.Accuracy;
    if (countryObj = place.AddressDetails.Country) {
      countryNameCode = countryObj.CountryNameCode;
      if (admAreaObj = countryObj.AdministrativeArea) {
        administrativeAreaName = admAreaObj.AdministrativeAreaName;
        if (subAdmAreaObj = admAreaObj.SubAdministrativeArea) {
          subAdministrativeAreaName = subAdmAreaObj.SubAdministrativeAreaName;
          // Locality en ocasiones depende de area administrativa de primer nivel
          admAreaObj = subAdmAreaObj;
        }
        if (localityObj = admAreaObj.Locality) {
        // Cabeza de partido judicial
        localityName = localityObj.LocalityName;
          if (depLocalityObj = localityObj.DependentLocality) {
            dependentLocalityName = depLocalityObj.DependentLocalityName;
            localityObj = depLocalityObj;
          }
          if (thoroughfareObj = localityObj.Thoroughfare) {
            thoroughfareName = thoroughfareObj.ThoroughfareName;
          }
          if (postalCodeObj = localityObj.PostalCode) {
            postalCodeNumber = postalCodeObj.PostalCodeNumber;
          }
        }
      }
    }
  }
/*
  setElementValue('pais', countryNameCode);
  setElementValue('comunidad', administrativeAreaName);
  setElementValue('provincia', subAdministrativeAreaName);
  setElementValue('localidad', localityName);
  setElementValue('dep_localidad', dependentLocalityName);
  setElementValue('direccion', thoroughfareName);
  setElementValue('cp', postalCodeNumber);
  setElementValue('geo_lat', lat);
  setElementValue('geo_lng', lng);
  setElementValue('precision', accuracy);
*/
}

// ******* ********* ********* ********* ********* ********* ********* *********

function showLocation(address) {
  geocoder.getLocations(address, addAddressToMap);
}

// ******* ********* ********* ********* ********* ********* ********* *********

function setElementValue(id, value) {
  // document.getElementById(id).value = value;
  document.getElementById(id).innerHTML = value;
}

// ******* ********* ********* ********* ********* ********* ********* *********

function clearInfo() {
  setElementValue('pais', '');
  setElementValue('comunidad', '');
  setElementValue('provincia', '');
  setElementValue('localidad', '');
  setElementValue('dep_localidad', '');
  setElementValue('direccion', '');
  setElementValue('cp', '');
  setElementValue('geo_lat', '');
  setElementValue('geo_lng', '');
  setElementValue('precision', '');
}

// ******* ********* ********* ********* ********* ********* ********* *********
