document.getElementById('w3c').innerHTML='loading...';
document.getElementById('gears').innerHTML='loading...';
document.getElementById('client').innerHTML='loading...';
document.getElementById('webwm').innerHTML='loading...';
document.getElementById('loki').innerHTML='loading...';
document.getElementById('bb').innerHTML='loading...';

(function() {
  // We are already defined. Hooray!
  if (window.google && google.gears) {
    return;
  }

  var factory = null;

  // Firefox
  if (typeof GearsFactory != 'undefined') {
    factory = new GearsFactory();
  } else {
    // IE
    try {
      factory = new ActiveXObject('Gears.Factory');
      // privateSetGlobalObject is only required and supported on IE Mobile on
      // WinCE.
      if (factory.getBuildInfo().indexOf('ie_mobile') != -1) {
        factory.privateSetGlobalObject(this);
      }
    } catch (e) {
      // Safari
      if ((typeof navigator.mimeTypes != 'undefined')
           && navigator.mimeTypes["application/x-googlegears"]) {
        factory = document.createElement("object");
        factory.style.display = "none";
        factory.width = 0;
        factory.height = 0;
        factory.type = "application/x-googlegears";
        document.documentElement.appendChild(factory);
      }
    }
  }

  // *Do not* define any objects if Gears is not installed. This mimics the
  // behavior of Gears defining the objects in the future.
  if (!factory) {
    return;
  }

  // Now set up the objects, being careful not to overwrite anything.
  //
  // Note: In Internet Explorer for Windows Mobile, you can't add properties to
  // the window object. However, global objects are automatically added as
  // properties of the window object in all browsers.
  if (!window.google) {
    google = {};
  }

  if (!google.gears) {
    google.gears = {factory: factory};
  }
})();

//google gears
if (window.google && google.gears){
  document.getElementById('gears').innerHTML='You do support Google Gears I think... locating....';

  try{
    var geo = google.gears.factory.create('beta.geolocation');
    geo.getCurrentPosition(
      function(position) {
        var gi = new Image();
        gi.src='http://maps.google.com/staticmap?center='+ position.latitude +','+ position.longitude +'&zoom=13&size=240x240&maptype=mobile&markers='+ position.latitude +','+ position.longitude +',blue&key=ABQIAAAAqftbx1vhG0rS7scuEENxyRTRJtTXunJE4SLsqxZ4CG1yCz4wuxQfwSJJ_KX2yGZA1aYYtJFWbNkcpQ&sensor=false';
        document.getElementById('gears').innerHTML=position.gearsAddress.city + ', '+position.gearsAddress.country+'. Current lat/lon is: ' + position.latitude + ',' + position.longitude+'<br/>';
        document.getElementById('gears').appendChild(gi);
      }, 
      function (positionError) {
        document.getElementById('gears').innerHTML='Attempt to get location failed: ' + positionError.message;
      },
      { enableHighAccuracy: true, gearsRequestAddress: true }
    );
  }catch(e){
    alert(e);
  }
}else{
  document.getElementById('gears').innerHTML='No support for google gears';
}

//w3c spec
if(navigator.geolocation){
  document.getElementById('w3c').innerHTML='Seems like your browser supports geolocation... locating....';
  navigator.geolocation.getCurrentPosition(
    function(position) {  
      var wi = new Image();
      wi.src='http://maps.google.com/staticmap?center='+ position.coords.latitude +','+ position.coords.longitude +'&zoom=13&size=240x240&maptype=mobile&markers='+ position.coords.latitude +','+ position.coords.longitude +',blue&key=ABQIAAAAqftbx1vhG0rS7scuEENxyRTRJtTXunJE4SLsqxZ4CG1yCz4wuxQfwSJJ_KX2yGZA1aYYtJFWbNkcpQ&sensor=false';
      document.getElementById('w3c').innerHTML='Current lat/lon is: ' + position.coords.latitude + ',' + position.coords.longitude+'<br/>';
      document.getElementById('w3c').appendChild(wi);

    },function(error){
        document.getElementById('w3c').innerHTML='Sorry, could not find you... error code: '+error.code
      }
  );  
}else{
  document.getElementById('w3c').innerHTML='No support for w3c geolocation';
}

//ClientLocation
if (google.loader.ClientLocation) {
  document.getElementById('client').innerHTML='looks like you support ClientLocation... locating....';
  var ci = new Image();
  ci.src='http://maps.google.com/staticmap?center='+ google.loader.ClientLocation.latitude  +','+ google.loader.ClientLocation.longitude +'&zoom=13&size=240x240&maptype=mobile&markers='+ google.loader.ClientLocation.latitude  +','+ google.loader.ClientLocation.longitude +',blue&key=ABQIAAAAqftbx1vhG0rS7scuEENxyRTRJtTXunJE4SLsqxZ4CG1yCz4wuxQfwSJJ_KX2yGZA1aYYtJFWbNkcpQ&sensor=false';
  document.getElementById('client').innerHTML='Current lat/lon is: ' + google.loader.ClientLocation.latitude  + ',' + google.loader.ClientLocation.longitude+'<br/>';
  document.getElementById('client').appendChild(ci);
  
}else{
  document.getElementById('client').innerHTML='No support for ClientLocation';
}

//webwm
if (window.webvm) {
  document.getElementById('webwm').innerHTML='looks like you support webwm... locating....';
  var options = {};
  options.timeout = 30000;
  webvm.load('geolocation', { id: 'geolocation',
    successCallback: function() { geolocation.getCurrentPosition(
        function(position) {
          var gi = new Image();
          gi.src='http://maps.google.com/staticmap?center='+ position.latitude +','+ position.longitude +'&zoom=13&size=240x240&maptype=mobile&markers='+ position.latitude +','+ position.longitude +',blue&key=ABQIAAAAqftbx1vhG0rS7scuEENxyRTRJtTXunJE4SLsqxZ4CG1yCz4wuxQfwSJJ_KX2yGZA1aYYtJFWbNkcpQ&sensor=false';
          document.getElementById('webwm').innerHTML=position.gearsAddress.city + ', '+position.gearsAddress.country+'. Current lat/lon is: ' + position.latitude + ',' + position.longitude+'<br/>';
          document.getElementById('webwm').appendChild(gi);
        }, 
        function (positionError) {
          document.getElementById('webwm').innerHTML='Attempt to get location failed: ' + positionError.message;
        }
        , options); },
    errorCallback: function (positionError) {
          document.getElementById('webwm').innerHTML='Attempt to get location failed: ' + positionError.message;
        }
  });
}else{
  document.getElementById('webwm').innerHTML='No support for webwm';  
}

//locli
if (LokiAPI.isInstalled()) {
  document.getElementById('loki').innerHTML='Found Loki. locating...';  
  var loki = LokiAPI();
  loki.onSuccess = function(position) {
          var li = new Image();
          li.src='http://maps.google.com/staticmap?center='+ position.latitude +','+ position.longitude +'&zoom=13&size=240x240&maptype=mobile&markers='+ position.latitude +','+ position.longitude +',blue&key=ABQIAAAAqftbx1vhG0rS7scuEENxyRTRJtTXunJE4SLsqxZ4CG1yCz4wuxQfwSJJ_KX2yGZA1aYYtJFWbNkcpQ&sensor=false';
          document.getElementById('loki').innerHTML=position.street + ', '+position.city + ', '+position.country+'. Current lat/lon is: ' + position.latitude + ',' + position.longitude+'<br/>';
          document.getElementById('loki').appendChild(li);
        };
  loki.onFailure = function (positionError) {
          document.getElementById('loki').innerHTML='Attempt to get location failed: ' + positionError;
        };
  loki.setKey('mpulp.mobi/');
  loki.requestLocation(true,loki.FULL_STREET_ADDRESS_LOOKUP);
}else{
  document.getElementById('loki').innerHTML='No support for loki';  
}

//if bb. provided by Roland at http://www.mobiforge.com/developing/blog/location-location-location
  function locationCB()
  {
      var bi = new Image();
      bi.src='http://maps.google.com/staticmap?center='+ blackberry.location.latitude +','+ blackberry.location.longitude +'&zoom=13&size=240x240&maptype=mobile&markers='+ blackberry.location.latitude +','+ blackberry.location.longitude +',blue&key=ABQIAAAAqftbx1vhG0rS7scuEENxyRTRJtTXunJE4SLsqxZ4CG1yCz4wuxQfwSJJ_KX2yGZA1aYYtJFWbNkcpQ&sensor=false';
      document.getElementById('bb').innerHTML='Current lat/lon is: ' + blackberry.location.latitude + ',' + blackberry.location.longitude+'<br/>';
      document.getElementById('bb').appendChild(bi);
      return true;
  }
 
  if( window.blackberry && blackberry.location.GPSSupported) {
        blackberry.location.onLocationUpdate("locationCB()");
        blackberry.location.setAidMode(2);
        blackberry.location.refreshLocation();
  }    
  else
  {
    document.getElementById('bb').innerHTML='No BlackBerry location api available';  
  }

