window.addEvent("domready", function(){
				
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("mapContainer"));
		map.addControl(new GLargeMapControl());
		//map.enableScrollWheelZoom();
	}
	
	$("MapSearch_Type_Sale").addEvent("click", toggleSearchType);
	$("MapSearch_Type_Rent").addEvent("click", toggleSearchType);
		
	toggleSearchType();
	doMapSearch(map);
	
	/*
	    //button will submit the MapSearch
	    
	    $("MapSearch_Type_Sale").addEvent("click", function(){doMapSearch(map)});
	    $("MapSearch_Type_Rent").addEvent("click", function(){doMapSearch(map)});
    	
	    $("MapSearch_MinPrice_Sale").addEvent("change", function(){doMapSearch(map)});
	    $("MapSearch_MaxPrice_Sale").addEvent("change", function(){doMapSearch(map)});
    	
	    $("MapSearch_MinPrice_Rent").addEvent("change", function(){doMapSearch(map)});
	    $("MapSearch_MaxPrice_Rent").addEvent("change", function(){doMapSearch(map)});
    	
	    $("MapSearch_HouseholdIncome").addEvent("change", function(){doMapSearch(map)});
	    $("MapSearch_MinBedrooms").addEvent("change", function(){doMapSearch(map)});
    	
	    $("MapSearch_IsNew").addEvent("click", function(){doMapSearch(map)});
	    $("MapSearch_IsResale").addEvent("click", function() { doMapSearch(map) });
	
	*/
	
	$("Do_Map_Search").addEvent("click", function(e) { 
	    new Event(e).stop();	   
	    doMapSearch(map); 	
	});
	
	
});

function addSearchMapProperty(map, property){
	
	var propPoint = new GLatLng(property.Lat, property.Lng);					
	var marker = new GMarker(propPoint)
	map.addOverlay(marker);
	
	var infoWindowHTML = new Element("div", {
		"id" : "infoWindowHTML_" + property.PropertyID,
		"styles" : {
			"display" : "none"
		}
	});
	
	infoWindowHTML.inject($("propertyInfoWindows"));
	
	var mapPropertyContents = new Element("div", {
		"class" : "mapPropertyContents"
	});
	
	mapPropertyContents.inject(infoWindowHTML);
	
	// Add the property name link
	new Element("a", { "href" : "/sales-and-rentals/our-properties/" + property.PropertyID + "/" + property.URI + "/", "html" : property.Name }).inject(
		new Element("h2").inject(mapPropertyContents)
	);
						
	// Add the property address
	new Element("p", { "html" : property.Address }).inject(mapPropertyContents);

	//Info
	var paragraphInfo = new Element("p", { "html": "For Sale " }).inject(mapPropertyContents);
	
	// Add the property status and price
	new Element("span", { "html": formatCurrency(property.SearchPrice) }).inject(paragraphInfo);

	//Contact Form
	new Element("span", { "class": "contact", "html": "<a href='" + property.SendToFormUri + "'>Contact Us</a>" }).inject(paragraphInfo)
	
	// Add a clear div
	new Element("div", { "class" : "clear" }).inject(mapPropertyContents);
	
	// Add the property description container
	var propertyDesc = new Element("div", { "class" : "propertydescription" }).inject(mapPropertyContents);
	
	// Add the thumbnail image
	if (property.ImageURI) {
		new Element("img", { "class" : "thumbnail", "alt" : property.ImageDescription, "src" : property.ImageURI }).inject(
			new Element("a", { "href" : "/sales-and-rentals/our-properties/" + property.PropertyID + "/" + property.URI + "/" }).inject(propertyDesc)
		);
	}
	
	// Add the property description
	new Element("p", { "html": property.ShortDesc }).inject(propertyDesc);

		
	// Link the marker to the info window
	GEvent.addListener(marker, 'click', function() {  
		marker.openInfoWindowHtml($("infoWindowHTML_" + property.PropertyID).get("html")); 
	});
	
}

function formatCurrency(strValue)
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	dblValue = Math.floor(dblValue/100).toString();
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + '£' + dblValue);
}

function setSearchMapCenter(map, minLat, maxLat, minLng, maxLng){
	
	// Define the two corners of the bounding box  
	var sw = new GLatLng(minLat.toFloat() - 0.01, minLng.toFloat() - 0.01);  
	var ne = new GLatLng(maxLat.toFloat() + 0.01, maxLng.toFloat() + 0.01);  
	   
	// Create a bounding box  
	var bounds = new GLatLngBounds(sw, ne);  
	
	// Center map in the center of the bounding box  
	// and calculate the appropriate zoom level  
	map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
	
}

function toggleSearchType(){
	
	if ($("MapSearch_Type_Sale").checked){
	
		$("forsale_prices").setStyle("display", "block");
		$("MapSearch_NewResale").setStyle("display", "block");
		
		$("MapSearch_MinPrice_Rent").set("value", "");
		$("MapSearch_MaxPrice_Rent").set("value", "");
		
	} else {
	
		$("forsale_prices").setStyle("display", "none");
		$("MapSearch_NewResale").setStyle("display", "none");
		
	}
	
	if ($("MapSearch_Type_Rent").checked){
	
		$("torent_prices").setStyle("display", "block");
		
		$("MapSearch_MinPrice_Sale").set("value", "");
		$("MapSearch_MaxPrice_Sale").set("value", "");
		
		$("MapSearch_IsNew").set("checked", false);
		$("MapSearch_IsResale").set("checked", false);
		
	} else {
		$("torent_prices").setStyle("display", "none");
	}
	
}


function doMapSearch(map){

    var jsonRequest = new Request.JSON(
		{
		    url: "/properties/map-search-ajax/",
		    onComplete: function(data) {

		        $("propertyInfoWindows").empty();
		        map.clearOverlays();

		        if (data==null) {
		            $("mapSearchError").setStyle("display", "block");
		        } else {
		            if (data.Properties.length == 0) {
		                $("mapSearchError").setStyle("display", "block");
		            } else {

		                $("mapSearchError").setStyle("display", "none");

		                setSearchMapCenter(
						    map,
						    data.BoundingBox.MinLat,
						    data.BoundingBox.MaxLat,
						    data.BoundingBox.MinLng,
						    data.BoundingBox.MaxLng
					    );

		                data.Properties.each(function(property) {
		                    addSearchMapProperty(map, property);
		                });
		            }
		        }
		    }
		}
	).post($("mapFilter"));

	
}

