 var map;
    var gdir;
    var geocoder = null;
    var addressMarker;
	var reasons=[];

    function initialize() {
      if (GBrowserIsCompatible()) {      
        map = new GMap2(document.getElementById("map_canvas"));

		map.setCenter(new GLatLng(54.66708073763456, 25.279541015625),10);

		map.addControl(new GMapTypeControl());
		map.addControl(new GScaleControl());
		map.addControl(new GOverviewMapControl());

		map.addControl(new GLargeMapControl());

		map.enableContinuousZoom();

		 // ====== Create a Client Geocoder ======
		  geo = new GClientGeocoder(); 

		  // ====== Array for decoding the failure codes ======
		  reasons[G_GEO_SUCCESS]            = "Success";
		  reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
		  reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
		  reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
		  reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
		  reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
		  reasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";


        gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "error", handleErrors);
      }
    }
    
    function setDirections(fromAddress, toAddress, locale) {
      gdir.load("from: " + fromAddress + " to: " + toAddress,
                { "locale": locale });
    }

    function handleErrors(){
   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		showAddress();
   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.");
    }


	 // ====== Geocoding ======
      function showAddress() {
        var search = document.getElementById("fromAddress").value;
        // ====== Perform the Geocoding ======        
        geo.getLocations(search, function (result)
          {
            map.clearOverlays(); 
            if (result.Status.code == G_GEO_SUCCESS) {
              // ===== If there was more than one result, "ask did you mean" on them all =====
              if (result.Placemark.length > 1) { 
                //document.getElementById("directions").innerHTML = "";
                // Loop through the results
                for (var i=0; i<result.Placemark.length; i++) {
                  var p = result.Placemark[i].Point.coordinates;
                  document.getElementById("directions").innerHTML += "<br>"+(i+1)+": <a href='javascript:place(\"" +result.Placemark[i].address+"\")'>"+ result.Placemark[i].address+"<\/a>";
                }
              }
              // ===== If there was a single marker =====
              else {
                document.getElementById("directions").innerHTML = "";
                var p = result.Placemark[0].Point.coordinates;
                place(result.Placemark[0].address);
              }
            }
            // ====== Decode the error status ======
            else {
              var reason="Code "+result.Status.code;
              if (reasons[result.Status.code]) {
                reason = reasons[result.Status.code]
              } 
              alert('Could not find "'+search+ '" ' + reason);
            }
          }
        );
      }

	 function place(from) {
		var to = document.getElementById("fromAddress").value;
		var local = document.getElementById("locale").value;
		document.getElementById("fromAddress").value = from;
		document.getElementById("directions").innerHTML = '';
     }

	$(document).ready(function() {
		initialize();
	});
