Index: main/trunk/greenstone3/web/interfaces/default/js/classifier_scripts.js
===================================================================
--- main/trunk/greenstone3/web/interfaces/default/js/classifier_scripts.js	(revision 33167)
+++ main/trunk/greenstone3/web/interfaces/default/js/classifier_scripts.js	(revision 33168)
@@ -46,5 +46,6 @@
 		}
 		updateOpenClassifiers();
-		if(typeof mapEnabled !== 'undefined'){
+		if(typeof mapEnabled !== 'undefined' && mapEnabled){
+			//console.log("Classifier scripts -> updateMap()");
 			updateMap();
 		}
@@ -154,5 +155,5 @@
 			}
 			updateOpenClassifiers();			
-			if(typeof mapEnabled !== 'undefined'){
+			if(typeof mapEnabled !== 'undefined' && mapEnabled){
 				getSubClassifier(sectionID);
 			}
Index: main/trunk/greenstone3/web/interfaces/default/js/map-scripts-shapes-util.js
===================================================================
--- main/trunk/greenstone3/web/interfaces/default/js/map-scripts-shapes-util.js	(revision 33167)
+++ main/trunk/greenstone3/web/interfaces/default/js/map-scripts-shapes-util.js	(revision 33168)
@@ -1,2 +1,20 @@
+
+// https://stackoverflow.com/questions/3284808/getting-the-bounds-of-a-polyline-in-google-maps-api-v3
+
+google.maps.Polyline.prototype.getBounds = function() {
+    var bounds = new google.maps.LatLngBounds();
+    this.getPath().forEach(function(item, index) {
+        bounds.extend(new google.maps.LatLng(item.lat(), item.lng()));
+    });
+    return bounds;
+};
+
+google.maps.Polygon.prototype.getBounds = function() {
+    var bounds = new google.maps.LatLngBounds();
+    this.getPath().forEach(function(item, index) {
+        bounds.extend(new google.maps.LatLng(item.lat(), item.lng()));
+    });
+    return bounds;
+};
 
 
@@ -516,3 +534,31 @@
 }
 
-
+ShapesUtil.overlayBounds = function (overlays) {
+	
+	
+	var extended_bounds = new google.maps.LatLngBounds();
+	
+	for (var i=0; i<overlays.length; i++) {
+		
+		var overlay_item = overlays[i];
+		
+		if (ShapesUtil.overlayItemIsShape(overlay_item)) {
+			var shape = overlay_item;
+
+			var bounds = shape.getBounds();
+			var ne = bounds.getNorthEast();
+			var sw = bounds.getSouthWest();
+			extended_bounds.extend(ne);
+			extended_bounds.extend(sw);					
+		}
+		else {
+			var marker = overlay_item;
+			var latlng = marker.getPosition();
+			extended_bounds.extend(latlng);
+		}
+	}
+	
+	return extended_bounds;
+	
+}
+
Index: main/trunk/greenstone3/web/interfaces/default/js/map-scripts.js
===================================================================
--- main/trunk/greenstone3/web/interfaces/default/js/map-scripts.js	(revision 33167)
+++ main/trunk/greenstone3/web/interfaces/default/js/map-scripts.js	(revision 33168)
@@ -16,10 +16,10 @@
 var _searchRunning = false;
 var _nearbyDocs = new Array();
-var _scrollThroughDefault = true;
+var _scrollThroughDefault = true; // TODO: default starts at true
 
 function initializeMapScripts() 
-{
-	setUpMap();
-	
+{	
+	//console.log("@@@@ initializeMapScripts()");
+
 	var jsonNodeDiv = $("#jsonNodes");
 	if(jsonNodeDiv.length)
@@ -28,4 +28,5 @@
 		var jsonNodes = eval(jsonNodeDiv.html());
 		
+		renderMap();
 		if(jsonNodes && jsonNodes.length > 0)
 		{
@@ -41,9 +42,9 @@
 		{
 			//hiding the map
-			//$("#map_canvas").css({visibility:"hidden", height:"0px"});
-			$("#map_canvas").css({visibility:"hidden"}); // if you comment this out, add a log message saying "suppressing hiding map"
-		}
-	}
-	
+			mapEnabled = false;
+			$("#map_canvas").css({display:"none"});
+			return;
+		}	
+	}	
 	_docList.loopIndex = 0;
 	
@@ -84,8 +85,7 @@
 }
 
-function setUpMap()
-{
-	//alert("@@@@in map-scripts::setUpMap()");
-	
+function renderMap()
+{
+	//console.log("@@@@in map-scripts::renderMap()");	
 	var myOptions = 
 	    {
@@ -97,5 +97,8 @@
 	
 	if ($map_canvas.length > 0) {
+		//console.log("docList is " + _docList.toString());
 		_map = new google.maps.Map($map_canvas[0], myOptions);
+		//console.log("@@@ created Google _map");
+		
 		google.maps.event.addListener(_map, 'bounds_changed', performSearchForMarkers);
 	}
@@ -104,9 +107,18 @@
 function performSearchForMarkers()
 {
+	//console.log("@@@ performSearchForMarkers()");
+	
+	var bounds = _map.getBounds();
+	var ne = bounds.getNorthEast();
+    var sw = bounds.getSouthWest();
+				  
+	//console.log("bounds: ne = " + ne + ", sw = " + sw);
+	
+	
 	if(_searchRunning)
 	{
+		//console.log("*** performSearchForMarkers(): already running search => not initiating an additional search");
 		return;
 	}
-	
 	
 	
@@ -243,4 +255,6 @@
 	var url = gs.xsltParams.library_name;
 	var data = "a=q&s=RawQuery&rt=rd&c=" + gs.cgiParams.c + "&s1.rawquery=" + query + "&excerptid=jsonNodes";
+	//console.log("*** performSearchForMarkers(): rawQuery query data = " + query);
+	
 	$.ajax({type:"POST", url:url, data:data})
 	.success(function(responseText)
@@ -256,4 +270,5 @@
 
 			var jsonNodes = eval(responseText.substring(startIndex+1, endIndex));
+			//console.log("Number of matches returned from ajax rawQuery search = " + jsonNodes.length);
 			if(jsonNodes && jsonNodes.length > 0)
 			{
@@ -320,4 +335,5 @@
 function updateMap()
 {
+	//console.log("@@@ updateMap()");	
     var markersOnMap = 0;
     var bounds =  new google.maps.LatLngBounds();
@@ -331,5 +347,5 @@
 					doc.shapes[x].setVisible(false);
 				}
-				markersOnMap = doc.shapes.length;
+				//markersOnMap += doc.shapes.length;
 			} else {
 				doc.marker.setVisible(false);
@@ -340,16 +356,20 @@
 		{
 			if(doc.shapes) {
-				for(var x = 0; x < doc.shapes.length; x++) {
+				//console.log("@@@ making doc.shapes["+x+"] visible");
+				for(var x = 0; x < doc.shapes.length; x++) {					
 					doc.shapes[x].setVisible(true);
 				}
-				markersOnMap = doc.shapes.length;
+				markersOnMap += doc.shapes.length;
+				//alert("HERE I AM " + markersOnMap);
+				
 			} else {
 				doc.marker.setVisible(true);
-			}
-			markersOnMap++;
-		}
-
+				markersOnMap++;
+			}
+			
+		}
 		
 		if(doc.coords) {
+			//console.log("@@@@ HERE IN doc.coords");
 			for(var x = 0; x < doc.coords.length; x++) {
 				var coord = doc.coords[x];
@@ -358,16 +378,28 @@
 			}
 		}
+		
+		if(doc.mapoverlay) {
+			console.log("@@@@ HERE IN doc.mapoverlay");
+			var overlay_bounds = ShapesUtil.overlayBounds(doc.shapes);
+			bounds.extend(overlay_bounds.getNorthEast());
+			bounds.extend(overlay_bounds.getSouthWest());
+		}
 		else {
- 	        bounds.extend(new google.maps.LatLng(doc.lat, doc.lng));
-		}
-	}
-	
+			var doc_latlng = new google.maps.LatLng(doc.lat, doc.lng);
+ 	        bounds.extend(doc_latlng);
+		}
+	}
+	
+	//console.log("@@@ UpdateMap() : bounds = " + bounds);				
 	if(markersOnMap > 1)
     {
-	_map.fitBounds(bounds);
+		_map.fitBounds(bounds);
     } else if (markersOnMap == 1) {
-	// sometimes a single point bounds are too small for the map to display, so use center and zoom instead of fitbounds.
-	_map.setCenter(bounds.getCenter());
-	_map.setZoom(18); // arbitrary value that looked nice for my example
+		//console.log("@@@ updating bounds with " + markersOnMap + " markers on the map");
+		//console.log(bounds);
+		
+		// sometimes a single point bounds are too small for the map to display, so use center and zoom instead of fitbounds.
+		_map.setCenter(bounds.getCenter());
+		_map.setZoom(18); // arbitrary value that looked nice for my example
     }
 }
@@ -406,8 +438,17 @@
 	{
 	    var doc = _docList.getDocByIndex(i);
-		if(doc.marker.getVisible())
-		{
-			visibleMarkers.push(doc);
-		}
+		if(typeof doc.shapes !== 'undefined') {			
+			for(var x = 0; x < doc.shapes.length; x++) {
+				var shape = doc.shapes[i];
+				if(shape.type === google.maps.drawing.OverlayType.MARKER && shape.getVisible())
+				{
+					visibleMarkers.push(doc);
+				}				
+			}
+		}
+		if(doc.marker && doc.marker.getVisible())
+		{
+			visibleMarkers.push(doc);		
+		}		
 	}
 	
@@ -421,5 +462,6 @@
 	
 	var elem = null;
-	while(!elem)
+	while(!elem) // TODO: why redefine elem? Why does this work, and only this, but not while(true) or while(doc.marker) or while(!AnythingFalse)???
+				// Some clever behaviour here, but no documentation on the cleverness. Hard to understand
 	{
 		if(_docList.loopIndex >= visibleMarkers.length)
@@ -429,5 +471,5 @@
 
 		var doc = visibleMarkers[_docList.loopIndex];
-		var elem = gs.jqGet("div" + doc.nodeID);
+		elem = gs.jqGet("div" + doc.nodeID); // TODO: this used to redefine elem by doing var elem = <....>
 		if(elem.length)
 		{
@@ -435,7 +477,23 @@
 			setTimeout(function(){elem.css("background", "");}, 2000);
 		}
-		_docList.loopIndex++;
-	}
-	doc.marker.markerInfo.open(_map, doc.marker);
+		_docList.loopIndex ++;		
+	}	
+	//console.log("@@@@ DOC:");
+	//console.log(doc);
+	
+	if(doc.marker) {
+		doc.marker.markerInfo.open(_map, doc.marker); // TODO: how does doc have a value here? Where is the value set at this block level?
+	}
+	
+	if(doc.shapes) {			
+		for(var x = 0; x < doc.shapes.length; x++) {
+			var shape = doc.shapes[i];
+			if(shape.type === google.maps.drawing.OverlayType.MARKER) {
+				shape.markerInfo.open(_map, shape);
+			} else {
+				shape.markerInfo.open(_map);
+			}
+		}
+	}	
 }
 
@@ -503,5 +561,5 @@
 	
 	if(shape.type === google.maps.drawing.OverlayType.MARKER) {
-		var marker = shape;
+		var marker = shape;		
 		marker.addListener('mouseover', function() {
           infoWindow.open(_map, marker);
@@ -524,4 +582,5 @@
 		attachClickHandler(shape, doc.nodeID); // as above		
 	}
+	shape.markerInfo = infoWindow;
 }
 
@@ -535,9 +594,13 @@
 	// for doc.shapes: don't store JSON anymore, convert them to google Shapes overlays and store them instead
 	doc.shapes = [];
+	
 	for (var i=0; i<doc.mapoverlay.length; i++) {
-		var shape = ShapesUtil.JSONToShape(doc.mapoverlay[i]);		
+		//console.log("in: mapoverlay["+i+"] =" + JSON.stringify(doc.mapoverlay[i]));
+		var shape = ShapesUtil.JSONToShape(doc.mapoverlay[i]);	
+		//console.log("out: shape = " + JSON.stringify(shape));
+		
 		doc.shapes[i] = shape;		
 		shape.setMap(_map);
-		shape["title"] = doc.title;
+		//shape["title"] = doc.title;  // TODO: Think on it some more.
 		
 		// Unset editable and draggable properties of shape
@@ -551,9 +614,17 @@
 			}
 			marker.clickable = false; // only markers
+			/*
+			console.log("@@@ map-scripts::addInfoMarker - marker.position");
+			console.log("Lat is " + typeof(marker.position.lat()));
+			console.log(marker.position.lat());
+			console.log("Long is " + typeof(marker.position.lng()));
+			console.log(marker.position.lng());	
+			*/
+		} else {			
+			//console.log("Creating non-marker shape.");
 		}
 		
 		shape.editable = false;
 		shape.draggable = false;
-		
 		
 		
@@ -672,8 +743,9 @@
 		
 		updateMap();
+		//console.log("getSub Classifier -> updateMap()");
 	})
 	.error(function()
 	{
-		console.log("Error getting subclassifiers");
+		//console.log("Error getting subclassifiers");
 		return;
 	});	
@@ -711,7 +783,9 @@
 			var lngDelta = (j - degrees) * 0.01;
 			
-			query += "(" + getDistanceQueryString(lat, latDelta, 2, "LA", ["N","S"]);
-			query += "+AND+";
-			query += getDistanceQueryString(lng, lngDelta, 2, "LN", ["E","W"]) + ")";
+			//query += "(" + getDistanceQueryStringOldApproach(lat, latDelta, 2, "LA", ["N","S"]);
+			//query += "+AND+";
+			//query += getDistanceQueryStringOldApproach(lng, lngDelta, 2, "LN", ["E","W"]) + ")";
+			
+			query += "(" + getDistanceQueryStringTerm(lat, lng, latDelta, lngDelta, 2, "CS") + ")";
 			
 			if(i != ((degrees * 2) - 1) || j != ((degrees * 2) - 1)){ query += "+OR+"; }
@@ -858,6 +932,8 @@
 }
 
-function getDistanceQueryString(currentCoord, delta, precision, indexName, dirs)
-{
+function getDistanceQueryStringOldApproach(currentCoord, delta, precision, indexName, directions)
+{
+	console.error("**** Old Approach called!!!");
+	
 	var query = "";
 	var coordFloat = parseFloat(currentCoord);
@@ -866,11 +942,12 @@
 	var beforeDec = newCoord.substring(0, newCoord.indexOf("."));
 
-	var dir = dirs[0];
+	var direction = directions[0];
 	if(coordFloat < 0)
 	{
-		dir = dirs[1];
-		beforeDec = beforeDec.substring(1);
-	}
-	beforeDec += dir;
+		// negative value
+		direction = directions[1];
+		beforeDec = beforeDec.substring(1); // skip over '-' at front
+	}
+	beforeDec += direction;
 
 	var afterDec = newCoord.substring(newCoord.indexOf(".") + 1, newCoord.indexOf(".") + (precision) + 1);
@@ -878,2 +955,36 @@
 	return indexName + ":" + beforeDec + "+AND+" + indexName + ":" + afterDec;
 }
+
+function coordValToIndexToken(coordValStr, delta, precision, directions)
+{
+	var coordValFloat = parseFloat(coordValStr);
+
+	var deltaCoordValStr = "" + (coordValFloat + delta);
+	var beforeDec = deltaCoordValStr.substring(0, deltaCoordValStr.indexOf("."));
+
+	var direction = directions[0];
+	if(coordValFloat < 0)
+	{
+		// negative value
+		direction = directions[1];
+		beforeDec = beforeDec.substring(1); // skip over '-' at front
+	}
+	
+	var beforeDecWithDirection = beforeDec + direction;
+	
+	var afterDecPrecision = deltaCoordValStr.substring(deltaCoordValStr.indexOf(".") + 1, deltaCoordValStr.indexOf(".") + (precision) + 1);
+
+    var indexToken = beforeDecWithDirection + afterDecPrecision;	
+
+	return indexToken;
+}
+
+function getDistanceQueryStringTerm(currentLat,currentLng, deltaLat, deltaLng, precision, indexName)
+{
+	var latToken = coordValToIndexToken(currentLat,deltaLat,precision,["N","S"]);
+	var lngToken = coordValToIndexToken(currentLng,deltaLng,precision,["E","W"]);
+	
+	var queryStringTerm = indexName + ":\"" + latToken + " " + lngToken + "\"";
+	
+	return queryStringTerm;
+}
