var valCenter = new GLatLng(39.473173,-0.376797);
var map;

function load(allowclick, preload) {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    map.setCenter(valCenter, 13);

    map.removeMapType(G_HYBRID_MAP);
    var mapControl = new GMapTypeControl();
    map.addControl(mapControl);
    map.addControl(new GLargeMapControl());

    if (allowclick) {
      GEvent.addListener(map,"click", function(overlay,point) {     
        var latInput = document.getElementById('loc_lat');
        var longInput = document.getElementById('loc_lng');
        latInput.value = point.lat();
        longInput.value = point.lng();
        var marker = new GMarker(point, {draggable: true});
        map.addOverlay(marker);
        GEvent.addListener(marker, "dragend", function() {
          var newLatLng = marker.getLatLng();
          latInput.value =  newLatLng.lat();
          longInput.value =  newLatLng.lng();
        });
      });
    } 

    if (preload) {
      /* Find all inputs and page, and handle them correctly */
      var allinput = document.getElementsByTagName('input');
      for (var i=0;i<allinput.length;i++) {
        if (allinput[i].id.indexOf('loadspot_lat_') !== -1) {
          thisSpotId = allinput[i].id.substr(13);
          thisSpotLat = allinput[i].value;
          thisSpotLng = document.getElementById('loadspot_lng_' + thisSpotId).value;
          thisSpotDrag = document.getElementById('loadspot_drag_' + thisSpotId).value;
          thisSpotColor = document.getElementById('loadspot_color_' + thisSpotId).value;
          thisSpotIcon = 'http://www.holavalencia.net/img/gmap_icons/icon' + thisSpotColor + '.png';
          thisSpotInfoText = document.getElementById('loadspot_infotext_' + thisSpotId).value;
          thisSpotInfoOn = 0;

          if (thisSpotId == 'solo') { 
            /* If this is a solo-type event, we can reset the center */
            map.setMapType(G_SATELLITE_MAP);
            map.setCenter(new GLatLng(thisSpotLat, thisSpotLng), 17);
            thisSpotInfoOn = 1;
          }

          placeMarker(thisSpotLat, thisSpotLng, thisSpotIcon, thisSpotDrag, thisSpotInfoText, thisSpotInfoOn);
        }
      }
    }
  }
}

function placeMarker(lat, lng, icon, drag, infotext, infoon) {
  var point = new GLatLng(lat,lng);

  var blueIcon = new GIcon(G_DEFAULT_ICON);
  blueIcon.image = icon;

  if (drag == 1) {
    // Set up our GMarkerOptions object
    markerOptions = { icon:blueIcon, draggable: true };

    var marker = new GMarker(point, markerOptions);
    map.addOverlay(marker);
    GEvent.addListener(marker, "dragend", function() {
      var latInput = document.getElementById('loc_lat');
      var longInput = document.getElementById('loc_lng');

      var newLatLng = marker.getLatLng();
      latInput.value =  newLatLng.lat();
      longInput.value =  newLatLng.lng();
    });
  } else {
    // Set up our GMarkerOptions object
    markerOptions = { icon:blueIcon };

    var marker = new GMarker(point, markerOptions);
    map.addOverlay(marker);
  }

  if (infoon) {
    marker.openInfoWindowHtml(infotext);
  } else {
    GEvent.addListener(marker, "mouseover", function() {
      marker.openInfoWindowHtml(infotext);
    });
  }
}

function randomPoints() {
  for (var i = 0; i < 10; i++) {
    var point = randomLocation();
    map.addOverlay(new GMarker(point));
  }
}

function switchMapType(newType) {
  var currentType = map.getCurrentMapType();
  var currentTypeName = currentType.getName();
  if (newType == 'map') {
    map.setMapType(G_NORMAL_MAP);
  } else if (newType == 'sat') {
    map.setMapType(G_SATELLITE_MAP);
  } else {
    map.setMapType(G_HYBRID_MAP);
  }
}

function myZoom(zoomout) {
  var currentZoom = map.getZoom();

  if (zoomout) {
    if (zoomout > 0) {
      var newZoom = currentZoom - 1;
    }
  } else {
    if (zoomout < 20) {
      var newZoom = currentZoom + 1;
    }
  }
  map.setCenter(valCenter, newZoom);
}

function randomPan() {
  var point;
  point = randomLocation();
  map.panTo(point);
}

var geocoder = new GClientGeocoder();

function showAddress() {
  var address = document.getElementById('loc_addr').value;
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " not found");
      } else {
        /* Set the point in the correct field */
        var latField = document.getElementById('loc_lat');
        var longField = document.getElementById('loc_lng');
        latField.value = point.lat();
        longField.value = point.lng();
        map.setCenter(point, 17);
        var marker = new GMarker(point, {draggable: true});
        map.addOverlay(marker);
        GEvent.addListener(marker, "dragend", function() {
          var newLatLng = marker.getLatLng();
          latField.value =  newLatLng.lat();
          longField.value =  newLatLng.lng();
        });
        marker.openInfoWindowHtml(address);
      }
    }
  );
}

function confirmRemoval(spotid) {
  var answer = confirm("Remove Spot ID " + spotid + '?');
  if (!answer) {
    return;
  }

  window.location='?removespot='+spotid;
}

function admShowSubcats(catid) {
  var checkBox = document.getElementById('loc_cat_' + catid).checked; 
  var subcatDiv = document.getElementById('loc_subcatchoices_' + catid); 

  if (checkBox) {
    subcatDiv.style.display = 'block';
  } else {
    subcatDiv.style.display = 'none';
  }
}
