// Google Maps and AJAX API stuff

var biz_box_id = null;

var map;
var localSearch = new GlocalSearch();

var icon = new GIcon();
icon.image = "http://www.google.com/mapfiles/marker.png";
icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
icon.iconSize = new GSize(20, 34);
icon.shadowSize = new GSize(37, 34);
icon.iconAnchor = new GPoint(10, 34);

function ExpandBizBox(id)
{
	$(".biz_companybox_bottom").hide();
	if ($("#bottom_bizbox_" + id).css("display") == "none")
	{
		$("#bottom_bizbox_" + id).fadeIn("slow");
		var postcode = $("#bizbox_" + id + " span.postcode").html();
		biz_box_id = "bizbox_map_" + id;
		usePointFromPostcode(postcode, mapLoad);
	}
	else
	{
		$("#bottom_bizbox_" + id).hide();
	}
}

function usePointFromPostcode(postcode, callbackFunction) {
	
	localSearch.setSearchCompleteCallback(null, 
		function() {
			
			if (localSearch.results[0])
			{		
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				var point = new GLatLng(resultLat,resultLng);
				callbackFunction(point);
			}else{
				alert("Postcode not found!");
			}
		});	
		
	localSearch.execute(postcode + ", UK");
}

function placeMarkerAtPoint(point)
{
	var marker = new GMarker(point,icon);
	map.addOverlay(marker);
}

function setCenterToPoint(point)
{
	map.setCenter(point, 17);
}

function showPointLatLng(point)
{
	alert("Latitude: " + point.lat() + "\nLongitude: " + point.lng());
}

function mapLoad(point) {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById(biz_box_id));
		
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		placeMarkerAtPoint(point);
		map.setCenter(point, 15);
	}
}

