function silentErrorHandler(){
	return true;
}
//window.onerror=silentErrorHandler;


var marker  = null;
var baseIcon = new GIcon();
var point = 0;
var i=0;
var newMarker=false;
baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
baseIcon.iconSize = new GSize(20, 34);
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(9, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);
baseIcon.infoShadowAnchor = new GPoint(18, 25);

// A function to create the marker and set up the event window
function createMarker(point,info,edit,markerId){
  var mIcon = new GIcon(baseIcon);
  if(!markerId){
    i=markers.length;
  }
  else{
    i=markerId;
  }
//  alert(i);
  mIcon.image = "http://www.carfreewalks.org/graphics/markers/"+(i+1)+".png";
  var marker = new GMarker(point,
                  {icon: mIcon,
                  draggable: true});
  marker.id=i;

  GEvent.addListener(marker, "click", function() {
    markerInfo(marker.id);
  });

   if(edit){
    GEvent.addListener(marker, "drag", function() {
      document.map.closeInfoWindow();
  });

    GEvent.addListener(marker, "dragend", function() {
      document.getElementById('lat').value = marker.getPoint().y;
      document.getElementById('lng').value = marker.getPoint().x;
      update_waypoint(marker.id);
    });
  }

  points[i]   = point;
  markers[i]  = marker;
  htmls[i]    = info;
  newMarker   = false;

 // i++;
  return marker;
}

function markerInfo(i) {
  document.map.openInfoWindowHtml(points[i], htmls[i]);
//  markers[i].openInfoWindowHtml(htmls[i]);
}

function drawPolyline(point){
  document.map.addOverlay(new GPolyline(point));
}

function moveMarker(id,lat,lng){
  markers[id].setPoint(new GLatLng(lat,lng));
  markers[id].redraw(true);
}

function onCloseInfoWindow(id,obj){
  if(marker){
    document.map.removeOverlay(markers[id]);
    markers.pop();
	markers.length=markers.length-1;
    xajax_remove_waypoint(waypointId);
  }
  else{
    if(obj){
      document.map.removeOverlay(obj.id);
      obj.pop();
      obj.length=obj.length-1;
      xajax_remove_waypoint(obj.id);
    }
  }
}

// Create the Google Map to be used.
function createMap(edit,mapType) {
  if (!GBrowserIsCompatible()) {
    alert('Your browser is not compatible with the Google Maps API');
    return;
  }

document.map = new GMap2(document.getElementById("map"));
//var mgr = new GMarkerManager(document.map);
document.map.setCenter(new GLatLng(start_lat,start_long), 6);

  if(mapType=="print"){
	document.map.addMapType(G_PHYSICAL_MAP);
	document.map.removeMapType(G_HYBRID_MAP);
	//document.map.removeMapType(G_SATELLITE_MAP);
    document.map.setMapType(G_PHYSICAL_MAP);
  }
  else{
    //document.map.setMapType(G_HYBRID_MAP);
	document.map.addMapType(G_PHYSICAL_MAP);
	document.map.removeMapType(G_HYBRID_MAP);
	//document.map.removeMapType(G_SATELLITE_MAP);
	document.map.setMapType(G_PHYSICAL_MAP);
  }
	document.map.addControl(new GMapTypeControl());
	document.map.addControl(new GLargeMapControl());
	document.map.enableScrollWheelZoom();

  if(edit){
    GEvent.addListener(document.map, "click", function(overlay, point) {
      infoWindow=document.map.getInfoWindow();
      if(true==infoWindow.isHidden()){
        if (marker == null) {
          //i = parseInt(document.getElementById("waypoint").value);
          marker    = createMarker(point, " ",true);
          marker.edit=true;
          newMarker = true;
          document.getElementById('lat').value = marker.getPoint().y;
          document.getElementById('lng').value = marker.getPoint().x;
          document.map.addOverlay(marker);
          fetch_waypoint(marker.id);
          marker.openInfoWindowHtml("<img src='graphics/loading.gif' align='left' height='32' width='32' class='clean'><p>Just quickly looking up where we are...</p>");
        }
        else{
          markers[i].setPoint(point);
          //drawPolyline(point);
          document.getElementById('lat').value = markers[i].getPoint().y;
          document.getElementById('lng').value = markers[i].getPoint().x;
          //alert("i: "+i+"\n\rmarker: "+marker+"\n\r"+markers.length+"\n\r"+markers[i-1]);
          markers[i].openInfoWindowHtml("<img src='graphics/loading.gif' align='left' height='32' width='32' class='clean'><p>Just quickly looking up where we are...</p>");
          fetch_waypoint(marker.id);
        }
      }
    });
  }
//  ======== Add a map overview ==========
      //  ======== save a reference to the control =========
      var ovcontrol = new GOverviewMapControl(new GSize(100,100));
      document.map.addControl(ovcontrol);

      //  ======== A function to adjust the positioning of the overview ========
      function positionOverview(x,y) {
        var omap=document.getElementById("map_overview");
        omap.style.left = x+"px";
        omap.style.top = y+"px";

        // == restyling ==
        omap.firstChild.style.border = "1px solid gray";

        omap.firstChild.firstChild.style.left="4px";
        omap.firstChild.firstChild.style.top="4px";
        omap.firstChild.firstChild.style.width="160px";
        omap.firstChild.firstChild.style.height="160px";
      }

      // ======== get a reference to the GMap2 ===========
      var ovmap = ovcontrol.getOverviewMap();
      //  ======== Cause the overview to be positioned AFTER IE sets its initial position ========
      //setTimeout("positionOverview(558,254)",1);
      //  ======== change the overview map type AFTER the overview finishes initializing =====
      //setTimeout("ovmap.setMapType(G_SATELLITE_MAP);",1);

}
