/* cyoung@gmail.com
   December 1st, 2008
   Written for poorbuthappy.com
*/


/* Global variables. */
var globalMapObj = null;
var theform = null;

var hostelsAdded = 0;


function changeMarker(map, inform, overlay, latlng, overlaylatlng) {
 if (overlay != null) {
  confirmAction = window.confirm('Do you want to remove the map data?');
  if (confirmAction == true) {
   map.clearOverlays();
   inform.geo_lat.value = '0';
   inform.geo_lon.value = '0';
   inform.map_zoom_level.value = '2';
  }
  return; // the "click" event is then triggered on the existing overlay.
 }

 map.clearOverlays();

 var mkr = new GMarker(latlng);
 map.addOverlay(mkr);

 inform.geo_lat.value = latlng.lat();
 inform.geo_lon.value = latlng.lng();
 inform.map_zoom_level.value = map.getZoom();

}


function addHostelToMap(lat, lon, infoWindowContent) {
 var newMarker = new GMarker(new GLatLng(lat, lon));
 globalMapObj.addOverlay(newMarker);
 if (!window.hidePopUps) {
  GEvent.addListener(newMarker, "click", function() {
   newMarker.openInfoWindowHtml(infoWindowContent);
  });
 }
 hostelsAdded++;
}


function body_initialize(geo_lat, geo_lon, map_zoom_level) {

/* The default is (0,0,2) for "un-geocoded" places. It's sufficiently unlikely that
   any place will be put here, so don't even bother initializing map. */

 if ((geo_lat == 0) && (geo_lon == 0) && (map_zoom_level == 2)) {
  document.getElementById("map_canvas").style.display = "none"; // Hide the map canvas.
  return;
 }

 if (mapMode == 'edit') {
  theform = document.getElementById("theform");
  geo_lat = theform.geo_lat.value;
  geo_lon = theform.geo_lon.value;
  map_zoom_level = parseInt(theform.map_zoom_level.value);
 }


 if (GBrowserIsCompatible()) {
  var map = new GMap2(document.getElementById("map_canvas"));
  globalMapObj = map;

  map.setCenter(new GLatLng(geo_lat, geo_lon), map_zoom_level);
  var markerObj = new GMarker(new GLatLng(geo_lat, geo_lon));
  map.addOverlay(markerObj);


  map.addControl(new GSmallMapControl());

  if (mapMode == 'edit') {
   GEvent.addListener(map, "click", function(overlay,  latlng,  overlaylatlng) {
    changeMarker(map, theform, overlay, latlng, overlaylatlng);
   });
  }

 }

 if (window.hostelMode) {
  hostelInit();
  if ((hostelsAdded == 0) && (window.removeMapWithoutHostels)) {
/* A false start. Added a hostel map but didn't actually add any hostels.
   Remove the map and make the canvas invisible.
*/
   GUnload();
   document.getElementById("map_canvas").style.display = "none"; // Hide the map canvas.

  }
 }
 if (window.hostel_edit) {
  hostel_edit_init();
 }

}





/* For initializing when doing a hostel/place add (new) */

var globalMapObj_hostel;
var globalMapObj_place;

function add_body_initialize() {
 theform = document.getElementById("theform");
 geo_lat = theform.geo_lat.value;
 geo_lon = theform.geo_lon.value;
 map_zoom_level = parseInt(theform.map_zoom_level.value);

 theform2 = document.getElementById("theform2");
 geo_lat2 = theform2.geo_lat.value;
 geo_lon2 = theform2.geo_lon.value;
 map_zoom_level2 = parseInt(theform2.map_zoom_level.value);



 if (GBrowserIsCompatible()) {
  var map = new GMap2(document.getElementById("hostel_map_canvas"));
  globalMapObj_hostel = map;

  var map2 = new GMap2(document.getElementById("place_map_canvas"));
  globalMapObj_place = map2;

  map.setCenter(new GLatLng(geo_lat, geo_lon), map_zoom_level);

  map.setCenter(new GLatLng(geo_lat2, geo_lon2), map_zoom_level);

  map.addControl(new GLargeMapControl());
  map2.addControl(new GLargeMapControl());

  GEvent.addListener(map, "click", function(overlay,  latlng,  overlaylatlng) {
   changeMarker(map, theform, overlay, latlng, overlaylatlng);
  });

  GEvent.addListener(map2, "click", function(overlay,  latlng,  overlaylatlng) {
   changeMarker(map2, theform2, overlay, latlng, overlaylatlng);
  });

 }

 add_init();

}

