/** Code to detect if gear has been installed, if NOT redirect to google gear page */
if (!window.google || !google.gears) {
    
    var txt= "http://gears.google.com/?action=install&message=Welcome to Google Gear Installation" +
                    "&return=http://www.ajaxapp.com/2009/03/25/google-gear-geolocation-api-example-displays-your-location-on-google-map/";

    document.write('<div id="googleGearsPrompt" style="border:1px solid #556688;padding:5px;font-size:12pt;background:#FFFFDD;color:#DD0000;display:block;">Oppps! You haven\'t had Google Gears Installed'+
    '<br/>Click <a href="'+txt+'" style="font-size:24pt;font-weight:bolder;" target=_new>HERE</a> to install Google Gears in order to view the example below correctly<br/><br/>'+
    '<input type=button value="Close" onClick="document.getElementById(\'googleGearsPrompt\').style.display=\'none\';"/></div>');
}


var geo = google.gears.factory.create('beta.geolocation');

// The global variable latitude
var lat=0;

// The global variable longitude
var lon=0;

// The address name
var addr='';


/**
The call-back function to get
latitude, longtitude and the address of the
location using Google Gear Geolocation API
*/
function updatePosition(position) {
  lat=position.latitude;
  lon= position.longitude;

  // Get the address with all posible values, such as
  // street number, street, city, state, country etc
  addr='';
  if ( position.gearsAddress.streetNumber!=null )
  	addr+=position.gearsAddress.streetNumber+', ';
  if ( position.gearsAddress.street!=null )
  	addr+=position.gearsAddress.street+', ';
  if ( position.gearsAddress.city!=null )
  	addr+=position.gearsAddress.city+', ';
  if ( position.gearsAddress.region!=null ){
  	if (addr!='' )addr+='<br/>';
  	addr+=position.gearsAddress.region+', ';
  }
  if ( position.gearsAddress.country!=null )
   	addr+=position.gearsAddress.country+', ';
  if ( position.gearsAddress.postalCode!=null )
  	addr+=position.gearsAddress.postalCode;

}

/**
The call-back function to display error
while it's unable to fetch the location of the
WiFi user. It'll display the error message on
the div with ID "err_stat"
*/
function handleError(positionError) {

  var e=document.getElementById('err_stat');
  if ( e!=null )
  {
		e.style.display='block';
		e.innerHTML='<br/><br/>Oppps! Attempt to get location failed: ' + positionError.message;
  }
}

// Get the location with high accuracy
geo.getCurrentPosition(updatePosition, handleError,
{ enableHighAccuracy: true,gearsRequestAddress: true });

