// ==UserScript==
// @name           FireEagleGoogleMaps
// @namespace      http://shamurai.com
// @description    Auto-set your location with Fire Eagle
// @include        http://maps.google.com/
// ==/UserScript==

// Loop quitely till everything is loaded.
window.waitForFireEagle = function() {  
  if (typeof window.FireWrench == 'undefined' ||
      window.FireWrench.IsReady() == false) {
    window.setTimeout(window.waitForFireEagle, 100);
  } else {
    doSomething();
  }
}

// if Fire Wrench is running, wait for it to load.
if (window.FireWrench)
  window.addEventListener('load', window.waitForFireEagle, false);

// FireWrench loaded, now do something.
window.doSomething = function() {
  var fe = window.FireWrench.GetInstance();
  var url = fe.getUserUrl('json');
  window.FireWrench.getHttpRequest('GET', url, showLocation);
}

// Call back for xmlhttp request.  Fill in the search box with our
// last location.
window.showLocation = function(responseDetails) {
  var r = responseDetails.responseText;
  var j = eval('(' + r + ')');
  if (j.user.location_hierarchy.length == 0) {
    return;
  }
  var s = j.user.location_hierarchy[0].name;	
  var inputQuery = document.getElementById('q_d');
  if (inputQuery.value.length == 0) {
    inputQuery.value = s;
    inputQuery.blur();
  }
}
