// ====== global vars ======
var geo;
var reasons=[];

// ====== 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: Please tell us where you had your picnic.";
reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown address: Sorry, we can't find that location. Please check the spelling or try another place name.";
reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unknown address: Sorry, we can't find that location. Please check the spelling or try another place name.";
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.";
    		

// ====== redirect to form with lat,lng and loc ======
function place(lat,lng,loc) {
	// clear results div
	document.getElementById("Results").innerHTML = '';
	
	// load into ajax div
	$j.ajax({
		url: '/picnics/addmap/'+lat+'/'+lng+'/'+encodeURIComponent(loc)+'/',
  		cache: false,
  		success: function(html){
    		$j("#ajax").html(html);
  		}
	});
}   	
      		

// ====== Geocoding ======
function showAddress() {
	var search = document.getElementById("PicnicLocation").value;
    // ====== Perform the Geocoding ======      
    geo.setBaseCountryCode('UK');   
    geo.getLocations(search, function (result)
    {
    	//initialMap.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) { 

            	var html = "<h3>Did you mean:</h3>\n<ul>";

		        // Loop through the results
        		for (var i=0; i<result.Placemark.length; i++) {

                	var p = result.Placemark[i].Point.coordinates;
                	var enc = encodeURIComponent(result.Placemark[i].address)
                  	html += "<li>"+(i+1)+": <a href='javascript:place(" +p[1]+","+p[0]+",\""+enc+"\")'>"+result.Placemark[i].address+"</a></li>";
           			
                }
                html += "</ul>";
                document.getElementById("Results").innerHTML = html;
          	}
		    // ===== If there was a single marker =====
        	else {
            	document.getElementById("Results").innerHTML = '';
                	var p = result.Placemark[0].Point.coordinates;
                	place(p[1],p[0],encodeURIComponent(search));
          	}
       	}
		// ====== Decode the error status ======
        else {               
        	var reason="Code "+result.Status.code;
            if (reasons[result.Status.code]) {
            	reason = reasons[result.Status.code]
           	} 
         
        	var html = "<h3>Error:</h3>\n";
        	html += "<p>"+reason+"</p>"; 
        	document.getElementById("Results").innerHTML = html;         
         
     	}
 	});
        		
    return false;
}      	
