
// A few global variables
var map; var dump = []; var gicons = [];


window.onload = function()
{
	if (GBrowserIsCompatible() )
	{
		// Map object
		map = new GMap2( document.getElementById( "map" ) );
		map.enableScrollWheelZoom();
		
		// Bounds object
		var bounds = new GLatLngBounds();

		// Different icon types
		gicons[ 0 ] = new GIcon( G_DEFAULT_ICON, "http://walks.theboyhope.com/images/unclimbed.png" );
		gicons[ 1 ] = new GIcon( G_DEFAULT_ICON, "http://walks.theboyhope.com/images/climbed.png" );
		gicons[ 2 ] = new GIcon( G_DEFAULT_ICON, "http://walks.theboyhope.com/images/unclimbed2.png" );
		gicons[ 3 ] = new GIcon( G_DEFAULT_ICON, "http://walks.theboyhope.com/images/climbed2.png" );
		
		// Set the first value of the select menu
		if( document.getElementById( "menu" ) )
		{
			document.getElementById( "menu" ).options[ 0 ] = new Option( "Select a hill ...", 0 );
			document.getElementById( "menu" ).onchange = selectChanged;
		}
		
				
		// Loop through the dump array of objects
		for ( var i = 0; i < dump.length; i++ )
		{
			// Temporary variable
			var dumpi = dump[ i ];
			
			var point = new GLatLng( dumpi.latitude, dumpi.longitude );
			
			// Map centre needs to be set before you can add an overlay
			// so set a dummy centre at the first marker for now
			if( i == 0 )
				map.setCenter( point, 7 );


			// Normal icon type i.e. not a double-hill-type page
			if ( dumpi.climbed && dumpi.type == "normal" )
			{
				var marker = createMarker( point, dumpi, 1 );
			}
			else if ( !dumpi.climbed && dumpi.type == "normal" )
			{
				var marker = createMarker( point, dumpi, 0 );
			}
	
			// Second type on double-hill-type pages
			if ( dumpi.climbed && dumpi.type != "normal" )
			{
				var marker = createMarker( point, dumpi, 2 );
			}
			else if ( !dumpi.climbed && dumpi.type != "normal" )
			{
				var marker = createMarker( point, dumpi, 3 );
			}

			// Attach the marker and point to the object
			dumpi.marker = marker;
			dumpi.point = point;
			
			// HTML for climbed and unclimbed links
			dumpi.unclimbedlinkhtml = '<a onclick="updateHill( dump[ ' + i + ' ], 0 )">Mark as unclimbed</a>';
			dumpi.climbedlinkhtml = '<a onclick="updateHill( dump[ ' + i + ' ], 1 )">Mark as climbed</a>';
			
			// Extend bounds
			bounds.extend( point );
			
			// Add the marker
			map.addOverlay( marker );
			
			// Update the select menu
			updateSelect( dumpi );
		}

		// Now that the bounds are defined
		// centre the map properly and set the zoom level
		map.addMapType(G_PHYSICAL_MAP);
		map.setMapType(G_PHYSICAL_MAP);
		map.removeMapType(G_SATELLITE_MAP);
		map.setCenter( bounds.getCenter(), map.getBoundsZoomLevel( bounds ) );

		// Set various map options
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.enableDoubleClickZoom()
		map.enableContinuousZoom()
	}
	else
	{
		alert("Sorry, the Google Maps API is not compatible with this browser");
	}
}



// Create a new marker
// Parameters: GPoint, dump array object, icon-type
function createMarker( point, dumpi, icontype )
{
	var marker = new GMarker( point, {icon:gicons[ icontype ], title:dumpi.hillname } );

	if( dumpi.climbed )
	{
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowTabsHtml( [ new GInfoWindowTab( "Details", dumpi.html + dumpi.unclimbedlinkhtml ), new GInfoWindowTab( "Notes", "<h4>Sorry</h4>To track climbed and unclimbed<br />hills and keep notes on them, you need to<br /><a href='/maps/register.php' title='Registration page'>register an account</a>. It's free! :O]<br /><br />If you're already registered then<br />please <a href='/maps/' title='Login page'>log in</a>." ) ] );
		});
	}
	else
	{
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowTabsHtml( [ new GInfoWindowTab( "Details", dumpi.html + dumpi.climbedlinkhtml ), new GInfoWindowTab( "Notes", "<h4>Sorry</h4>To track climbed and unclimbed<br />hills and keep notes on them, you need to<br /><a href='/maps/register.php' title='Registration page'>register an account</a>. It's free! :O]<br /><br />If you're already registered then<br />please <a href='/maps/' title='Login page'>log in</a>." ) ] );
		});
	}

	// Attach the marker to the object
	dumpi.marker = marker;
	
	// Return the marker object
	return marker;
}



// Function gives the notes tab focus because
// there are no updates available. The user is
// not logged in.
function updateHill( dumpi, state)
{

	// Get the current info window
	var el = map.getInfoWindow();
	
	// Give the second tab focus
	el.selectTab( 1 );
}



// Function to update a user's notes for a hill
// Parameters: dump object
function saveNotes( dumpi )
{
	alert( "Sorry, but you need to register to keep notes." );
/*	
	// Relevant textarea
	var textarea = document.getElementById ( "notes" + dumpi.number );
	
	// Check the textarea exists
	if ( textarea )
	{
		// Update the object notes attribute with
		// the value from the textarea
		dumpi.notes = textarea.value;
	
		var url = "http://walks.theboyhope.com/includes/update_notes.php?hill=" + dumpi.number + "&notes=" + dumpi.notes;
		request.open("GET", url, true);
		request.send(null);
		
		alert ( "Notes saved." );
	}
	else // The textarea cannot be found
	{
		alert( "Sorry, we haven't been able to save your notes." );
	}
*/
}




// Builds a select menu of all hills
function updateSelect( dumpi )
{
	var el = document.getElementById( "menu" );
	el.options[ el.options.length ] = new Option( dumpi.hillname, dumpi.index + 1 );
}




// Onchange handler for the select menu 
function selectChanged()
{
	var el = document.getElementById( "menu" );
	var opt = el.options[ el.options.selectedIndex ].value - 1; // Corrected -1 because because the dump array is one less than the option values
	var adjusted_point = new GLatLng( dump[ opt ].latitude + 0.05, dump[ opt ].longitude );
	
	if( map.getZoom() <= 8 )
	{
		map.setCenter( dump[ opt ].point, 10 );
		GEvent.trigger( dump[ opt ].marker, 'click' );
	}
	if( map.getZoom() > 8 )
	{
		GEvent.trigger( dump[ opt ].marker, 'click' );
	}
}
