 
var map = null; 
var geocoder = null; 
 
function getHomeCoords() 
{ 
	//alert(  mapCenterLat + "x" +  mapCenterLng + " length= " + mapCenterLat.length ); 
	 
	// if these were all set via backend 
	if( g_mapCenterLat != null && 
		g_mapCenterLng != null ) 
		return( new GLatLng( g_mapCenterLat, g_mapCenterLng ) ); 
	else 
		return( new GLatLng(43.98271769541311, -69.2042863368988) ); 
} 
 
function getZoomLevel() 
{ 
	if( g_mapZoom != null ) 
		return( g_mapZoom ); 
	 
	return( 14 );	 
} 
function initialize() 
{ 
	if( !GBrowserIsCompatible() ) 
	{ 
		alert( "Browser is not compatible with Google Maps" ); 
		return; 
	} 
	 
	map = new GMap2(document.getElementById("map"), {mapTypes:[G_NORMAL_MAP,G_SATELLITE_MAP,G_HYBRID_MAP,G_PHYSICAL_MAP]}); 
	map.setCenter( getHomeCoords(), getZoomLevel()); 
	map.addControl(new GLargeMapControl()); 
	map.addControl(new GMapTypeControl()); 
	map.addControl(new GScaleControl());
	map.setMapType(G_HYBRID_MAP); 
	 
	setMapInfo();
	showTidalMarkers();
	 
	// bind a search control to the map, suppress result list 
	//map.addControl(	new google.maps.LocalSearch(), 
	//				new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,20)) ); 
	 
	GEvent.addListener(map, "click", function(overlay,point) 
		{ 
			if( null == point ) 
				return; 
				 
      		//var myHtml = "The GPoint value is: " + map.fromLatLngToDivPixel(point) + " at zoom level " + map.getZoom(); 
      		//var myHtml = "LAT: " + point.lat() + "\nLNG: " + point.lng(); 
      		 
      		var elemLat = document.getElementById("id_lat"); 
      		var elemLng = document.getElementById("id_lng"); 
      		if( elemLat != null && 
      			elemLng != null ) 
      		{ 
      			elemLat.value = point.lat(); 
      			elemLng.value = point.lng(); 
      		} 
      		// if someone clicked on the map, then update the hazard info for add 
      		var elem = document.getElementById("id_hazard_op"); 
      		if( elem != null ) 
      		{ 
      			elem.innerHTML = "OP:ADD"; 
      		} 
      		// and empty the fields that would be unknown at this time 
      		elem = document.getElementById("id_radius"); 
      		if( elem ) elem.value = ""; 
      		elem = document.getElementById("id_elevation"); 
      		if( elem ) elem.value = ""; 
      		elem = document.getElementById("id_name"); 
      		if( elem ) elem.value = ""; 
      		elem = document.getElementById("id_description"); 
      		if( elem ) elem.value = ""; 
      		elem = document.getElementById("id_gone"); 
      		if( elem ) elem.value = ""; 
      		 
      		var select = null;          		 
			select = document.getElementById("id_haz_type"); 
			if( select != null ) 
				jsSelect( select, "select_first" ); 
			 
			//select = document.getElementById("id_context"); 
			//if( select != null )  
			//	jsSelect( select, 0 );				 
			 
			select = document.getElementById("id_radius"); 
			if( select != null ) 
				jsSelect( select, 0 ); 
			 
			select = document.getElementById("id_elevation"); 
			if( select != null ) 
				jsSelect( select, 0 ); 
			 
			 
			// change the form op to edit 
			elem = document.forms['id_hazard'].id_op; 
			if( elem ) elem.value = "add"; 
			else alert( "id_hazard not found" ); 
										          		 
      		// name the button appropriately 
      		elem = document.getElementById("id_btnCreateHazard"); 
      		if( elem ) elem.value = "Create Hazard"; 
      		 
      		setMapInfo(); 
    	} 
    	 
    	//getBounds() 
    ); 
     
     
	GEvent.addListener(map, "zoomend", function(oldzoom,zoom) 
		{ 
			// not zoomed in far enough, so encourage user to get closer to see listing. 
			if( zoom < g_ZoomLevel ) 
				//alert( "You are zoomed at " + zoom + "\nPlease zoom to " + g_ZoomLevel + " to see markers" ); 
				 
			setMapInfo();
			showTidalMarkers();
		} 
	);	 
	GEvent.addListener(map, "dragend", function() 
		{ 
			setMapInfo();
			showTidalMarkers();
		}		 
	); 
     
	geocoder = new GClientGeocoder(); 
} 

function showTidalMarkers()
{
	var bounds = map.getBounds();
	var zoom = map.getZoom();
	var ne = bounds.getNorthEast(); 
	var sw = bounds.getSouthWest(); 
	 
	var westLng = sw.lng(); 
	var eastLng = ne.lng(); 
	var northLat = ne.lat(); 
	var southLat = sw.lat();
	
	var stations = new CTideStations( zoom, westLng, eastLng, northLat, southLat );
	
	/*
	document.write(	"zoom: " + zoom + "\n" +
			"westLng: " + westLng + "\n" +
			"eastLng: " + eastLng + "\n" +
			"northLat: " + northLat + "\n" +
			"southLat: " + southLat + "\n\n" +
			"http://www.hazmap.net/processor/tide_stations.php?" +
			"zoom="+zoom+
			"&westLng="+westLng+
			"&eastLng="+eastLng+
			"&northLat="+northLat+
			"&southLat="+southLat );
	*/	
}

// take the current zoom level of the map and its size, 
// and list all hazards that exist in that area 
function setMapInfo() 
{ 
//	alert( "start setMapInfo" );
	
	if( null == map ) 
		return; 
	 
	var frm = document.forms['id_MapSize']; 
	 
	if( null == frm ) 
	{ 
		alert( "id_MapSize is null" ); 
		return; 
	} 
		 
	var bounds = map.getBounds(); 
	var ne = bounds.getNorthEast(); 
	var sw = bounds.getSouthWest(); 
	 
	var westLng = sw.lng(); 
	var eastLng = ne.lng(); 
	var northLat = ne.lat(); 
	var southLat = sw.lat(); 
	 
	var elem = null; 
	// set the values into the form 
	// these are used in the sql query to find 
	// other coords in this area 
	elem = frm.id_westLng; 
	if( elem ) elem.value = westLng; 
	else alert( "id_westLng not found" ); 
	 
	elem = frm.id_eastLng; 
	if( elem ) elem.value = eastLng; 
	else alert( "id_eastLng not found" ); 
	 
	elem = frm.id_northLat; 
	if( elem ) elem.value = northLat; 
	else alert( "id_northLat not found" ); 
	 
	elem = frm.id_southLat; 
	if( elem ) elem.value = southLat; 
	else alert( "id_southLat' not found" ); 
	 
	// update the map position and zoom, 
	// needed by JS when page reloads to put the map where it was	 
	g_mapCenterLat = map.getCenter().lat(); 
	g_mapCenterLng = map.getCenter().lng(); 
	g_mapZoom = map.getZoom(); 
	 
	 
	// pass these js vars via get 

	//alert( "PRE Form action = \n" +  frm.action + "\ng_URLNoArgs = " + g_URLNoArgs);


	if( 1 )
	{
	frm.action = 	g_URLNoArgs + 
			"?t=" + getURLV( 't' ) +
			"&lat=" + g_mapCenterLat +
			"&lng=" + g_mapCenterLng +
			"&z=" + g_mapZoom;
	}
	else
	{

	frm.action += 	"&lat=" + g_mapCenterLat + 
					"&lng=" + g_mapCenterLng + 
					"&z=" + g_mapZoom; 
	}

	//alert( "POST Form action = \n" +  frm.action );

	/* 
	frm.action += 	"&lat=" + g_mapCenterLat + 
					"&lng=" + g_mapCenterLng + 
					"&z=" + g_mapZoom; 
	
	// set the map center into the form 
	elem = document.forms['id_MapSize'].id_mapCenterLat; 
	if( elem ) elem.value =  
	else alert( "id_mapCenterLat' not found" ); 
	elem = document.forms['id_MapSize'].id_mapCenterLng; 
	if( elem ) elem.value 
	else alert( "id_mapCenterLng' not found" ); 
	 
	// set the zoom level into the form 
	elem = document.forms['id_MapSize'].id_mapZoom; 
	if( elem ) elem.value 
	else alert( "id_mapZoom' not found" ); 
*/ 
	 
} 

function showAddress(address) 
{
	/*
	// is the address some lat lng coords
	var coords = new HazLatLng( address );
	
	if( coords.valid() )
	{
		map.setCenter( coords.getGLatLng() );				
		return;
	}
	*/
	// ok, didn't appear to be coords, so try geocoding it.		 
	if (!geocoder) 
		return; 
	 
	geocoder.getLatLng( address, function(point) 
		{ 
			if (!point) 
			{ 
				alert(address + " not found"); 
			} 
			else 
			{ 
				map.setCenter(point);//, 13); 
				var marker = new GMarker(point); 
				map.addOverlay(marker); 
				marker.openInfoWindowHtml(address); 
				 
				setMapInfo(); 
			} 
		} 
	); 
} 
 
function getHTML(lat, lng, type, name, desc, created) 
{
	var style = "style=\"" +
				"font-family: arial; " +
				"font-weight: lighter; " +
				"font-size: 12px;" +
				"\"";
				
	var buf = 	"<table  " + style + " cellpadding=\"0\" cellspacing=\"0\" border=\"0\">" +  
				"<tr><td><b>Lat:</b></td><td>&nbsp;&nbsp;</td><td>" + lat + "</td></tr>" + 
				"<tr><td><b>Lng:</b></td><td>&nbsp;&nbsp;</td><td>" + lng + "</td></tr>" + 
				"<tr><td><b>Type:</b></td><td>&nbsp;&nbsp;</td><td>" + type + "</td></tr>" + 
				"<tr><td><b>Name:</b></td><td>&nbsp;&nbsp;</td><td>" + name + "</td></tr>" + 
				"<tr><td><b>Desc:</b></td><td>&nbsp;&nbsp;</td><td>" + breakText( desc, 48 ) + "</font></td></tr>" + 
				"<tr><td><b>Created:</b></td><td>&nbsp;</td><td> " + created + "</td></tr>" + 
				"</table>";
				 
	return( buf ); 
} 
 
function navTo( lat, lng, type, name, desc, created ) 
{ 
	//var myLat = new Number( lat ); 
	//var myLng = new Number( lng ); 
	 
	//alert( "lat: " + lat + "\nlng: " + lng ); 
	//map.setCenter( new GLatLng(myLat.valueOf, myLng.valueOf), 13); 
	var coords = new GLatLng( lat, lng); 
	map.setCenter( coords );//, getZoomLevel() ); 
	 
	map.openInfoWindowHtml(coords, getHTML(lat, lng, getHazTypeString(type), name, desc, created)); 
} 
 
/* 
// take the current zoom level of the map and its size, 
// and list all hazards that exist in that area 
function refreshHazardList() 
{ 
	//setMapInfo(); 
	/* 
	// set the values into the form 
	var elem = null; 
	elem = document.forms['id_MapSize'].id_westLng; 
	westLng = elem.value; 
	 
	elem = document.forms['id_MapSize'].id_eastLng; 
	eastLng = elem.value; 
	 
	elem = document.forms['id_MapSize'].id_northLat; 
	northLat = elem.value; 
	 
	elem = document.forms['id_MapSize'].id_southLat; 
	southLat = elem.value; 
	 
	/*alert( "Form set to:\n" + 
		"westLng = " + westLng + "\n" + 
		"eastLng = " + eastLng + "\n" + 
		"northLat = " + northLat + "\n" + 
		"southLat = " + southLat + "\n" +  
		"Submitting form" ); 
	 
	 
	var frm = document.forms['id_MapSize']; 
	 
	if( null == frm ) 
	{ 
		alert( "id_MapSize is null" ); 
		return; 
	} 
	 
	frm.submit(); 
} 
*/ 
 
//GSearch.setOnLoadCallback(initialize); 
      
