var map;  // declare outside function, so global

function loadMapBrowse() 
{ 
// 	//DBG
// 	alert('DBG: loadMap() called!');
	if (GBrowserIsCompatible()) 
	{ 
//   var map = new GMap2(document.getElementById("map")); 
		map = new GMap2(document.getElementById("map")); 
		map.addControl(new GSmallMapControl()); 
		map.addControl(new GMapTypeControl()); 
// 		map.setCenter(new GLatLng(47.648419,-122.342849), 11); 
		map.setCenter(new GLatLng(47.30,-120.65), 7); 
		
// // hard code one point:
// 		var point = new GLatLng(47.649600982666, -122.349998474121); 
// 		var marker = new GMarker(point); 
// 	 	GEvent.addListener(marker, "click", function()  
// //  	 	{    marker.openInfoWindowHtml("Marker #<b>" + "18" + "</b>"  );  });   
// 	  	{    marker.openInfoWindowHtml("Hazard #<b>" + "18" + "</b>" + "<br /><a href=item.php?id=18>View Details</a>" );  });   
// 		 map.addOverlay( marker); 
		 
// Call Javascript to add all geocoded bugs to the map
// Note: must make this a call here, but php code in mapHazards.php creates the function below....
// doing it this way because php is evaluated first, and this code not executed until after...
// ..and it must be done after the php, or the map item will not yet have been created.
// 	 	 mapBug( 47.6087989807129, -122.33699798584, 16 );
		 mapBugs(); 
    }
      
} 




function createMarker(point, BugID, BugInfoHtml)
{  
// 	//debug
// 	alert ("createMarker: Point: " + point + ", BugID: " + BugID );
	
	var marker = new GMarker(point);
	if ( BugInfoHtml.length > 0 )
	{
		GEvent.addListener(marker, "click", function() 
	{    marker.openInfoWindowHtml(BugInfoHtml);  });  
	}
	else
	{
		// empty string passed in, don't create marker balloon.
	}
	return marker;
}

	                            
function mapBug( BugLat, BugLong, BugID, BugInfoHtml)
{
	var point = new GLatLng(BugLat, BugLong);
// 	//debug
// 	alert ("mapBug: Lat/Long: " + BugLat + ", " + BugLong );

// 	var BugInfoHtmlStripped = BugInfoHtml.replace(/'/gi,'"');
// 	//debug
// 	alert ("BugInfoHtml before: " + BugInfoHtml + ", and after: " + BugInfoHtmlStripped);
	
	var marker = new createMarker(point, BugID, BugInfoHtml);
	map.addOverlay(marker);
	
// For some reason (?!) the following line DOES NOT WORK!  Must call createMarker with var/new first, then separate call to map.addOverlay.
// What a fucking pain!!!
// 	map.addOverlay(createMarker(point, BugID);
// nope. the reason it didn't work was because I was missing a paren!!! which is why i HATE javascript!
}

function loadMapItem() 
{ 
// 	//DBG
// 	alert('DBG: loadMapItem() called!');
	if (GBrowserIsCompatible()) 
	{ 
//   var map = new GMap2(document.getElementById("map")); 
		map = new GMap2(document.getElementById("map")); 
		map.addControl(new GSmallMapControl()); 
		map.addControl(new GMapTypeControl()); 
// 		map.setCenter(new GLatLng(47.648419,-122.342849), 11); 
		map.setCenter(new GLatLng(47.30,-120.65), 13); 
		
// // hard code one point:
// 		var point = new GLatLng(47.649600982666, -122.349998474121); 
// 		var marker = new GMarker(point); 
// 	 	GEvent.addListener(marker, "click", function()  
// //  	 	{    marker.openInfoWindowHtml("Marker #<b>" + "18" + "</b>"  );  });   
// 	  	{    marker.openInfoWindowHtml("Hazard #<b>" + "18" + "</b>" + "<br /><a href=item.php?id=18>View Details</a>" );  });   
// 		 map.addOverlay( marker); 
		 
// Call Javascript to add all geocoded bugs to the map
// Note: must make this a call here, but php code in mapHazards.php creates the function below....
// doing it this way because php is evaluated first, and this code not executed until after...
// ..and it must be done after the php, or the map item will not yet have been created.
// 	 	 mapBug( 47.6087989807129, -122.33699798584, 16 );
		 mapSingleBug(); 
    }
      
} 
                           
