// this variable will collect the html which will eventually be placed in the side_bar
var side_bar_html = '';
var map;


// arrays to hold copies of the markers and html used by the side_bar because the function closure trick doesnt work there
var gmarkers = [];
var i = 0;

if (google.search) var localSearch = new GlocalSearch();
var ajSearchTerm;
// Create a base icon for all of our markers that specifies the shadow, icon dimensions, etc.
var baseIcon = new GIcon();
baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
baseIcon.iconSize = new GSize(20, 34);
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(9, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);
baseIcon.infoShadowAnchor = new GPoint(18, 25);

// A function to create the marker and set up the event window
// Each instance of the function preserves the contents of a different instance
// of the 'marker' and 'html' variables which will be needed later when the event triggers.
function createMarker(point,html,sbText, toolTip, markerID) {

	if (markerID){
		// Create a lettered icon for this point using our icon class
		//var letter = String.fromCharCode("A".charCodeAt(0) - 1  + indexNum);
	     var letteredIcon = new GIcon(baseIcon);
	     letteredIcon.image = "http://maps.google.com/mapfiles/marker" + markerID + ".png";
		//alert("http://www.google.com/mapfiles/marker" + letter + ".png");
	     markerOptions = { icon:letteredIcon,title:toolTip };
	}
	else markerOptions = { title:toolTip };
	var marker = new GMarker(point,markerOptions );

	GEvent.addListener(marker, 'click', function() {marker.openInfoWindowHtml(html);});
    // save the info we need to use later for the side_bar
    gmarkers[i] = marker;
    // add a line to the side_bar html
    if (sbText) side_bar_html += '<a href=\"javascript:myclick(' + i + ')\">' + sbText + '</a><br>';
    i++;
	return marker;
}
 // =========================================================================
var d2r = Math.PI / 180;   // degrees to radians
var r2d = 180 / Math.PI;   // radians to degrees
var earthsradius = 3963; // 3963 is the radius of the earth in miles

// =========================================================================
function drawCircle(lat, lon, radius, nodes, liColor, liWidth, liOpa, fillColor, fillOpa){
// Esa 2006
	var center = map.getCenter();
	//calculating km/degree
	var latConv = center.distanceFrom(new GLatLng(center.lat()+0.1, center.lng()))/100;
	var lngConv = center.distanceFrom(new GLatLng(center.lat(), center.lng()+0.1))/100;

	//Loop
	var points = [];
	var step = parseInt(360/nodes)||10;
	for(var i=0; i<=360; i+=step)
	{
	var pint = new GLatLng(center.lat() + (radius/latConv * Math.cos(i * Math.PI/180)), center.lng() +
	(radius/lngConv * Math.sin(i * Math.PI/180)));
	points.push(pint);
	//bounds.extend(pint); //this is for fit function
	}
	fillColor = fillColor||liColor||"#0055ff";
	liWidth = liWidth||2;
	fillOpa    = fillOpa||.1;
	var poly = new GPolygon(points,liColor,liWidth,liOpa,fillColor,fillOpa);
	map.addOverlay(poly);
}
// ===============================================================================
// http://www.tomanthony.co.uk/blog/geocoding-uk-postcodes-with-google-map-api/
function ajPointFromPostcode(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!");
			}
		});
	
     ajSearchTerm = postcode + ", UK";
	localSearch.execute(ajSearchTerm);
}

function ajMarkerAtPoint(point)
{
  	//alert("Latitude: " + point.lat() + "\nLongitude: " + point.lng());
	var marker = new GMarker(point,icon);
	map.addOverlay(marker);

}
function ajCenterToPoint(point)
{
  	var html= "PostCode: " + ajSearchTerm + "<br>Latitude: " + point.lat() + "<br>Longitude: " + point.lng();
	var sbText= '';
	var toolTip= "PostCode: " + ajSearchTerm  ;
	var markerID='A';
	var marker = createMarker(point,html, sbText, toolTip, markerID);
	map.addOverlay(marker);
	map.setCenter(point, 17);
}

function ajAlertInfo(point)
{
	alert("PostCode: " + ajSearchTerm + "\nLatitude: " + point.lat() + "\nLongitude: " + point.lng());
}


// http://www.tomanthony.co.uk/blog/geocoding-uk-postcodes-with-google-map-api/
// ===============================================================================

// =========================================================================
// This function picks up the click and opens the corresponding info window
function myclick(i) { GEvent.trigger(gmarkers[i], 'click');}
			
