// Open the info box for the specified marker.
function popup( i ) {
    markers[i].openInfoWindowHtml( infoHtmls[i] + 
    	'<div id="directions' + i + '">' + 
    		'Directions: <a href="javascript:createToDirections(' + i + ');">To here</a> - <a href="javascript:createFromDirections(' + i + ');">From here</a>' + 
    	'</div>'
    );
}

function makePopupCaller( i )
    {
    	return function() { popup( i ); };
    }

function createToDirections( i ) {
    markers[i].openInfoWindowHtml( infoHtmls[i] + 
    	'<div id="directions' + i + '">' + 
    		'<form onsubmit="return false;">' +
    		'Directions: <b>To here</b> - <a href="javascript:createFromDirections(' + i + ');">From here</a><br />' + 
			'Start address:<br />' + 
			'<input type="text" style="width:200px" id="toAddress' + i + '"><br />' + 
			'<input type="submit" value="Get Directions" onclick="launchToDirections(' + i + ');">' +
			'</form>' +
    	'</div>'
    ); 
 
}
function createFromDirections( i ) {

    markers[i].openInfoWindowHtml( infoHtmls[i] + 
    	'<div id="directions' + i + '">' + 
    		'<form onsubmit="return false;">' +
			'Directions: <a href="javascript:createToDirections(' + i + ');">To here</a> - <b>From here</b><br />' + 
			'End address:<br />' + 
			'<input type="text" style="width:200px" id="fromAddress' + i + '"><br />' + 
			'<input type="submit" value="Get Directions" onclick="launchFromDirections(' + i + ');">' + 
			'</form>' +
		'</div>'
    );
}
function launchFromDirections( i ) {
	var fromAddress = document.getElementById('fromAddress' + i);
	
	window.open('http://maps.google.com/maps?saddr=' + encodeURI(addresses[i]) + '&daddr=' + encodeURI(fromAddress.value) + '&hl=en','directions','');
}
function launchToDirections( i ) {
	var toAddress = document.getElementById('toAddress' + i);
	
	window.open('http://maps.google.com/maps?saddr=' + encodeURI(toAddress.value) + '&daddr=' + encodeURI(addresses[i]) + '&hl=en','directions','');
}

// Concatenate the values in an element list.
function concatValues( elements )
	{
		var values = '';
		for ( var i = 0; i < elements.length; ++i )
			{
				if ( values != '' ) values += ' ';
				values += elements[i].firstChild.nodeValue;
			}
		return values;
    }