	 $(document).ready(init);
	 
	 function init() { 
		$(".addthis_container").css("display", "none");
		$("#container").css("padding-bottom", "0px");
		$(".control_sub_box fieldset").simpletip({content:'e.g.: "Hull" or "SW19" or "Finchley Road, NW3"', position: 'right', offset: [50, 0] }); 

		$("#update").click(update);
		$("#slider-price-range").slider({
			range: true,
			min: 0,
			max: 1010000,
			values: [0, 1010000],
			step:10000,
			slide: function(event, ui) {
				if ((ui.values[0] == 0) && (ui.values[1] == 1010000)) {
					$("#pricerangeamount").val('any');
				} else if ((ui.values[0] == 0) && (ui.values[1] < 1010000)) {
					$("#pricerangeamount").val('upto \u00A3' + addCommas(ui.values[1]));
				} else if ((ui.values[0] > 0) && (ui.values[1] == 1010000)) {
					$("#pricerangeamount").val('more than \u00A3' + addCommas(ui.values[0]));
				} else if ((ui.values[0] > 0) && (ui.values[1] < 1010000)) {
					$("#pricerangeamount").val('\u00A3' + addCommas(ui.values[0]) + ' to \u00A3' + addCommas(ui.values[1]));
				}
			},
			change: update
		});
		$("#pricerangeamount").val("any");
		$("#slider-bedrooms").slider({
			range: true,
			min: 1,
			max: 6,
			values: [1, 6],
			step:1,
			slide: function(event, ui) {
				if (ui.values[0] == ui.values[1]) {
					$("#bedroomsamount").val(ui.values[0]);
				} else {
					$("#bedroomsamount").val(ui.values[0] + ' - ' + ui.values[1]);
				}
			},
			change: update
		});
		$("#bedroomsamount").val($("#slider-bedrooms").slider("values", 0) + ' - ' + $("#slider-bedrooms").slider("values", 1));
		$("#property-sort").change(update);
		$("#property-type").change(update);
		$("#location").click(function() {if ($("#location").val() == "Enter location") {$("#location").val("");} });
		$("#location").keypress(function(event) {
			// bind hitting return to update the results, not submit the form
			if (event.keyCode == '13') {
				event.preventDefault();
				update();
			}
		});
		//update();
		if (GBrowserIsCompatible()) {
			var map = new GMap2(document.getElementById("map"));
			map.setCenter(new GLatLng(54.76267,-4.306641));
			var mapControl = new GMapTypeControl();
			map.addControl(mapControl);
			map.addControl(new GLargeMapControl());
			map.setZoom(5);
		}
		
		
	 };

	function changePage(page) {
		$("#page").val(page);
		update();
		$("#page").val(1);
	}

	function changeLocation(location) {
		$("#location").val(location);
		update();
	}
		
	function update() {
		$("#debug").text();
		var page = $("#page").val();
		var location = $('#location').val();
		var pricelow = $("#slider-price-range").slider("values", 0);
		var pricehigh = $("#slider-price-range").slider("values", 1);
		var bedroomlow = $("#slider-bedrooms").slider("values", 0);
		var bedroomhigh = $("#slider-bedrooms").slider("values", 1);
		var sort = $("#property-sort").val();
		var type = $("#property-type").val();
		if (location != "Enter location") {
			$("#property_list").html("<img style='display:block; margin:200px auto 25px;' src='/images/update.gif' alt='Updating' /><p style='text-align:center;padding:0; margin:0;'>Updating Results...</p>");
			$.get("/script.php", {"page": page, "location": location, "pricelow": pricelow, "pricehigh": pricehigh, "bedroomlow": bedroomlow, "bedroomhigh": bedroomhigh, "sort": sort, "type": type}, updateResults);
		}
	}
	   
	function updateResults(data, textStatus) {
		$("#results_and_map").html(data);
	}
	   
	function addCommas(nStr) {
		nStr += '';
		x = nStr.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	}

/* Google Maps functions */
	function addClickevent(marker) { // Add a click listener to the markers

		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(marker.content);
		});
		return marker;
	}

	// Zooms the map to the best-fitting view of the markers on the map
	function fitMap( map, points ) {
		var bounds = new GLatLngBounds();
		for (var i=0; i< points.length; i++) {
			bounds.extend(points[i]);
		}
		map.setZoom(map.getBoundsZoomLevel(bounds));
		map.setCenter(bounds.getCenter());
	}

	// Creates a marker pin for each property, along with a balloon & brief text.
	function createMarker(point, index, text) {
		// Create a lettered icon for this point using our icon class
		var letter = String.fromCharCode("A".charCodeAt(0) + index);
		var letteredIcon = new GIcon(baseIcon);
		letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
		
		// Set up our GMarkerOptions object
		markerOptions = { icon:letteredIcon };
		// Store data attributes as property of gmarkers
		var html ="<div class='infowindow'><strong>"+ text + "<\/strong><\/div>";
		var marker = new GMarker(point, markerOptions);
		marker.content = html;

		addClickevent(marker);
		
		return marker;
	}
