function createXMLHttpRequest() {
	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
}

function startMap() {
	if (GBrowserIsCompatible()) {
		var vars = location.pathname.split('/');
		for (var i = 3; i < vars.length; i++) {
			vars2 = vars[i].split('-');
			if (vars2[0] == 'area') { var area = vars2[1]; for (j = 2; j < vars2.length; j++) { area = area + '-' + vars2[j]; } }
			if (vars2[1] == 'bedrooms') var rooms = vars2[0];
			if (vars2[0] == 'type') var type = vars2[1];
			if (vars2[0] == 'price') var price = vars2[1];
			if (vars2[0] == 'seek') var seek = vars2[1];
		}
		var map = new GMap2(document.getElementById("searchmap"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		if (startlat != "") map.setCenter(new GLatLng(startlat, startlng), parseInt(startzoom));
		else map.setCenter(new GLatLng(53.3835331448366, -1.50581359863281), 12);
		
		var baseIcon = new GIcon();
		baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
		baseIcon.iconSize = new GSize(20, 34);
		baseIcon.shadowSize = new GSize(37, 34);
		baseIcon.iconAnchor = new GPoint(9, 34);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);
		baseIcon.infoShadowAnchor = new GPoint(18, 25);
		
		createXMLHttpRequest();
		var address = "/includes/getproperties.php?find=props";
		if (area) address += "&area="+area;
		if (rooms) address += "&rooms="+rooms;
		if (type) address += "&type="+type;
		if (price) address += "&price="+price;
		if (seek) address += "&seek="+seek;
		xmlHttp.open("GET", address, true);
		xmlHttp.onreadystatechange = function () {
			if (xmlHttp.readyState == 4) {
				if (xmlHttp.status == 200) {
					var results = xmlHttp.responseText;
					if (results != '') {
						var resultArray = results.split("[break1]");
						for (var i = 0; i < resultArray.length; i++) {
							var currentArray = resultArray[i].split("[break2]");
							var point = new GLatLng(currentArray[7], currentArray[8]);
							var number = '';
							if (currentArray[3] != 'NULL') number += currentArray[3]+' ';
							if (currentArray[4] != 'NULL') number += currentArray[4]+' ';
							if (currentArray[5] != 'NULL') number += currentArray[5]+' ';
							var bedroom = currentArray[2]+' Bedroom';
							if (currentArray[2] > 1) bedroom += 's';
							map.addOverlay(createMarker(point, number, currentArray[6], currentArray[1], map, bedroom, currentArray[9], currentArray[0], i, baseIcon, currentArray[10], currentArray[11]));
						}
					}
				}
			}
		};
		xmlHttp.send(null);
	}
}

function createMarker(point, number, road, area, map, room, img2, propid, index, baseIcon, price, type) {
	var letter = String.fromCharCode("A".charCodeAt(0) + index);
	var icon = new GIcon(baseIcon);
	icon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
	var marker = new GMarker(point, icon);
	GEvent.addListener(marker, "click", function() {
		var html = '<div class="prop-marker"><div class="marker-details"><h4>'+number+road+', '+area+'</h4><ul><li>'+room+'</li><li>for '+type+'</li><li>&pound;'+price+'/week rent</li><p><a href="/accommodation-services/property-detail/'+propid;
		var vars = location.pathname.split('/');
		for (var i = 3; i < vars.length; i++) {
			vars2 = vars[i].split('-');
			if (vars2[0] == 'area') { var area2 = vars2[1]; for (j = 2; j < vars2.length; j++) { area2 = area2 + '-' + vars2[j]; } html += '/area-' + area2; }
			if (vars2[1] == 'bedrooms') html += '/' + vars2[0] + '-bedrooms';
			if (vars2[0] == 'type') html += '/type-' + vars2[1];
			if (vars2[0] == 'price') html += '/price-' + vars2[1];
			if (vars2[0] == 'seek') html += '/seek-' + vars2[1];
		}
		html += '">View '+number+road+' in detail</a></p></div>';
		if (img2 != 'noimage.jpg') html+= '<img src="/images/properties/small_'+img2+'" alt="photograph of '+number+road+'" title="'+number+road+'" />';
		html += '</div>';
		marker.openInfoWindowHtml(html);
	});
	return marker;
}

addLoadEvent(startMap);