//var gdir = new GDirections(map);
var gDirWindow, srcTag, lastMark;

/* Send address query to the Google server and listen for answer */
function DrawRoute(i) { 
	
	var origin = new GLatLng(Lat[i],Lng[i]);
	var counter;
	
	srcTag = i;

	GEvent.addListener(gdir, "load", showPanel);
	GEvent.addListener(gdir, "addoverlay", hideIcon);
	GEvent.addListener(gdir, "error", handleErrors);
	
	//var coords = new Array()
	var coords = String(ground0);
	
	gdir.load("from: " + coords.substring(1,coords.length-1) + " to: " + Lat[i] + "," + Lng[i] , {"getSteps":true} );
}

/* Error handling for the directions object */
function handleErrors(){
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);

	else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
		alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
		
	else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
		alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
			
	else if (gdir.getStatus().code == G_GEO_BAD_KEY)
		alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

	else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
		alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
		
	else alert("An unknown error occurred.");				   
}

/* Hide the default destination marker (red Google pin) */
function hideIcon() {	
	gdir.getMarker(0).hide();
	gdir.getMarker(1).hide();
}

/* Create a table showing the steps from origin to the apartment complex */
function showPanel() {

    if(lastMark == lmName[srcTag]){return false;}

	var strHtml, PxOffset;
	
	PxOffset = screen.width - 435;
	
    if (gDirWindow && !gDirWindow.closed) {
        gDirWindow.close()
    }
    
    gDirWindow = window.open("","","width=420,height=530,scrollbars=yes,left=" + PxOffset + ",top=5");
	
	strHtml = "<html><head><title>Driving Directions</title><link rel='stylesheet' href='/main.css' /><link rel='stylesheet' href='/popup.css' /></head><body id='gDirBody'>";
	
	//Populate the directions table header with apartment complex name and directions summary
	//"<img src='/images/home_round_s.png' style='float:left;display:inline'>"
	strHtml =  strHtml + "<div id='Summary'>Directions from <b>" + name + "</b> to <b>" + lmName[srcTag] + "</b><br /><br />Total Trip Distance: " + gdir.getSummaryHtml() + "</div>"; 
	strHtml = strHtml  + "<table cellpadding='0' cellspacing='0'>";
	
	//Populate the directions table body with information from the gRoute object
	for (var i = 1; i < gdir.getRoute(0).getNumSteps(); i++) {
	    
		strHtml = strHtml + "<tr>"
	
		strHtml = strHtml + "<td class='step'>" + gdir.getRoute(0).getStep(i).getDescriptionHtml() + "</td>";
		strHtml = strHtml + "<td class='distance'>" + gdir.getRoute(0).getStep(i).getDistance().html + "</td>";
		
	    strHtml = strHtml + "</tr>"	
	}

	strHtml = strHtml + "</table></body></html>"	

    gDirWindow.document.write(strHtml);
	gDirWindow.focus();
	
	lastMark = lmName[srcTag];
}
