

var map;
var smap, lmap, cmap;
var mymarkers = [];
var myicons = [];
var myextras = [];
var localSearch = new GlocalSearch();

function getpostcode( postcode, callbackFunction, extra )
{	
	// The call back function can only use the search results as parameters.
	// Adding other parameters in there only works when there is one search 
	// awaiting completion. If there is more than one the parameters from the
	// last search get used for all of them.  So we use a global for the extra
	// data.
	myextras[postcode] = extra;
	
	localSearch.setSearchCompleteCallback
	(
	  	null, 
   		function()
   		{
      			if (localSearch.results[0])
      			{    
        			var resultLat = localSearch.results[0].lat;
        			var resultLng = localSearch.results[0].lng;
        			var pc = localSearch.results[0].title;
        			
        			// Google search has started returning the post town in the 'title' in June 2007, so...
        			pc = pc.replace(/.*,\s/,'');
        			// By August 2007 Google had moved the goalposts again as the comma wasn't there
        			// and it had 'Greater London' instead of the post town. Fortunately it only really matters
        			// to the Street Gazetteer that we can reduce this to the post code.
        			pc = pc.replace(/.*don\s/i,'');
        			// Checking in May 2008 revealed that now 'SURREY' was now appearing in results, so...
        			pc = pc.replace(/Surrey\s/i,'');
				
        			callbackFunction( resultLat, resultLng, pc );
      			}
      			else
      			{
        			alert( 'The postcode '+postcode+' has not been recognised.' );
      			}
		 }
   	);  
    
	localSearch.execute( postcode + ", UK" );
}


function createMarker(name, point, label, colour) 
{
	var tooltip = label.replace(/<strong>(.*)<\/strong>.*/, "$1");
	tooltip = tooltip.replace(/<h3>(.*)<\/h3>.*/, "$1");
	
	var marker = new GMarker( point, { icon:myicons[colour], title:tooltip } );
			
	GEvent.addListener
	(
		marker, 
		"click", 
		function() { marker.openInfoWindowHtml(label); }
	);
 
 	mymarkers[name] = marker;
 			
 	return marker;
}	


function myclick(name)
{
	if ( mymarkers[name] )
	{
		GEvent.trigger( mymarkers[name], "click" );
		
		return true;
	}
	else
	{
		alert( 'Please try again when the Google map has finished loading.' );
		
		return false;
	}
}
	
	
function load() 
{
	if ( GBrowserIsCompatible() )
	{
		map = new GMap2(document.getElementById('map'), 10);
		
		smap = new GSmallMapControl();
		lmap = new GLargeMapControl();
		cmap = 0;
		
		map.addControl( smap );
		map.addControl( new GMapTypeControl() );
		map.setCenter( new GLatLng( 51.3815, -0.0770 ), 15, G_HYBRID_MAP);
		
		// Handle location of markers folder based on URL, both locally and live.
		var markersfolder = self.location.href.replace(/addiscombe.org.uk\/.*/, 'addiscombe.org.uk/images/markers/');
		markersfolder = markersfolder.replace(/addiscombe\/.*/, 'addiscombe/images/markers/');
		
      		myicons["red"] = new GIcon(G_DEFAULT_ICON, markersfolder+"colour101.png");
        	myicons["orange"] = new GIcon(G_DEFAULT_ICON, markersfolder+"colour111.png");
        	myicons["green"] = new GIcon(G_DEFAULT_ICON, markersfolder+"colour041.png");
         	myicons["blue"] = new GIcon(G_DEFAULT_ICON, markersfolder+"colour004.png");
        	myicons["black"] = new GIcon(G_DEFAULT_ICON, markersfolder+"colour001.png");
         	myicons["purple"] = new GIcon(G_DEFAULT_ICON, markersfolder+"colour055.png");
     		    					
		myinit();
	}
}
						

function showpostcode( lat, lng, postcode )
{
	        			
       var point = new GLatLng( lat, lng );
       			
	map.addOverlay( createMarker( postcode.toUpperCase(), point, "<h3>"+postcode.toUpperCase()+"<\/h3><a href=\"http://www.streetmap.co.uk/streetmap.dll?postcode2map?"+postcode.replace(/ /, '+')+"&amp;title="+postcode.replace(/ /, '+')+"&amp;nolocal=Y\">Show on Streetmap.co.uk<\/a><br><strong>Lat:<\/strong> "+lat+" <strong>Long:<\/strong> "+lng, "blue" ) );
}

function showstreet( lat, lng, postcode )
{
       var point = new GLatLng( lat, lng );
       
       var street = myextras[postcode];
       
	map.addOverlay( createMarker( street, point, "<h3>"+street+" ("+postcode.toUpperCase()+")<\/h3><a href=\"http://www.streetmap.co.uk/streetmap.dll?postcode2map?"+postcode.replace(/ /, '+')+"&amp;title="+postcode.replace(/ /, '+')+"&amp;nolocal=Y\">Show on Streetmap.co.uk<\/a><br><strong>Lat:<\/strong> "+lat+" <strong>Long:<\/strong> "+lng, "purple" ) );
}

function setcentre( lat, lng )
{
       var point = new GLatLng( lat, lng );

	map.setCenter( point );
}

function togglezoomcontrol()
{
	cmap = cmap ^ 1;
	
	if ( cmap == 1 )
	{
		map.removeControl(smap);
		map.addControl(lmap);
	}
	else
	{
		map.removeControl(lmap);
		map.addControl(smap);
	}
}
		
		  				
window.onload = load;
window.onunload = GUnload;
 