 function finishMapGM(pts,p) {
 	var myOptions = {
             zoom: 3,
             mapTypeId: google.maps.MapTypeId.ROADMAP
           };
       map = new google.maps.Map(document.getElementById("mapinterface"),myOptions);
       overlay = new MyOverlay(map);
       theBounds = new google.maps.LatLngBounds();
       for (i=0;i<pts.length;i++) {
       	 pt = new google.maps.LatLng(pts[i].lat(),pts[i].lng());
       	theBounds.extend(pt);
       }
       map.fitBounds(theBounds);
       map.setZoom(3);
 }


function mapMoveGM() {
	// nb: need to trigger the load event on 'idle' otherwise if 'bounds_changed' is used, the bounds change every time a new point is added to the map
	// to this triggers repeat calls for the same data!
	google.maps.event.addListener(map,'idle',function() {
		// only add icons if we are not in edit mode ..
		if (currentTab != "edit") {
			//markersArray = [];
			map.clearMarkers();
			updateMap(true);
		}
		if ((infodisplayed === true) && (isSearchMove === false)) {
			doCloseInfoWindow();
		}

	}); 

}



MyOverlay.prototype = new google.maps.OverlayView();
MyOverlay.prototype.onAdd = function() { }
MyOverlay.prototype.onRemove = function() { }
MyOverlay.prototype.draw = function() { }
function MyOverlay(map) { this.setMap(map); }



google.maps.Polygon.prototype.getBounds = function(latLng) {
	var bounds = new google.maps.LatLngBounds();
	var paths = this.getPaths();
	var path;
	for (var p = 0; p < paths.getLength(); p++) {
	  path = paths.getAt(p);
	  for (var i = 0; i < path.getLength(); i++) {
	      bounds.extend(path.getAt(i));
	  }
	}
	return bounds;
};


function getZoomLevelForPolygon(p) {
  var zoomlevel = map.getBoundsZoomLevel(p.getBounds());
  return zoomlevel;
  

}

function getZoomLevelForPoints(pts) {

  var boundary_distance = pts[0].distanceFrom(pts[2]);
  var zoomLevel;
  
  if(boundary_distance > 24000) {
    zoomLevel = 11;
  }else if(boundary_distance > 12000) {
    zoomLevel = 12;
  }else if(boundary_distance > 6000) {
    zoomLevel = 13;
  }else if(boundary_distance > 2000) {
    zoomLevel = 14;
  }else if(boundary_distance > 1500) {
    zoomLevel = 15;
  }else {
    zoomLevel = 16;
  }
  return zoomLevel;
}

function getCenterPointFromPolyline(polyline) {
	return polyline.getBounds().getCenter();	
}

function getCenterPointFromPolygon(polygon) {
	return polygon.getBounds().getCenter();	
}


function getMapCentreLatGM() {
	lat = map.getCenter().lat();
	return lat;

}

function getMapCentreLngGM() {
	lng = map.getCenter().lng();
	return lng;
}

function getXPixGM(lat,lng) {
	  var mapLatLng = new google.maps.LatLng(lat,lng);
	  var theGPoint = overlay.getProjection().fromLatLngToContainerPixel(mapLatLng);
	  
	  var xPix = theGPoint.x;
	  return xPix;
}

function getYPixGM(lat,lng) {
	  var mapLatLng = new google.maps.LatLng(lat,lng);
	  var theGPoint = overlay.getProjection().fromLatLngToContainerPixel(mapLatLng);
	  var yPix = theGPoint.y;
	  return yPix;
}


function removeAdminOverlaysGM() {

	// just in case the user is in moderation mode, remove the temporary moderation point line poly from the map
	try {
		// just in case there is an existing polyline + info box open
		map.removeOverlay(theAdminPoint);
	}
	catch (e) {
	}
	try {
		// just in case there is an existing polyline + info box open
		map.removeOverlay(theAdminLine);
	}
	catch (e) {
	}
	try {
		// just in case there is an existing polyline + info box open
		map.removeOverlay(theAdminPolygon);
	}
	catch (e) {
	}

}


// from: http://www.andrewsellick.com/16/google-maps-api-uk-postcode-geocoding
function doPostcodeSearchGM(thePostcode) {
  ukPostcodeTest(thePostcode);

}
function gmap_map_postcode(lon, lat){
  map.setCenter(new google.maps.LatLng(lon,lat),17);
} 


function ukPostcodeTest(thePostcode) {
	// test to see if this is a postcode or an address, and perform search accordingly
	// take a copy and convert to upper case
	var s = thePostcode.toUpperCase();
	// Replace punctuation and whitepsace by a single space
	s = s.replace(/\W+/g, " ");
	// Remove and trailing leading spaces
	s = s.replace(/^ /, "");
	s = s.replace(/ $/, "");
	// Perform the check
	var match = s.match(/^[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][ABD-HJLNP-UW-Z]{2}$/);
	if (!match) {
	  // Its not a UK Postcode, so perform a standard GClientGeocoder call on the original search string
	  //showAddress(thePostcode);
	  showPostcode(thePostcode);
	} else {
	  // It is a UK Postcode, so call GDirections on the reformatted search string 
	  showPostcode(s);
	}
}

      
// ====== Code for handling search strings that are not UK Postcodes =======
// ====== Use the GClientGeocoder in the normal way ======


// from: http://econym.org.uk/gmap/example_ukpostcode.htm
      
// ====== Geocoding ======
function showAddress(search) {
	// ====== Perform the Geocoding ======    
	
	// default to the UK
	//search = search + " UK";
	
	
	    geo.geocode( { 'address': search}, function(results, status) {
	      if (status == google.maps.GeocoderStatus.OK) {
	        map.setCenter(results[0].geometry.location);
	      } else {
	        alert("Geocode was not successful for the following reason: " + status);
	      }
	    });
	  }

	
	
// ====== Using GDirections to process a UK postcode ======
function showPostcode(search) {
	// Call GDirections
  directionsDisplay = new google.maps.DirectionsRenderer();
  directionsDisplay.setMap(map);


  var start = search;
  var end = search;
  var request = {
    origin:start, 
    destination:end,
    travelMode: google.maps.DirectionsTravelMode.DRIVING
  };
  directionsService.route(request, function(result, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(result);
    } else {
	    alert("Geocode was not successful for the following reason: " + status);
	}
  });
}	



function resizeMapDivGM() {
		var centerLatLong = map.getCenter();
		var startZoom = map.getZoom();
	  	var el = document.getElementById("map_canvas");
	  	var currWidth = parseInt(getStyle(el,"width"));
		var newWidth = parseInt(document.body.clientWidth) - 300;
		setStyle("map-canvas", "width", newWidth + "px");
    	//document.getElementByID("map").style.width = (document.body.clientWidth - 300) + "px";

		// now recentre the map
		map.checkResize();
		map.setCenter(centerLatLong, startZoom);
}

