//globals
var minx;
var miny;
var maxx;
var maxy;

function checkBounds() {

  //GLog.write("in checkbounds");
	var bounds = googleMap.getBounds();
	var zoom = googleMap.getBoundsZoomLevel(bounds);
	southWest = bounds.getSouthWest();
	northEast = bounds.getNorthEast();
	lngSpan = northEast.lng() - southWest.lng();
	latSpan = northEast.lat() - southWest.lat();

	minx = southWest.lng();
	maxx = southWest.lng() + lngSpan;
	maxy = northEast.lat();
	miny = northEast.lat() - latSpan;

}

function checkPan(MinLat,MinLon,MaxLat,MaxLon) {

  //Set the bottom-left and the top-right of the allowed region
  var allowedBounds = new GLatLngBounds(new GLatLng(parseFloat(MinLat),parseFloat(MinLon)), new GLatLng(parseFloat(MaxLat),parseFloat(MaxLon)));
     // Perform the check and return if map is within the allowed bounds
    if (allowedBounds.contains(googleMap.getCenter())) {
      return;
    }
       
        // It`s not OK, so find the nearest allowed point and move there
        var C = googleMap.getCenter();
        var X = C.lng();
        var Y = C.lat();

        var allowedMaxX = allowedBounds.getNorthEast().lng();
        var allowedMaxY = allowedBounds.getNorthEast().lat();
        var allowedMinX = allowedBounds.getSouthWest().lng();
        var allowedMinY = allowedBounds.getSouthWest().lat();

        // If the map position is out of range, move it back
        if (X < allowedMinX) {X = allowedMinX;}
        if (X > allowedMaxX) {X = allowedMaxX;}
        if (Y < allowedMinY) {Y = allowedMinY;}
        if (Y > allowedMaxY) {Y = allowedMaxY;}        
        googleMap.setCenter(new GLatLng(Y,X));
}