function myclick(i) {
    gmarkers[i].openInfoWindowHtml(htmls[i]);
}
  
  
var gmarkers = [];
var htmls = [];
				
function buildPropertyMap(categoryID, regionID) {
	
	$('#map_properties').html('');
	var side_bar_html = "";
	
	if (GBrowserIsCompatible()) { 
	  
	  
		var map = new GMap2(document.getElementById("map"));
	  	map.addControl(new GMapTypeControl());
		map.addControl(new GLargeMapControl());
		map.addControl(new GOverviewMapControl());
	  	
	  	// Don't know any values, set center to make map play nice before adding markers
	   map.setCenter(new GLatLng(0,0),0);
	  	
	  
		var i = 0;
		
	  	// Start with an empty GLatLngBounds object   
		var bounds = new GLatLngBounds();
	   
	   // Empty sidebar var
	   var side_bar_html = "";
	   
	   // Create a marker function (continues loop)
	   function createMarker(markerNum,point,name,html) {
				
			var blueIcon = new GIcon();
			blueIcon.image = "/property/map/img/marker"+markerNum+".png";
			blueIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
			blueIcon.iconSize = new GSize(20, 34);
			blueIcon.shadowSize = new GSize(37, 34);
			blueIcon.iconAnchor = new GPoint(9, 34);
			blueIcon.infoWindowAnchor = new GPoint(9, 2);
			blueIcon.infoShadowAnchor = new GPoint(18, 25);
			
			var marker = new GMarker(point, blueIcon);
			
			GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(html);
			});
			gmarkers[i] = marker;
			htmls[i] = html;
			side_bar_html += '<li><a href="javascript:myclick(' + i + ')"><img src="/property/map/img/marker'+markerNum+'.png" />' + name + '</a><li>';
        	
			i++;
			return marker;
		}
	  
		// Read the data from XML
		var request = GXmlHttp.create();
		request.open("GET", "/property/map/property-xml.php?region="+regionID+"&category="+categoryID+"", true);
		request.onreadystatechange = function() {
			if (request.readyState == 4) {
				var xmlDoc = request.responseXML;
				/* This whole block is a pain in the IE
				--------------------------------------- */
				if (!xmlDoc.documentElement) {
					$("#no_message").show();
                                        $("#no_message").html('Unfortunately there are no featured property sites to match your search. <br />Please contact us at york-england.com for alternative opportunities on: 01904 641118, or its.all@york-england.com.');
      			
	  				// hide the map
	  				$('#map_properties').html('');
	  				$("#map").css("visibility","hidden");
	  				
	  				// exit
	  				return;	
	  				
				} else {
					// obtain the array of markers and loop through it
					var markers = xmlDoc.documentElement.getElementsByTagName("marker");
	      	}
	      	/* --------------------------------------- */
	        	
	        	// if no markers (IE6 doesn't get this, returns no object)
	        	if (markers.length==0) {

	        		// Show error message
	  				$("#no_message").show();
      			$("#no_message").html('Unfortunately there are no featured property sites to match your search. <br />Please contact us at york-england.com for alternative opportunities on: 01904 641118, or its.all@york-england.com.');
      			
	  				// hide the map
	  				$('#map_properties').html('');
	  				$("#map").css("visibility","hidden");
	  				
	  				// exit
	  				return;	
	        	}
	        		
	        	// If we have markers
				else {
					$("#no_message").hide();
	  				// show the map
	  				$("#map").css("visibility","visible");;
	  				
					for (var i = 0; i < markers.length; i++) {
					
						// obtain the attribues of each marker
						var markerNum = i+1;
	  					
	  					var pid = markers[i].getAttribute("pid");
	  					var query = markers[i].getAttribute("query");
	  					var description = markers[i].getAttribute("description");
						var label = markers[i].getAttribute("label");
	            	var categoryQuery = markers[i].getAttribute("category");
	            	var PImage = markers[i].getAttribute("image");
	            	
	  					var lat = parseFloat(markers[i].getAttribute("lat"));
	            	var lng = parseFloat(markers[i].getAttribute("lng"));    	
	            	
	            	var point = new GLatLng(lat,lng);
						
	            	var propLink = '/property/featured-properties/'+categoryQuery+'/id'+pid+'/'+query+'/';
						
						var html = '<div class="map_info_window">';
	            	if (PImage=='true') {
	            		html = html + '<img src="/img/thumbs/properties'+pid+'.jpg" class="map_img" />';	
	            	}
	            	html = html + '<h4 class="map_heading">'+label+'</h4><p class="map_p">'+description+' <a href="'+propLink+'" class="view_full">view full details</a></p>';
	            	html = html + '</div>';
	            	
	            	// create the marker
						var marker = createMarker(markerNum,point,label,html);
						map.addOverlay(marker);
	            
	            	// ==== Each time a point is found, extent the bounds ato include it =====
	            	bounds.extend(point);
	            	
	            	document.getElementById("map_properties").innerHTML = side_bar_html;
	        
						// ===== determine the zoom level from the bounds =====
						map.setZoom(map.getBoundsZoomLevel(bounds));
			  
						// ===== determine the centre from the bounds ======
						/*
						var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
						var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
						map.setCenter(new GLatLng(clat,clng));
						*/
						
						map.setCenter(bounds.getCenter());
						
						//map.setMapType(G_HYBRID_MAP); 
	            	
					}
	  			}
	  			// Else show error
	  			//else {
	  				// Show error message
	  				//$("#no_message").show();
      			//$("#no_message").html('There were no properties found with these filters on.');
      			
	  				// hide the map
	  				//$('#map_properties').html('');
	  				//$("#map").css("visibility","hidden");
	  				
	  				// exit
	  				//return;
	  			//}
	  
	        
				
			}
		}
		request.send(null);
  
	}
	// display a warning if the browser was not compatible
	else {
		alert("Sorry, the Google Maps API is not compatible with this browser");
	}
}
