var branches = '';

if (window.XMLHttpRequest) {
	xhttp = new XMLHttpRequest();
}
else { // IE 5/6
	xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

function querySt(ji) {
	hu = window.location.search.substring(1);
	gy = hu.split("&");

	for (i = 0; i < gy.length; ++i) {
		ft = gy[i].split('=');
		if (ft[0] == ji) {
			return ft[1];
		}
	}
}

function initialize() {
	if (GBrowserIsCompatible()) {
		officeMarker = new GIcon();
		officeMarker.image = 'http://www.linleyandsimpson.co.uk/gmaps/ls-office.png';
		officeMarker.iconSize = new GSize(24, 24);
		officeMarker.iconAnchor = new GPoint(15, 15);
		officeMarker.infoWindowAnchor = new GPoint(15, 15);

		propertyMarker = new GIcon();
		propertyMarker.image = 'http://www.linleyandsimpson.co.uk/gmaps/house-single.png';
		propertyMarker.iconSize = new GSize(32, 32);
		propertyMarker.iconAnchor = new GPoint(15, 15);
		propertyMarker.infoWindowAnchor = new GPoint(15, 15);

		clusterMarker = new GIcon();
		clusterMarker.image = 'http://www.linleyandsimpson.co.uk/gmaps/house-cluster.png';
		clusterMarker.iconSize = new GSize(32, 32);
		clusterMarker.iconAnchor = new GPoint(15, 15);
		clusterMarker.infoWindowAnchor = new GPoint(15, 15);

		var styles = [[{
				url: 'http://www.linleyandsimpson.co.uk/gmaps/house-cluster.png',
				height: 32,
				width: 32,
				opt_anchor: [16, 0],
				opt_textColor: '#FF00FF'
			}]];


		var map = new GMap2(document.getElementById('map_canvas'));
		map.addControl(new GLargeMapControl3D());

		// Pin branches
		xhttp.open('GET', '/images/gmaps/branches.xml', false);
		xhttp.send('');
		xml = xhttp.responseXML;

		branches = xml.getElementsByTagName('branch');
		branchcount = branches.length;

		for (var i = 0; i < branchcount; ++i) {
			var name = branches[i].getElementsByTagName('name').item(0).firstChild.nodeValue;
			var address = branches[i].getElementsByTagName('address').item(0).firstChild.nodeValue;
			var tel = branches[i].getElementsByTagName('tel').item(0).firstChild.nodeValue;
			var fax = branches[i].getElementsByTagName('fax').item(0).firstChild.nodeValue;
			var email = branches[i].getElementsByTagName('email').item(0).firstChild.nodeValue;
			var latitude = branches[i].getElementsByTagName('latitude').item(0).firstChild.nodeValue;
			var longitude = branches[i].getElementsByTagName('longitude').item(0).firstChild.nodeValue;

			var openwin = '<span style="font-family: Trebuchet MS; font-size: 12px;"><strong>' + name + '</strong><br>' + address + '<br><strong>Tel:</strong> ' + tel + '<br><strong>Fax:</strong> ' + fax + '<br><strong>Email:</strong> ' + email + '</span>';

			var latlng = new GLatLng(latitude, longitude);
			var marker = createMarker(latlng, {icon:officeMarker}, openwin)
			map.addOverlay(marker);
		}

		// Pin properties
		xhttp.open('GET', '/images/gmaps/gmaps_col.xml', false);
		xhttp.send('');
		xml = xhttp.responseXML;

		properties = xml.getElementsByTagName('property');
		propcount = properties.length;

		var qs_id = querySt('f0') == null ? null : querySt('f0');
		var qs_proptype = querySt('f1') == null ? '' : unescape(querySt('f1'));
		var qs_pricelow = querySt('pricelower') == null ? 0 : parseInt(querySt('pricelower'));
			if (isNaN(qs_pricelow)) { qs_pricelow = 0; }
		var qs_pricehigh = querySt('priceupper') == null ? 20000 : parseInt(querySt('priceupper'));
			if (isNaN(qs_pricehigh)) { qs_pricehigh = 20000; }
		var qs_bedrooms = querySt('f12') == null ? 0 : parseInt(querySt('f12'));
			if (isNaN(qs_bedrooms)) { qs_bedrooms = 0; }
		var qs_furnishing = querySt('f15') == null ? '' : querySt('f15');
		var qs_area = querySt('f24') == null ? 0 : parseInt(querySt('f24'));
			if (isNaN(qs_area)) { qs_area = 0; }
		var qs_selectproperties = parseInt(querySt('f26')); // 0 or 1 (1 = select) - not needed on new search

		switch (qs_area)
		{
			case 1:
				var centre_lat = 53.84333;
				var centre_long = -1.505685;
				var centre_zoom = 13;
				var qs_areaname = 'Roundhay';
				break;
			case 2:
				var centre_lat = 53.846894;
				var centre_long = -1.63192;
				var centre_zoom = 13;
				var qs_areaname = 'Horsforth';
				break;
			case 3:
				var centre_lat = 53.991975;
				var centre_long = -1.538612;
				var centre_zoom = 13;
				var qs_areaname = 'Harrogate';
				break;
			case 4:
				var centre_lat = 53.92949;
				var centre_long = -1.389198;
				var centre_zoom = 13;
				var qs_areaname = 'Wetherby';
				break;
			case 5:
				var centre_lat = 53.794225;
				var centre_long = -1.543257;
				var centre_zoom = 13;
				var qs_areaname = 'Leeds City Centre';
				break;
			case 6:
				var centre_lat = 53.957129;
				var centre_long = -1.086129;
				var centre_zoom = 13;
				var qs_areaname = 'York';
				break;
			case 7:
				var centre_lat = 53.794225;
				var centre_long = -1.543257;
				var centre_zoom = 9;
				var qs_areaname = 'All Leeds Areas';
				break;
			case 8:
				var centre_lat = 53.684283;
				var centre_long = -1.499101;
				var centre_zoom = 11;
				var qs_areaname = 'Wakefield';
				break;
			case 9:
				var centre_lat = 53.925726;
				var centre_long = -1.822998;
				var centre_zoom = 13;
				var qs_areaname = 'Ilkley';
				break;
			default:
				var centre_lat = 53.794225;
				var centre_long = -1.543257;
				var centre_zoom = 9;
				qs_area = 0;
				var qs_areaname = 'All Areas';
				break;
		}
		map.setCenter(new GLatLng(centre_lat, centre_long), centre_zoom);

		var pushcount = 0;
		var markers = [];
		var badlatlong = 0;

		for (var i = 0; i < propcount; ++i) {
			var propid = properties[i].getAttribute('id');
			var latitude = properties[i].getAttribute('lat');
			var longitude = properties[i].getAttribute('long');
			var infowin = properties[i].getAttribute('infowin');
			var beds = properties[i].getAttribute('beds');
			var price = parseInt(properties[i].getAttribute('price'));
			var ptype = properties[i].getAttribute('type');
			var area = properties[i].getAttribute('area');
			var furnished = properties[i].getAttribute('furnished');
			var select = properties[i].getAttribute('select');
			var pushme = true;

			if (qs_id != null) {
				if (propid == qs_id) {
					if (latitude == '' || longitude == '') {
						document.getElementById('map_canvas').innerHTML = '<img src="http://www.linleyandsimpson.co.uk/newbuild/images/placeholders/propertylocation.jpg" width="360" height="264" alt="Sorry, map unavailable"/>';
					}
					else {
						var openwin = '<span style="font-family: Trebuchet MS; font-size: 14px;">' + infowin + '</span><br/><br/><span style="color: #005299; font-weight: bold; font-family: Trebuchet MS; font-size: 12px;">' + beds + ' bedroom<br/>&pound;' + price + ' per month</span>';
	  				var latlng = new GLatLng(latitude, longitude);
	  				var marker = createMarker(latlng, {icon: propertyMarker}, openwin);
	  				markers.push(marker);
						map.setCenter(new GLatLng(latitude, longitude), 16);
						break;
					}
				}
	  	}
	  	else {
		  	if (latitude == '' || longitude == '') { pushme = false; ++badlatlong; }

	  		if (price >= qs_pricelow && price <= qs_pricehigh) {} else { pushme = false; }

		  	if (beds >= qs_bedrooms) {} else { pushme = false; }

		  	if (qs_proptype == '' || ptype == qs_proptype) {} else { pushme = false; }

	  		if (qs_furnishing == '' || furnished == qs_furnishing) {} else { pushme = false; }

				if (qs_area == 0 || area.indexOf(qs_area) != -1) {} else { pushme = false; }

	  		if (pushme) {
		  		++pushcount;
	  			var openwin = '<span style="font-family: Trebuchet MS; font-size: 14px;">' + infowin + '</span><br/><br/><span style="color: #005299; font-weight: bold; font-family: Trebuchet MS; font-size: 12px;">' + beds + ' bedroom<br/>&pound;' + price + ' per month</span>';
	  			var latlng = new GLatLng(latitude, longitude);
	  			var marker = createMarker(latlng, {icon: propertyMarker}, openwin);
	  			markers.push(marker);
	  		}
	  	}
		}

		var markerCluster = new MarkerClusterer(map, markers, {gridSize: 30, styles: styles[0]});

		var outputHTML = '<p>You searched for: ';
		if (qs_proptype == '') { outputHTML += 'Properties'; } else { outputHTML += qs_proptype; }
		outputHTML += ' with ' + qs_bedrooms + '+ beds between &pound;';
		outputHTML += qs_pricelow + ' and &pound;' + qs_pricehigh + ' ';
		outputHTML += qs_furnishing + ' in ' + qs_areaname + '</p><p><strong>';
		outputHTML += pushcount + ' properties were found.</strong></p>';

		if (document.getElementById('resultcountTop')) {
			document.getElementById('resultcountTop').innerHTML = outputHTML + '<hr>';
		}
		if (document.getElementById('resultcountBottom')) {
			document.getElementById('resultcountBottom').innerHTML = '<hr style="margin-top: 10px;">' + outputHTML + '<hr style="margin-top: 10px;">';
		}
	}
}

function createMarker(point, icon, openwin) {
	var marker = new GMarker(point, icon);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml('<div class="openwin" style="width: 250px;">' + openwin + '</div>');
	});
	return marker;
}
