// ==UserScript==
// @name           FireWrench Yahoo! Local
// @namespace      http://shamurai.com
// @description    Automatically fill in your city and state for Yahoo! Local Searches.
// @include        http://local.yahoo.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, fillInTheBlank);
}

// Call back for xmlhttp request.  Fill in the search box with our
// last location.
window.fillInTheBlank = 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 i = 0;
  while(j.user.location_hierarchy[i].level < 1) i++;
  var s = j.user.location_hierarchy[i].name;
	
  // this will pre-fill the set the search box
  // var inputQuery = document.getElementById('csz');
  // inputQuery.value = s;
  // inputQuery.blur();

  // but we really want the whole page to be set for our location for
  // the whole page
  var s = j.user.location_hierarchy[0].name;	 
  window.location = window.location + "?csz=" + escape(s);
}
