//The caller of showAddress should have the map and geocoder defined and initialized
//It should define a global marker (gmarker) as well as the variables infoWindowHtml and directionsInfoWindowHtml and
// initialize them to null.

//A function to showw the address in a special condition as follows:
// .. where the back-end address doesn't really exist in the google API nor
// .. in the web framework, so we have to use a cross street, but in the marker
// .. we'd like to display the actual address.  So the get directions thing will
// .. send to google the cross street and that will show up in the popup window, but
// .. at least the actual address will show up on the map page itself on the marker
function showAddressWithDifferentDisplayAddress(myAddress1, myAddress2, myDisplayAddress1, myDisplayAddress2) {
	myAddress = myAddress1 + ', ' + myAddress2;
	if (geocoder) {
		geocoder.getLatLng(
			myAddress,
			function(point) {
				if (point) {
					map.setCenter(point, 13);
					createMarker(point, myAddress1, myAddress2, myDisplayAddress1, myDisplayAddress2);
					initializeMap();
				}
			}
		);
	}
}

//A utility method for when the display address is the same as the back-end address we're searching on
function showAddress(myAddress1, myAddress2) {
	showAddressWithDifferentDisplayAddress(myAddress1, myAddress2, myAddress1, myAddress2);
}

//A function to create the marker and set up the event window
function createMarker(point, myAddress1, myAddress2, myDisplayAddress1, myDisplayAddress2) {
	gmarker = new GMarker(point);

	myHtml = '<span style="font-family: Arial, Helvetica, sans-serif; font-size: 14px;">';
	myHtml += myDisplayAddress1 + '<br/>' + myDisplayAddress2 + '</span>';

	// The info window version with the "to here" form open
	directionsInfoWindowHtml = myHtml + '<form action="/w/maps.google.com/maps" method="get" ' +
        (isKiosk?'target="_parent"':'target="_blank"') + '>' +
		'<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">' +
		'Start address:<br/>' +
		'<input type="text" SIZE=40 MAXLENGTH=60 name="saddr" id="saddr" value="" /><br/>' +
		'<INPUT value="Get Directions" TYPE="SUBMIT">' +
		'<input type="hidden" name="daddr" value="' + myAddress1 + ', ' + myAddress2 + '"/>' +
		'</span>' +
		'</form>';

	// The inactive version of the direction info
	myHtml = myHtml + '<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;"><br>Directions: <a href="javascript:tohere()">To here</a></span>';

	GEvent.addListener(gmarker, "click", function() {
		gmarker.openInfoWindowHtml(myHtml);
	});

	infoWindowHtml = myHtml;
}

//A function to show the address in a special condition as follows:
// .. where the back-end address is not in sync with the google web address
// .. so that we have to use a different google maps API address than the one
// .. that is used to get the directions for them in the popup window.
function showAddressWithDifferentAPILocation(myAPIAddress1, myAPIAddress2, myDisplayWebAddress1, myDisplayWebAddress2) {
	//Remember to use the web address as the display address too...
	myAddress = myAPIAddress1 + ', ' + myAPIAddress2;
	if (geocoder) {
		geocoder.getLatLng(
			myAddress,
			function(point) {
				if (point) {
					map.setCenter(point, 13);
					//Just use the same createMarker function...
					createMarker(point, myDisplayWebAddress1, myDisplayWebAddress2, myDisplayWebAddress1, myDisplayWebAddress2);
					initializeMap();
				}
			}
		);
	}
}

function initializeMap() {
	map.addOverlay(gmarker);
	gmarker.openInfoWindowHtml(infoWindowHtml);
}

function tohere() {
	gmarker.openInfoWindowHtml(directionsInfoWindowHtml);
}