var geoCoderObj = null;
var googMap = null;
var gAddressStack = new Array();

function goog_show_map( addr, city, state, zip, lat, lng )
{
	//push onto the stack and load the google maps API if it hasn't been already
	gAddressStack.push( {"addr":addr, "city":city, "state":state, "zip":zip, "lat":lat, "lng":lng} );
	googMapInitAPI();
}

function googMapInitAPI( callback )
{
	if( typeof google == 'undefined' )
	{
		var gScript = document.createElement("script");
		gScript.type = "text/javascript";
		gScript.src = "http://maps.google.com/maps/api/js?sensor=false&callback="+(callback?callback:'checkAddressStack');
		document.body.appendChild(gScript);
	}
	else
		if( callback )
			eval( callback +"()" );
		else
			checkAddressStack();
}

function checkAddressStack()
{
	if( typeof google != 'undefined' && gAddressStack.length > 0 )
	{
		var gAddr = gAddressStack.shift();
		var fullAddr = gAddr.addr;
		if ( ( gAddr.city != null ) && ( gAddr.city.trim().length > 0 ) )
			fullAddr += ", " + gAddr.city;
		if ( ( gAddr.state != null ) && ( gAddr.state.trim().length > 0 ) )
			fullAddr += ", " + gAddr.state;
		if ( ( gAddr.zip != null ) && ( gAddr.zip.trim().length > 0 ) )
			fullAddr += ", " + gAddr.zip;

		if( gAddr.lat != null && gAddr.lng != null )
		{
			var latlng = new google.maps.LatLng( gAddr.lat, gAddr.lng );
			goog_show_location( latLngObj, fullAddr );
		}
		else
		{
			//geolocation
			if( !geoCoderObj )
				geoCoderObj = new google.maps.Geocoder();
			geoCoderObj.geocode( { 'address': fullAddr }, function( fullAddr )
				{
					return function( results, status )
					{
						if( status == google.maps.GeocoderStatus.OK )
						{
							goog_show_location( results[0].geometry.location, fullAddr );
						}
						else
						{
							//display error in floater
							Floaters.createNew( "genGoogleMap", null, "Map", '<div>We could not map "'+fullAddr+'" at this time.</div>', 515, 400 );
						}
					};
				}( fullAddr )
			);
		}
	}
}

function goog_show_location( latLngObj, locationDesc )
{
	Floaters.createNew( "genGoogleMap", null, "Map", '<div id="map_canvas" style="width:500px; height:300px;"></div><br /><div><span class="bold">Location:</span> '+locationDesc+'</div>', 515, 400 );
	var gOptions = {
		zoom: 11,
		center: new google.maps.LatLng( 41.269033, -96.04248 ),
      	mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	googMap = new google.maps.Map( document.getElementById('map_canvas'), gOptions );

	googMap.setCenter( latLngObj );
	googMap.setZoom( 15 );
	var marker = new google.maps.Marker( {map:googMap, position:latLngObj} );
}

/*merge the bottom and the top at some point*/
var locationAddressStack = new Array();
var gMarker = null;
var gInfoWindow = null;
function showLocationOnMap( container, fullAddr, gOptions )
{
	locationAddressStack.push( {'container':container, 'fullAddr':fullAddr, 'gOptions':gOptions} );
	googMapInitAPI('checkLocationStack');
}
function checkLocationStack()
{
	if( typeof google != 'undefined' && locationAddressStack.length > 0 )
	{
		var gAddr = locationAddressStack.shift();

		//geolocation
		if( !geoCoderObj )
			geoCoderObj = new google.maps.Geocoder();
		var geoCodeOptions = { 'address': gAddr.fullAddr };
		if( typeof gAddr.gOptions.bounds != 'undefined' )
			geoCodeOptions.bounds = gAddr.gOptions.bounds;
		geoCoderObj.geocode( geoCodeOptions, function( gAddr )
			{
				return function( results, status )
				{
					if( status == google.maps.GeocoderStatus.OK )
					{
						gAddr.address_components = results[0].address_components;
						mapLocation( results[0].geometry.location, gAddr );
					}
					else
					{
						//display error in floater
						alert( 'We could not map "'+gAddr.fullAddr+'" at this time.' );
					}
				};
			}( gAddr )
		);
	}
}
function mapLocation( latLng, gAddr )
{
	gAddr.gOptions.center = latLng;
	if( typeof gAddr.gOptions.zoom == 'undefined' )
		gAddr.gOptions.zoom = 15;
	if( !googMap )
		googMap = new google.maps.Map( gAddr.container, gAddr.gOptions );
	else
	{
		if( typeof gAddr.gOptions.zoom == 'undefined' )
			googMap.setZoom( 15 );
		else
			googMap.setZoom( gAddr.gOptions.zoom );
		//googMap.panTo( latLng );
	}
	var markOptions = {
		map:googMap,
		position:latLng
	};
	if( typeof gAddr.gOptions.title != 'undefined' )
		markOptions.title = gAddr.gOptions.title;
	
	var markerContents = '';
	if( typeof gAddr.gOptions.url != 'undefined' && gAddr.gOptions.url )
		markerContents += '<span><a href="'+gAddr.gOptions.url+'" target="_blank">'+gAddr.gOptions.title+'</a></span>';
	else
		markerContents += '<span><strong>'+gAddr.gOptions.title+'</strong></span>';

	var addrParts = {addr:null, street:null, city:null, state:null, zip:null};
	for( c = 0; c < gAddr.address_components.length; c++ )
	{
		var ac = gAddr.address_components[c];
		for( tc = 0; tc < ac.types.length; tc++ )
		{
			switch( ac.types[tc] )
			{
				case "street_number":
					addrParts.addr = ac.long_name;
					break;
				case "route":
					addrParts.street = ac.long_name;
					break;
				case "locality":
					addrParts.city = ac.long_name;
					break;
				case "administrative_area_level_1":
					addrParts.state = ac.short_name;
					break;
				case "postal_code":
					addrParts.zip = ac.short_name;
					break;
			}
		}
	}

	var directionsLink = '';
	if( addrParts.addr && addrParts.street )
	{
		markerContents += '<br /><span>'+addrParts.addr+' '+addrParts.street+'</span>';
		directionsLink += addrParts.addr+' '+addrParts.street+' ';
	}
	if( addrParts.city && addrParts.state )
	{
		markerContents += '<br /><span>'+addrParts.city+', '+addrParts.state+(addrParts.zip?' '+addrParts.zip:'')+'</span>';
		directionsLink += addrParts.city+', '+addrParts.state+(addrParts.zip?' '+addrParts.zip:'');
	}
	directionsLink += ' @'+latLng.toUrlValue();
	markerContents += '<br /><br /><span style="word-wrap:break-word;">Directions: <a href="http://www.google.com/maps?daddr='+encodeURIComponent(directionsLink)+'" target="_blank">To here</a> <a href="http://www.google.com/maps?saddr='+encodeURIComponent(directionsLink)+'" target="_blank">From here</a></span>';

	//delete the existing one
	if( gInfoWindow )
	{
		gInfoWindow.setMap(null);
		gInfoWindow = null;
	}
	gInfoWindow = new google.maps.InfoWindow({ content:markerContents });
	
	//delete existing one
	if( gMarker )
	{
		gMarker.setMap(null);
		gMarker = null;
	}
	gMarker = new google.maps.Marker( markOptions );
	google.maps.event.addListener( gMarker, 'click', function(){ gInfoWindow.open(googMap,gMarker); } );
	setTimeout( function(){ google.maps.event.trigger(gMarker, 'click'); }, 500 );
}

