	var map = null;

	function InitEvents(is_small)
	{
		if($("#mapsmall").length == 0 || map != null)
			return;

		if(GBrowserIsCompatible())
		{
			map = new GMap2(document.getElementById("mapsmall"));
			if (is_small != null)
				map.addControl(new GSmallMapControl());
			else
			{
				map.addControl(new GLargeMapControl());
				map.addControl(new GScaleControl());
				map.addControl(new GMapTypeControl());
			}
			map.setCenter(new GLatLng(51.5080, -0.1248), 15);
			var p_latt = parseFloat(place_latitude);
			var p_long = parseFloat(place_longitude);

			if(p_latt == 0 || p_long == 0 || isNaN(p_latt))
			{
				FetchCoordsByAddress(place_city+', '+place_country, 11);
			}
			else
				UpdateCoordinatesReview(p_long, p_latt, "");
		}
	}
	var global_counter = 0;
	var global_counter_received = 0;
	function FetchCoordsByAddress(place_address, zoomlevel, evalfunc, x, y)
	{
		if (map == null && $("#mapsmall").length > 0 && GBrowserIsCompatible())
		{
			map = new GMap2(document.getElementById("mapsmall"));
			map.addControl(new GSmallZoomControl());
			map.setCenter(new GLatLng(51.5080, -0.1248), 15);
		}
		var current_zoomlevel = zoomlevel;
		var geocoder = new GClientGeocoder();
		var increment = global_counter;
		global_counter += 1;
		geocoder.getLatLng(
				place_address,
				function(point){
					if(increment < global_counter_received)
						return;
					global_counter_received = increment;
				   	if(point){
						var p_latt = parseFloat(point.x);
						var p_long = parseFloat(point.y);
						if (x != null && y != null)
						{
							$('#'+x).val(p_latt);
							$('#'+y).val(p_long);
						}
						UpdateCoordinatesReview(p_latt, p_long, "", current_zoomlevel);
					}
					else if (evalfunc != null)
					{
						eval(evalfunc);
					}
				}
			);
	}

	function UpdateCoordinatesReview(x, y, entity, zoom)
	{
		if (zoom == null)
			zoom = 15;
		map.setCenter(new GLatLng(y, x), zoom);
		var point = new GLatLng(y, x);
		map.clearOverlays();
		if (zoom == 15)
		map.addOverlay(new GMarker(point));

		//$("enlarge_link").href = "http://maps.google.co.uk/maps?ll=" + y + "," + x +
		//	"&q=" + escape("{data.postcode}");
	}

	function FindCoordinatesReview(city, postcode)
	{
		gLocLooking	= "";
		gLocError	= "";

		gLocFunc = "UpdateCoordinatesReview";

		var addr = new atAddress("", city, "", postcode, "UK");
		var licensekey = 'js5417-2861-8807'; // old one - 4184-7919-7574
		var recordcount = 1;
		var threshold = 0.85;
		var coordsys = 1;

		searchFormattedAddress(addr, licensekey, recordcount, threshold, coordsys);
	}

	function CreateMarker(point, type, place, index)
	{
		var marker;
		if(type == 0)
		{
			if(index == "")
				marker = new GMarker(point);
			else
			{
				var icon = new GIcon();
				var letter = String.fromCharCode("A".charCodeAt(0) + index - 1);
				icon.image = "http://www.google.com/mapfiles/marker" + letter + ".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(9, 34);
				icon.infoWindowAnchor = new GPoint(9, 2);
				icon.infoShadowAnchor = new GPoint(18, 25);

				marker = new GMarker(point, icon);
			}
		}
		else
		{
			var icon = new GIcon();
			icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
			icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
			icon.iconSize = new GSize(12, 20);
			icon.shadowSize = new GSize(22, 20);
			icon.iconAnchor = new GPoint(6, 20);
			icon.infoWindowAnchor = new GPoint(5, 1);

			marker = new GMarker(point, icon);
		}

		GEvent.addListener(marker, "click",
		function()
		{
			map.panTo(marker.getPoint());
			$("#place_link").html('<span style="font-size:16px">' + place[0] + '</span>');
			$("#place_link").attr('href', place[1]);
			$("#place_link2").attr('href', place[1]);
			$("#rating_li").width(place[2] * 11);
			$("#info_wnd").width(Math.max(130, place[0].length * 9 + 30));
			if(place[3] != "")
			{
				$("#place_img").attr('src', place[3]);
				$("#place_img").show();
			}
			else
				$("#place_img").hide();

			$("#user_block").hide();
			$("#info_wnd").show();
		}
		);

		return marker;
	}

	function SetMapCenter(x, y, pos)
	{
		map.setCenter(new GLatLng(y, x));

		// Check if we need to scroll into view
		if($("mapsmall").scrollIntoView)
		{
			var top = (document.documentElement.scrollTop ?
				document.documentElement.scrollTop :
				document.body.scrollTop);

//			if(top > 200)
				$("mapsmall").scrollIntoView(true);
		}
		map.addOverlay(gMarkersArray[pos]);
	}
	var google_directions;
	function GoDirection()
	{
		$('#divDirections').html('');
		$('#spanBigMapError').html('');
		uk_postcode = /^([A-Z]{1,2}[0-9]{1,2}[A-Z]{0,1})[\s]{0,1}([0-9][A-Z]{2})?/i;
		google_directions = new GDirections(map, document.getElementById("divDirections"));
		google_directions.clear();
		map.clearOverlays();

		if($('#dir_start').val().match(uk_postcode) && $('#dir_start').val().indexOf('UK') == -1)
			$('#dir_start').val($('#dir_start').val() + ', UK');

		google_directions.load('from: ' + $('#dir_start').val() + ' to: ' + place_latitude + ', ' + place_longitude);
		updatePrintBigMap($('#dir_start').val(), place_latitude + ', ' + place_longitude);
		setTimeout('CheckDirectionStatus()', 3000);
		 return false;
	}

	function CheckDirectionStatus()
	{
		var text_status = GetDirectionStatus();
		if (text_status.trim() != '')
			$('#spanBigMapError').html('Error: ' + text_status);
	}

	function GetDirectionStatus()
	{
		var google_dir_status = google_directions.getStatus().code;
		if (google_dir_status == 601)
			return " empty address";
		else if (google_dir_status == 603 || google_dir_status == 602)
			return " no address was found, try to specify city or country"
		else if (google_dir_status == 604)
			return " no driving directions were found";
		else if (google_dir_status != 200)
			return " no data was fetched";
		else
			return '';
	}

	function updatePrintBigMap(saddr, daddr)
	{
		$('#hrefPrintBigMap').show();
		$('#hrefPrintBigMap a').attr('href', 'http://maps.google.com/maps?f=d&hl=en&geocode=&saddr='+saddr+'&daddr='+daddr+'&pw=2');
	}

	function initPlaces(bigcontrol)
	{
		map = new GMap2(document.getElementById("mapsmall"));
		map.setCenter(new GLatLng(0, 0), 0);
		map.clearOverlays();
		if (bigcontrol != null)
			map.addControl(new GSmallMapControl());
		else
		{
		map.addControl(new GLargeMapControl());
		map.addControl(new GScaleControl());
		map.addControl(new GMapTypeControl());
		}

		var point;
		var bounds = new GLatLngBounds();
		var has_coords = false;
		for(i = 0; i < geos.length - 1; i++)
		{
			if(geos[i][0] * geos[i][1] == 0)
				continue;
			has_coords = true;
			point = new GLatLng(geos[i][1], geos[i][0]);
			bounds.extend(point);
			gMarkersArray[i] = CreateMarker(point, 0,
				[ geos[i][2], geos[i][3], geos[i][4], geos[i][6] ],
				i + 1);
			map.addOverlay(gMarkersArray[i]);
		}
		if (has_coords)
		{
			map.setZoom(map.getBoundsZoomLevel(bounds));
			map.setCenter(bounds.getCenter());
		}
		else
		{
			var geocoder = new GClientGeocoder();
			geocoder.getLatLng(
					StateJSON.city +', '+ StateJSON.country,
					function(point){
					   	if(point){
							map.setCenter(new GLatLng(parseFloat(point.y), parseFloat(point.x)), 11);
						}
					}
				);
		}
	}
