

window.addEvent("domready",function () 
{
	if (!$("search_postcode")) return;

	$("search_postcode").addEvent("click",function () 
	{
		alert("Please wait until the page has finished loading!");		
	});

	$("search_postcode_text").addEvent("keypress",function (e) 
	{
		if (e.code == 13)
		{
			e.stop();
			$("search_postcode").fireEvent("click");
		}
	});

});

window.addEvent("load",function () 
{
	if (!$("search_postcode")) return;

	var host = "/";
	if (String(window.location.host).match(/desktopxp|localhost/i)) host+= "www.centriforce.com/";

	var post_url = host + "stockists.php";

	// function to reset loading
		var resetLoading = function (reset_text)
		{
			$("search_postcode").setStyle("display", "");
			$("search_map_loading").setStyle("display", "none");

			if (reset_text) $("search_postcode_text").value = "";
		}

	// create ajax page
		var google_postcode_search = new Request({
			url: post_url,
			method: 'post',
			onSuccess: function(response_text)
			{
				if (!GOOGLE_MAP_LOADED)
					alert("Google Map is not loaded!");
				else if (!response_text || !response_text.match(/,/))
					alert("Nothing received from search!");
				else
				{
					var coords = response_text.split(/,/);
					var lat = coords[0];
					var lng = coords[1];

					// create position
						var position = new GLatLng(lat, lng);

					// remove marker
						if (search_marker) google_map.removeOverlay(search_marker);

					// (re)create marker
						var marker_image = new GIcon(G_DEFAULT_ICON);
						marker_image.image = host + "images/marker-green-semi.png";
						marker_image.iconSize = new GSize(19, 32);
						//marker_image.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/green-dot.png";

						var markerOptions = { icon : marker_image, title : $("search_postcode_text").value, clickable : true };

						search_marker = new GMarker(position, markerOptions);
						google_map.addOverlay(search_marker);

					// go through markers and get smallest distances
						var nearest = [-1, -1];
						var nearest_index = [-1, -1];

						if (google_map_markers.length)
						{
							// find two nearest
								for (var i = 0; i < google_map_markers.length; i++)
								{
									var marker = google_map_markers[i];
									var distance = parseFloat(marker.getPoint().distanceFrom(position));
									
									if (nearest[0] < 0)
									{
										nearest[0] = distance;
										nearest_index[0] = i;
									}
									else if (nearest[1] < 0)
									{
										nearest[1] = distance;
										nearest_index[1] = i;
									}
									else if (distance < parseFloat(Math.max(nearest[0],nearest[1])))
									{
										// if 0 is smaller than 1, replace 1
											var k = (nearest[0] < nearest[1]) ? 1 : 0;

										// replace
											nearest[k] = distance;
											nearest_index[k] = i;
									}
								}

							// put nearest in order
								if (nearest[1] > -1 && nearest[1] < nearest[0])
								{
									nearest = [nearest[1], nearest[0]];
									nearest_index = [nearest_index[1], nearest_index[0]];
								}

							

							// create new bounds box containing the two nearest and the current point
								search_bounds = new GLatLngBounds();
								search_bounds.extend(position);

								for (var i = 0; i < nearest_index.length; i++)
								{
									// if not set, quit
										if (nearest_index[i] < 0) break;

									// if second one is miles away, don't bother
										if (i && nearest[i] > 250000) break;

									search_bounds.extend(google_map_markers[nearest_index[i]].getPoint());															
								}

							// move map and zoom according to new bounding box
								var zoom = google_map.getBoundsZoomLevel(search_bounds);
								/*zoom--;
								if (zoom < 5) zoom = 5;*/
								google_map.setZoom(zoom);

							// determine the centre from the bounds
								google_map.setCenter(search_bounds.getCenter());
						}
				}

				resetLoading(true);
			},
			onFailure: function () {
				alert("Couldn't filter results - AJAX error!");
				resetLoading();
			},
			onException: function (headerName, value) {
				alert("Exception - " + headerName + ' - ' + value);
				resetLoading();
			}
		});



	$("search_postcode").removeEvents("click");

	$("search_postcode").addEvent("click",function () 
	{
		// check we have a value for the postcode
			if (!$("search_postcode_text").value.trim()) return;

		google_postcode_search.send("search_postcode=1&search_postcode_text=" + encodeURI($("search_postcode_text").value));
		this.setStyle("display", "none");
		$("search_map_loading").setStyle("display", "");	
	});

});

