////////////////////////////////////////////////////////////////////////
// Populate a search box with the user's current location
//
// OurAirports uses an opt-in approach for geolocation - the user must
// explicitly click on a link and submit a search to use his/her
// current location.
//
// This script looks for an HTML element with the id 
// "prompt-geolocation" and replaces its content with a prompt IF the
// browser supports the W3C geolocation API.  When the user clicks on
// the prompt, the browser will populate the search box (with the id
// "form.search.q" with the user's current latitude and longitude.
// The user may then submit the search if desired.
////////////////////////////////////////////////////////////////////////

/**
 * Show the geolocation in the search box
 */
function showGeolocation (position) {
    var node = document.getElementById('form.search.q');
    node.setAttribute('value', position.coords.latitude + " " + position.coords.longitude);
    node.focus();
}

/**
 * Send the asynchronous geolocation query
 */
function getGeolocation () {
    var node = document.getElementById('form.search.q');
    node.setAttribute('value','Getting your location (please wait) ...');
    navigator.geolocation.getCurrentPosition(showGeolocation);
}

//
// If the W3C geolocation API is supported, add a link
//
if (navigator.geolocation) {
    var node = document.getElementById('prompt-geolocation');
    var link = document.createElement('a');
    link.setAttribute('title','Use current latitude and longitude as supplied by your browser.');
    link.setAttribute('href','javascript:getGeolocation()');
    link.appendChild(document.createTextNode('fill in my current location'));
    node.appendChild(link);
    node.appendChild(document.createTextNode(' | '));
}

// end

