var blDebug = false;
var sDebug = '';
var sListFromAddress = '';
var aListLocations = Array();
var iListIndex = 0;
    var gdir;
    var addressMarker;
	
	function initializeList() {
      if (GBrowserIsCompatible()) {      
        gdir = new GDirections();
        GEvent.addListener(gdir, "load", onGDirectionsLoadList);
        GEvent.addListener(gdir, "error", handleErrorsList);
      }
    }
    
    function setDirectionsList(iIndex) {
    	if (iIndex >= aListLocations.length)
    	{
    		return;	
    	}
    	fromAddress = sListFromAddress;
    	toAddress = aListLocations[iIndex][1];
    	if (0 == fromAddress.length || null == fromAddress)
    	{
    		return false;	
    	}
		if (0 == toAddress.length || null == toAddress)
    	{
    		return false;	
    	}    	
    	
    	sDebug += "from: " + fromAddress + " to: " + toAddress + "<br/>";
  		 print_debug(sDebug);
      gdir.load("from: " + fromAddress + " to: " + toAddress,
                { "locale":"de_DE" });

    }


    function handleErrorsList(){
//	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
//	     alert("Es wurden keine geograpischen Daten für eine der Adressen gefunden. Es ist möglich, das diese Adresse realtiv neu, oder nicht korrekt ist. \nError code: " + gdir.getStatus().code);
//	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
//	     alert("Die Ermittlung der geopgrahischen Daten war nicht erfoglreich, die genaue Ursache ist nicht bekannt.\n Error code: " + gdir.getStatus().code);
//	   
//	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
//	     alert("Es wurde keine Adresse eingegeben.\n Error code: " + gdir.getStatus().code);
//
//	//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
//	//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
//	     
//	   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
//	     alert("Es trat ein Problem mit dem API-Schlüssel auf.\n Error code: " + gdir.getStatus().code);
//
//	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
//	     alert("Die Anfrage zur Routenberechnung konnte nicht erfolgreich bearbeitet werden.\n Error code: " + gdir.getStatus().code);
//	    
//	   else alert("Es trat ein unbekannter Fehler auf.");
	    if (iListIndex < aListLocations.length)
	  {
	  	iListIndex++;	
	  	setDirectionsList(iListIndex);
	  }
	}

	function onGDirectionsLoadList(){ 
      // Use this function to access information about the latest load()
      // results.

      // e.g.
      // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	  // and yada yada yada...
	 
	  
	  if (iListIndex >= aListLocations.length)
    	{
    		return;	
    	}
	  var sSummaryID = aListLocations[iListIndex][0];
	  if (0 == sSummaryID.length)
	  {
	  	return;	
	  }	
	  
	 
	  var sSummary = ': ';
	  sSummary += '<a href="anfahrt/' + aListLocations[iListIndex][0] + '/';
	  sSummary += aListLocations[iListIndex][1] + '/';
	  sSummary += sListFromAddress;
	  sSummary += '" title="Wegbeschreibung nach ' + aListLocations[iListIndex][1] + ' aufrufen">';
	  sSummary += "Fahrtstrecke " + gdir.getDistance().html + ", Fahrtzeit etwa " + gdir.getDuration().html;
	  sSummary += "</a>";
	  sDebug += sSummary + "<br/>";
	  print_debug(sDebug);
	  sSummaryID = 'object_direction_summary_' + sSummaryID;
	  var oSummarySpan = $(sSummaryID);
	  if (oSummarySpan)
	  {
	  	oSummarySpan.update(sSummary);
	  }
	  
	  
	  if (iListIndex < aListLocations.length)
	  {
	  	iListIndex++;	
	  	setDirectionsList(iListIndex);
	  }
	  
	}
	
function listCalculationDirections()
{
	initializeList();
	sDebug = 'Start: ' + sListFromAddress + '<br/>';
	print_debug(sDebug);
	setDirectionsList(0);

}

function print_debug(sString)
{
	var oDivDebug = $('debug');
	if (blDebug && oDivDebug)
	{	
		oDivDebug.update(sDebug);	
	}
}