var map, houseCluster, flatCluster, commercialCluster, locationCluster, eventListeners=[];
var houseMarkersArray=[], flatMarkersArray=[], commercialMarkersArray=[], icon;

//var houseImageUrl = 'http://gmaps-utility-library.googlecode.com/svn/trunk/markermanager/release/examples/images/house.png';
//var houseShadowUrl = 'http://gmaps-utility-library.googlecode.com/svn/trunk/markermanager/release/examples/images/house.png';

var thisSiteUrl = window.location.href;
var thisPage, tmpPage, addedLoc;

var marker;

addedLoc = "";

if(thisSiteUrl.indexOf('http') > -1)
{
		
	if(thisSiteUrl.indexOf('localhost') > -1)
	{
		thisPage = thisSiteUrl.substr(0,thisSiteUrl.indexOf('/',9)) + "/10ninety";	
	}	
	else
	{

		//thisPage = 'http://www.10ninety.co.uk/1090dev';

		//thisPage = 'http://www.10ninety.co.uk/' + thisSiteUrl.substr(29,thisSiteUrl.indexOf('/',29));

		thisPage = thisSiteUrl.substr(0,thisSiteUrl.indexOf('/',8));

		if(thisPage.indexOf('10ninety'))
		{
			thisPage = thisSiteUrl.substr(0,thisSiteUrl.indexOf('/',thisPage.length + 1));
		}
	}
}	
else
{
	thisPage = thisSiteUrl.substr(0,thisSiteUrl.indexOf('/'));
}

var houseImageUrl = thisPage + '/images/map/house.png';
var houseClusterImageUrl = thisPage + '/images/map/housecluster.png';

var flatImageUrl = thisPage + '/images/map/flat.png';
var flatClusterImageUrl = thisPage + '/images/map/flatcluster.png';

var commercialImageUrl = thisPage + '/images/map/commercial.png';
var commercialClusterImageUrl = thisPage + '/images/map/commercialcluster.png';

var ssSchoolImageUrl = thisPage + '/images/map/secondaryschool.png';
var psSchoolImageUrl = thisPage + '/images/map/primaryschool.png';
var isSchoolImageUrl = thisPage + '/images/map/independentschool.png';
var schoolClusterImageUrl = thisPage + '/images/map/schoolcluster.png';



function myOnLoad() {

	if (GBrowserIsCompatible() && (getQueryVariable("searchbymap") == "true" || thisSiteUrl.indexOf('propertydet') > -1)) {
			
		
		map=new GMap2(document.getElementById('map'));
		map.setCenter(new GLatLng(0, 0), 0, G_NORMAL_MAP);
		
		map.addControl(new GLargeMapControl(), new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7)));
		map.addControl(new GMapTypeControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 7)));
		
		if(getQueryVariable("locations").indexOf("SCHOOL") > -1)
		{
		
			var locationClusterIcon=new GIcon();						
			locationClusterIcon.image = schoolClusterImageUrl;
			locationClusterIcon.iconSize=new GSize(35, 35);
			locationClusterIcon.iconAnchor=new GPoint(5, 35);		
			locationClusterIcon.infoWindowAnchor=new GPoint(11, 9);

			//locationCluster=new ClusterMarker(map, {clusterMarkerTitle:'UK Schools'});

			locationCluster=new ClusterMarker(map, {clusterMarkerTitle:'%count Schools, Click to view',clusterMarkerIcon:locationClusterIcon});		

			//	add an HtmlControl containing a couple of form elements
			//	see http://googlemapsapi.martinpearman.co.uk/htmlcontrol for more info on HtmlControl
			//var html='<div class="htmlControl" style="padding:0px 3px 3px 3px">Enable clustering: <input type="checkbox" checked="checked" onclick="toggleClustering()" /><form id="examplesForm" action=""><p>Choose markers:<br /><select name="examplesSelect" onchange="selectExample()"><option value="../clustermarker/scripts/marker_data_01.php" selected="selected">Panorama Camera</option><option value="../clustermarker/scripts/marker_data_02.php">Tine\'s markers</option><option value="random">Random 850</option></select></p></form></div>';
			//var control=new HtmlControl(html);
			//map.addControl(control, new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7,28)));

			// LISTENERS zoomend and moveend

			GEvent.addListener(map, "moveend", function() {
				//var center=map.getCenter();
				var zoomlevel=map.getZoom();
				// make another cluster for locations

				if(parseInt(zoomlevel) > 12)
				{
					if(addedLoc.length > 20000)
					{
						// too many locations in the cache
						// start again
						locationCluster.removeMarkers();
						addedLoc = "";
					}						

					displayLocations("SCHOOL",locationCluster);
				}
				else
				{
					locationCluster.removeMarkers();
					addedLoc = "";
				}

			});

		
		// end of locations
		
		}

		//	create our ClusterMarkers
		//cluster=new ClusterMarker(map, {clusterMarkerTitle:'%count Properties, click to view'});
		
		var houseClusterIcon=new GIcon();						
		houseClusterIcon.image = houseClusterImageUrl;
		houseClusterIcon.iconSize=new GSize(35, 35);
		houseClusterIcon.iconAnchor=new GPoint(5, 35);		
		houseClusterIcon.infoWindowAnchor=new GPoint(10, 10);
		
		var flatClusterIcon=new GIcon();						
		flatClusterIcon.image = flatClusterImageUrl;
		flatClusterIcon.iconSize=new GSize(35, 35);
		flatClusterIcon.iconAnchor=new GPoint(5, 35);		
		flatClusterIcon.infoWindowAnchor=new GPoint(10, 10);
		
		var commercialClusterIcon=new GIcon();						
		commercialClusterIcon.image = commercialClusterImageUrl;
		commercialClusterIcon.iconSize=new GSize(35, 35);
		commercialClusterIcon.iconAnchor=new GPoint(5, 35);		
		commercialClusterIcon.infoWindowAnchor=new GPoint(10, 10);			
		
		houseCluster=new ClusterMarker(map, {clusterMarkerTitle:'%count properties, click to view',clusterMarkerIcon:houseClusterIcon});
		flatCluster=new ClusterMarker(map, {clusterMarkerTitle:'%count properties, click to view',clusterMarkerIcon:flatClusterIcon});
		commercialCluster=new ClusterMarker(map, {clusterMarkerTitle:'%count properties, click to view',clusterMarkerIcon:commercialClusterIcon});
		
		// icon for properties
		
	   	if(thisSiteUrl.indexOf('propertydet.asp') > -1)
	   	{
			// max zoom when unclustering on single property page						
			houseCluster.fitMapMaxZoom=14;		
			flatCluster.fitMapMaxZoom=14;
			commercialCluster.fitMapMaxZoom=14;								
	   	}		
		
		icon=new GIcon();
		
		icon.iconSize=new GSize(25, 25);
		icon.iconAnchor=new GPoint(6, 25);
		
		icon.infoWindowAnchor=new GPoint(5, 1);		
		
		drawProperties(getQueryVariable("lat"),getQueryVariable("lng"),getQueryVariable("zoom"));
		
		var houseBounds, flatBounds, commBounds = null;
		// refresh our clusters
		
		// check arrays to see if we have stuff in the clusters
		if(houseMarkersArray.length > 0)
		{
			houseCluster.refresh();			
			houseCluster.fitMapToMarkers();
			houseBounds = map.getBounds();
		}
		
		if(flatMarkersArray.length > 0)
		{					
			flatCluster.refresh();			
			flatCluster.fitMapToMarkers();
			flatBounds = map.getBounds();
			
		}
		
		if(commercialMarkersArray.length > 0)
		{					
			commercialCluster.refresh();
			commercialCluster.fitMapToMarkers();
			commBounds = map.getBounds();
		}						

		// check zoom
		
		if(getQueryVariable("zoom").length > 0)
		{
		    // set the map back up from their last view
		    map.setCenter(new GLatLng(getQueryVariable("lat"), getQueryVariable("lng")), parseInt(getQueryVariable("zoom")), G_NORMAL_MAP);
		}
		else
		{
		    
		    if(houseBounds != null)
		    {
		    	// if there is flat bounds
		    	if(flatBounds != null)
		    	{
		    		// extend our houseBounds to contain the top-left
		    		// and bottom right of our flatBounds
		    		houseBounds.extend(flatBounds.getSouthWest());
		    		houseBounds.extend(flatBounds.getNorthEast());
		    	}
		    	
		    	if(commBounds != null)		    	
		    	{
		    		// extend our houseBounds to contain the top-left
		    		// and bottom right of our commercialBounds
		    		houseBounds.extend(commBounds.getSouthWest());
		    		houseBounds.extend(commBounds.getNorthEast());		    	
		    	}
		    	
		    	// set viewpoint to current house bounds
		    	map.setCenter(houseBounds.getCenter(), map.getBoundsZoomLevel(houseBounds), G_NORMAL_MAP);
		    }
		    else if(flatBounds != null)
		    {
		    
		    	// there is no house bounds - only flats
		    	// check to see if there are commercial ones
		    	if(commBounds != null)
		    	{
		    		// extend our flatBounds to contain the top-left
		    		// and bottom right of our commercialBounds
		    		flatBounds.extend(commBounds.getSouthWest());
		    		flatBounds.extend(commBounds.getNorthEast());

		    	}
		    	
		    	// set viewpoint to current flat bounds
		    	map.setCenter(flatBounds.getCenter(), map.getBoundsZoomLevel(flatBounds), G_NORMAL_MAP);		    	
		    
		    }
		    else
		    {
		    	// only commercial properties exist for this search
		    	if(commBounds != null)
		    	{
		    		map.setCenter(commBounds.getCenter(), map.getBoundsZoomLevel(commBounds), G_NORMAL_MAP);
		    	}	
		    }
	
		}
			
	   	// if its the property details page
	   	if(thisSiteUrl.indexOf('propertydet.asp') > -1)
	   	{
			GEvent.trigger(marker,'click');			
	   	}				
		
		//houseMarkersArray = [];
		//flatMarkersArray = [];
		//commercialMarkersArray = [];
		marker = null;
		
		displayMapKey();
		
	}
}

function newMarker(markerLocation,icontitle, title, markerIcon) {
	//var marker=new GMarker(markerLocation, {title:title, icon:markerIcon});
	var ptitle,ntitle;
	ntitle = title;
	
	ptitle = icontitle;

	//ptitle = "Property Details";
	var marker=new GMarker(markerLocation, {title:ptitle, icon:markerIcon});
	eventListeners.push(GEvent.addListener(marker, 'click', function() {

		//insert the zoom level - done twice as there are two links
		
		ntitle = title.replace(/zoom=&/,"zoom=" + map.getZoom() + "&");
		ntitle = ntitle.replace(/zoom=&/,"zoom=" + map.getZoom() + "&");
		ntitle = ntitle.replace(/zoom=&/,"zoom=" + map.getZoom() + "&");
		
		if(ntitle.length = title.length && ntitle.match(title) != null)
		{
		    ntitle = title.replace(/zoom=d+&/,"zoom=" + map.getZoom() + "&");
		    ntitle = ntitle.replace(/zoom=d+&/,"zoom=" + map.getZoom() + "&");
		    ntitle = ntitle.replace(/zoom=d+&/,"zoom=" + map.getZoom() + "&");
		}			
		else
		{
		   ntitle = title.replace(/zoom=&/,"zoom=" + map.getZoom() + "&");
		   ntitle = ntitle.replace(/zoom=&/,"zoom=" + map.getZoom() + "&");
		   ntitle = ntitle.replace(/zoom=&/,"zoom=" + map.getZoom() + "&");
		}
		marker.openInfoWindowHtml('<p>'+ntitle+'</p>');
	}));

	return marker;
}

function toggleClustering() {
	cluster.clusteringEnabled=!cluster.clusteringEnabled;
	cluster.refresh(true);
}

function drawProperties(latStr,lngStr,zoomStr)	
{
	// remove markers
	
	//cluster.removeMarkers();
	houseCluster.removeMarkers();
	flatCluster.removeMarkers();
	commercialCluster.removeMarkers();
	
	//markersArray=[];
	houseMarkersArray=[];
	flatMarkersArray=[];
	commercialMarkersArray=[];
	
	for(i=eventListeners.length-1; i>=0; i--){
		GEvent.removeListener(eventListeners[i]);
	}
	
	// get the markers from the xml...
	
	markers = createMarkers(latStr,lngStr,zoomStr);	
}

function createMarkers(latStr,lngStr,zoomStr)
{

	var lat, long, price, prop_type, prop_id, entity_id, img, addr, beds, prop_title, status, agent_id, agent_name, agent_branch, det_page, category, distance = "";
	var propInfoHtml = "";
	var newIcon;
	var sPage,sPropind,sCountry,sTown,sArea,sMinPrice,sMaxPrice,sMinBeds,sBedsEqual,sPropType,sFurn,sAvail,sO,sDir,sByMap,sStatusSql,sCategory;
	var sQueryStr;
	var swlat, swlng, nelat, nelng;
	var markertitle, showLocs;	
	
	//make our search query string
	
	sPage = getQueryVariable("page");
	sPropind = getQueryVariable("propind");
	sCountry= getQueryVariable("country");
	sTown = getQueryVariable("town");
	sArea = getQueryVariable("area");
	sMinPrice = getQueryVariable("MinPrice");
	sMaxPrice = getQueryVariable("MaxPrice");
	sMinBeds = getQueryVariable("MinBeds");
	sBedsEqual = getQueryVariable("BedsEqual");
	sPropType = getQueryVariable("PropType");
	sFurn = getQueryVariable("Furn");
	sAvail = getQueryVariable("Avail");
	sO = getQueryVariable("O");
	sDir = getQueryVariable("Dir");		
	sByMap = getQueryVariable("searchbymap");
	showLocs = getQueryVariable("locations");
	sStatusSql = getQueryVariable("SS");
	sCategory = getQueryVariable("Cat");
	
	sQueryStr = "page=" + sPage + "&propind=" + sPropind + "&country=" + sCountry + "&town=" + sTown + "&area=" + sArea + "&MinPrice=" + sMinPrice + "&MaxPrice=" + sMaxPrice + "&MinBeds=" + sMinBeds + "&BedsEqual=" + sBedsEqual + "&PropType=" + sPropType + "&Furn=" + sFurn + "&Avail=" + sAvail + "&O=" + sO + "&Dir=" + sDir + "&searchbymap=" + sByMap + "&locations=" + showLocs + "&SS=" + sStatusSql + "&Cat=" + sCategory;
	
	// get xml from hidden field		
	
	var xdata = '<markers>';
	
	// alert(xdata);
	
	xdata = xdata + document.getElementById("mapxml").value;
	
	
	xdata = xdata + '   </markers>';			
	
	var xml = GXml.parse(xdata);
	var ourMarkers = xml.documentElement.getElementsByTagName("marker");
	var markCnt = ourMarkers.length;
	// loop through markers
	j = 0;
	for(j;j<markCnt;j++) {
		
		// get stuff from xml		
		lat = ourMarkers[j].getAttribute("lat");
		lng = ourMarkers[j].getAttribute("lng");
		price = ourMarkers[j].getAttribute("price");
		prop_type = ourMarkers[j].getAttribute("prop_type");		
		prop_id = ourMarkers[j].getAttribute("prop_id");
		entity_id = ourMarkers[j].getAttribute("entity_id");
		img = new String(ourMarkers[j].getAttribute("img"));
		addr = ourMarkers[j].getAttribute("addr");
		beds = ourMarkers[j].getAttribute("beds");
		prop_title = ourMarkers[j].getAttribute("prop_title");		
		status = ourMarkers[j].getAttribute("status");		
		agent_id = ourMarkers[j].getAttribute("agent_id");
		agent_name = ourMarkers[j].getAttribute("agent_name");
		agent_branch = ourMarkers[j].getAttribute("agent_branch");
		det_page = ourMarkers[j].getAttribute("det_page");
		distance = ourMarkers[j].getAttribute("distance");	
		category = ourMarkers[j].getAttribute("category");	
		
		if(beds != null)
		{		
			markertitle = price + " " + beds;
		}
		else
		{
			markertitle = price;		
		}
		

		markertitle = markertitle + " " + prop_type;	
		
		'build the property details - if it is one'

		// make the html
		propInfoHtml = '<div class="map-prop-det">';
		
		//propInfoHtml = propInfoHtml + '<div class="floatleft"><a href="propertydet.asp?id=' + prop_id + '&lat=' + lat + '&lng=' + lng + '&zoom=&' + sQueryStr + '"><img src="' + img + '" alt="' + prop_title + '"/></a></div>';
		//propInfoHtml = propInfoHtml + '<div class="floatright"><a href="propertydet.asp?id=' + prop_id + '&lat=' + lat + '&lng=' + lng + '&zoom=&' + sQueryStr + '">' + prop_title + '</a><br/>';
		//propInfoHtml = propInfoHtml + addr + '<br/>' + beds + ' beds '; 
		//propInfoHtml = propInfoHtml + '<br/>&pound;' + price + '<br/><a href="propertydet.asp?id=' + prop_id + '&lat=' + lat + '&lng=' + lng + '&zoom=&' + sQueryStr + '">view details</a>' + '</div>';
		//propInfoHtml = propInfoHtml + '<div class="clearboth">&nbsp;</div></div><div class="clearboth">&nbsp;</div><br/>';			
		
		propInfoHtml = propInfoHtml + '<table><tr><td class="map-prop-det-left"><a href="' + det_page + '?id=' + prop_id + '&lat=' + lat + '&lng=' + lng + '&zoom=&' + sQueryStr + '"><img src="' + img.replace(/%26/g,"&") + '" alt="' + prop_title + '"/></a>';

		if(status != '' && status != ' ')
		{
			propInfoHtml = propInfoHtml + '<div class="status">' + status + '</div>';
		}

		propInfoHtml = propInfoHtml + '</td>';
		propInfoHtml = propInfoHtml + '<td class="map-prop-det-right"><div class="address"><a href="' + det_page + '?id=' + prop_id + '&lat=' + lat + '&lng=' + lng + '&zoom=&' + sQueryStr + '">' + prop_title + '</a></div>';
		
		if(beds != null)
		{
			propInfoHtml = propInfoHtml + '<div class="beds">' + beds + '</div>'; 
		}	
		
		propInfoHtml = propInfoHtml + '<div class="proptype">' + prop_type + '</div>'; 
		propInfoHtml = propInfoHtml + '<div class="price">' + price + '</div></td></tr></table>';
		propInfoHtml = propInfoHtml + '<div class="clearboth">&nbsp;</div></div><br/>';		

		// check category field first to see if the property is commercial			

		if(isCommercial(category))
		{
			   newIcon=new GIcon(icon, commercialImageUrl);	
			   
			   // make a new pdmarker
			   marker = new PdMarker(new GLatLng(lat,lng),newIcon);		
			   marker.setTooltip(convertToHtml(markertitle));
			   marker.setUserData(convertToHtml(propInfoHtml));
			   marker.setUserData2("PROPERTY");
			   marker.bindInfoWindowHtml(convertToHtml(propInfoHtml));

			   commercialMarkersArray.push(marker);				   
		}else
		{			   
		
		// if not, check prop_type, choose image accordingly

		switch(prop_type)
		{
			case "Detached":
			case "Cottage":
			case "Bungalow":			
			case "Semi-detached":
			case "Terraced":
			case "House share":
			case "Town house":						
			
			   newIcon=new GIcon(icon, houseImageUrl);
			   
			   // make a new pdmarker
			   marker = new PdMarker(new GLatLng(lat,lng),newIcon);		
			   marker.setTooltip(convertToHtml(markertitle));
			   marker.setUserData(convertToHtml(propInfoHtml));
			   marker.setUserData2("PROPERTY");
			   marker.bindInfoWindowHtml(convertToHtml(propInfoHtml));			   

			   houseMarkersArray.push(marker);	
			   			   
			break;
			
			case "Flat":
			case "Single room":
			case "Double room":			
			case "Bedsit":
			case "Maisonette":
			case "Studio":
			case "Executive flat":						
			
			   newIcon=new GIcon(icon, flatImageUrl);	
			   
			   // make a new pdmarker
			   marker = new PdMarker(new GLatLng(lat,lng),newIcon);		
			   marker.setTooltip(convertToHtml(markertitle));
			   marker.setUserData(convertToHtml(propInfoHtml));
			   marker.setUserData2("PROPERTY");
			   marker.bindInfoWindowHtml(convertToHtml(propInfoHtml));			   		   
			   
			   flatMarkersArray.push(marker);	
			   
			   
			break;

			case "Land":
			case "Office":
			case "Commercial":
			
			   newIcon=new GIcon(icon, commercialImageUrl);	
			   
			   // make a new pdmarker
			   marker = new PdMarker(new GLatLng(lat,lng),newIcon);		
			   marker.setTooltip(convertToHtml(markertitle));
			   marker.setUserData(convertToHtml(propInfoHtml));
			   marker.setUserData2("PROPERTY");
			   marker.bindInfoWindowHtml(convertToHtml(propInfoHtml));

			   commercialMarkersArray.push(marker);				   
			   
			break;
			
			default:
			
			newIcon=new GIcon(icon, houseImageUrl);	
		        // make a new pdmarker
		        marker = new PdMarker(new GLatLng(lat,lng),newIcon);		
		        marker.setTooltip(convertToHtml(markertitle));
		        marker.setUserData(convertToHtml(propInfoHtml));
		        marker.setUserData2("PROPERTY");
		        marker.bindInfoWindowHtml(convertToHtml(propInfoHtml));

		        houseMarkersArray.push(marker);			
		}	

		}					

	}
	
	// add the markers to the appropriate clusters - if they're there
	
	if(houseMarkersArray.length > 0)
	{
		houseCluster.addMarkers(houseMarkersArray);
	}
	
	if(flatMarkersArray.length > 0)
	{
		flatCluster.addMarkers(flatMarkersArray);
	}
	
	if(commercialMarkersArray.length > 0)
	{
		commercialCluster.addMarkers(commercialMarkersArray);
	}		
	
}

function isCommercial(cat)
{
	if(cat == "COMM" || cat == "COMMS")
	{
		return true;
	}else
	{
		return false;
	}
}

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
  return "";
}

function displayLocations(locType,thisCluster)
{

	
	// get the bounds and add schools accordingly	
			
	swlat = map.getBounds().getSouthWest().lat();
	
	swlng = map.getBounds().getSouthWest().lng();
		
	nelat = map.getBounds().getNorthEast().lat();	
		
	nelng = map.getBounds().getNorthEast().lng();
	
	// ALL SCHOOLS
	
	var locPath, oldBounds, oldZoom, data1, data2, data3, data4, data5;
	
	alerted = false;
		
	oldBounds = map.getBounds();
	
	//oldZoom = map.getZoom();
	
	locPath = "http://www.10ninety.co.uk/1090/pps/map/makelocationxml.asp?location=" + locType + "&swlat=" + swlat + "&swlng=" + swlng + "&nelat=" + nelat + "&nelng=" + nelng ;
	
	//var newMarkers;
	
	GDownloadUrl(locPath, function(data) {

		// get rid of invalid chars
		
		data = data.replace(" & "," And ");
		data = data.replace(" &"," And");
		data = data.replace("& ","And ");
		data = data.replace("&","And");
		data = data.replace("’","^");
		
		
		// get rid of hyphens
		
		data = data.replace("-","***");
		
		//data = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + data;
		
		try{
		
			var txml = GXml.parse(data);

			if(testIsValidObject(txml.documentElement))
			{ 
				newMarkers = txml.documentElement.getElementsByTagName("marker");
			}
			else
			{
				document.getElementById("maptextarea").value = data;
				newMarkers = [];
			}

			//setTimeout(function(){addSchoolMarkers(markers,pos);},0);				
			if(newMarkers != null && newMarkers.length > 0)
			{

				var tmpok = addLocationMarkers(thisCluster,newMarkers,locType,oldBounds,oldZoom);
			}	

			txml = null;
			newMarkers = [];		
		
		}
		
		catch(e)
		{
			alert(e);
		}		
		
	});	
	
}

function addLocationMarkers(thisCluster,tmarkers,loctype,oldBounds,oldZoom)
{
	var j, lat, lng, addr, title, propInfoHtml, locMarkersArray, subtype,iconURL;
	var markCnt = tmarkers.length;

	locMarkersArray = [];		
	
	// loop through markers
	j = 0;
	for(j;j<markCnt;j++) {
		
		// get stuff from xml				
		
		subtype = tmarkers[j].getAttribute("subtype");
		lat = tmarkers[j].getAttribute("lat");		
		lng = tmarkers[j].getAttribute("long");
		// make sure we aint got bad hyphens
		addr = tmarkers[j].getAttribute("address");
		title = tmarkers[j].getAttribute("title");
				
		lat = lat.replace("***","-");
		lng = lng.replace("***","-");
		
		subtype = subtype.replace("***","&shy;");
		addr = addr.replace("***","&shy;");
		title = title.replace("***","&shy;");
						
		//alert("NEW SCHOOL MARKER: LAT: " + lat + " , LNG: " + lng + " title: " + title);
		
		if(loctype = "SCHOOL")
		{		

			// make the html
			propInfoHtml = '<div class="map-prop-det">';

			var addrArr = addr.split(",");
			var addrStr = "";
			var k = 0;
			
			for(k=0; k < addrArr.length; k++)
			{				
				var tmpStr = addrArr[k];
				
				if(tmpStr.indexOf("Tel") > 1)
				{
					// insert a <br/>
					tmpStr = tmpStr.replace(/Tel/,"<br/>Tel");
				}
				
				if(addrStr.length > 0)
				{
					addrStr = addrStr + tmpStr + "<br/>";	
				}
				else
				{
					addrStr = tmpStr + "<br/>";					
				}											
			}			
			
			propInfoHtml = propInfoHtml + '<table><tr><td valign="top"><b>' + title + '</b></td></tr>';
			
			propInfoHtml = propInfoHtml + '<tr><td>' +  addrStr  + '</td></tr>';			
			
			propInfoHtml = propInfoHtml + '</table></div><br/>';

			if(subtype=='SS')
			{
				iconURL = ssSchoolImageUrl;
			}else if(subtype=='PS')
			{
				iconURL = psSchoolImageUrl;
			}else if(subtype=='IS')
			{
				iconURL = isSchoolImageUrl;
			}

			newIcon=new GIcon(icon, iconURL);
			
			addrStr = "";

		}	
		
		
		// pdmarker		
		marker = new PdMarker(new GLatLng(lat,lng),newIcon);		
		marker.setTooltip(convertToHtml(title));
		marker.setUserData(convertToHtml(propInfoHtml));		

		// set up info window
		var opts = new Object();
		opts.noCloseOnClick = true;
		
		marker.bindInfoWindowHtml(convertToHtml(propInfoHtml),opts);
		
		if(addedLoc.indexOf(lat + ":" + lng + "-" + title + "#") == -1)
		{
			locMarkersArray.push(marker);
			addedLoc = addedLoc + lat + ":" + lng + "-" + title + "#";
		}	
	}
	
	if(markCnt > 0)
	{		
		
		thisCluster.addMarkers(locMarkersArray);
		thisCluster.refresh();		
	}				
}

function convertToHtml(thisStr)
{
	
	thisStr = thisStr.replace("^","'");
	thisStr = thisStr.replace("^","'");
	thisStr = thisStr.replace("^","'");
	thisStr = thisStr.replace("^","'");
	thisStr = thisStr.replace("^","'");
	
	thisStr = thisStr.replace(" , "," ,");
	thisStr = thisStr.replace(" , "," ,");
	thisStr = thisStr.replace(" , "," ,");
	thisStr = thisStr.replace(" , "," ,");
	thisStr = thisStr.replace(" , "," ,");
	
	return thisStr;
}

function displayMapKey()
{
	//build up the html
	var keyHtml;
	// house
	keyHtml = '<div class="map-key-item">';
	keyHtml = keyHtml + '<img src="' + houseImageUrl + '" alt="House"/>House';
	keyHtml = keyHtml + '</div>';
	keyHtml = keyHtml + '<div class="map-key-item">';
	keyHtml = keyHtml + '<img src="' + houseClusterImageUrl + '" alt="Houses"/>Houses';
	keyHtml = keyHtml + '</div>';
	keyHtml = keyHtml + '<div class="map-key-item">';
	keyHtml = keyHtml + '<img src="' + flatImageUrl + '" alt="Flat"/>Flat';
	keyHtml = keyHtml + '</div>';	
	keyHtml = keyHtml + '<div class="map-key-item">';
	keyHtml = keyHtml + '<img src="' + flatClusterImageUrl + '" alt="Flats"/>Flats';
	keyHtml = keyHtml + '</div>';
	keyHtml = keyHtml + '<div class="map-key-item">';
	keyHtml = keyHtml + '<img src="' + commercialImageUrl + '" alt="Commercial"/>Commercial';
	keyHtml = keyHtml + '</div>';	
	keyHtml = keyHtml + '<div class="map-key-item">';
	keyHtml = keyHtml + '<img src="' + commercialClusterImageUrl + '" alt="Commercial"/>Commercial';
	keyHtml = keyHtml + '</div>';
	
	// check query string to see what locations they have
	if(getQueryVariable("locations").indexOf("SCHOOL") > -1)
	{	
		
		// add the schools icons
		keyHtml = keyHtml + '<div class="map-key-item">';
		keyHtml = keyHtml + '<img src="' + psSchoolImageUrl + '" alt="Primary School"/>Primary School';
		keyHtml = keyHtml + '</div>';			
		
		keyHtml = keyHtml + '<div class="map-key-item">';
		keyHtml = keyHtml + '<img src="' + ssSchoolImageUrl + '" alt="Secondary School"/>Secondary School';
		keyHtml = keyHtml + '</div>';			
		
		keyHtml = keyHtml + '<div class="map-key-item">';
		keyHtml = keyHtml + '<img src="' + isSchoolImageUrl + '" alt="Independent School"/>Independent School';
		keyHtml = keyHtml + '</div>';
		
		keyHtml = keyHtml + '<div class="map-key-item">';
		keyHtml = keyHtml + '<img src="' + schoolClusterImageUrl + '" alt="Schools"/>Schools';
		keyHtml = keyHtml + '</div>';				
	}

	keyHtml = keyHtml.replace(/.png/g,'.gif')
	
	document.getElementById("mapkey").innerHTML = keyHtml;
	
}

function testIsValidObject(objToTest)
{

            if (null == objToTest) {

                        return false;
            }

            if ("undefined" == typeof(objToTest) ) {

                        return false;
            }

            if ("undefined" == objToTest.toString) {

                        return false;
            }

            return true;
}

