// A script which converts a users latitude/longitude location into address location
// version 0.2 BETA!
// 2008-08-03
// Copyright (c) 2008, Andrew Watts
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "Twitter latlon2address", and click Uninstall.
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name          Twitter latlon2address
// @namespace     http://www.thirstymind.org/
// @description   A script which converts a users iphone latitude/longitude location into address location  
// @include       http://twitter.com/*
// @include       http://*.twitter.com/*
// ==/UserScript==

var adr = document.evaluate('//span[@class="adr"]', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0);

if( adr.innerHTML.toLowerCase().indexOf('iphone') > -1 ){
    
    var latlng, lat, lng;
    
    latlng = adr.innerHTML.split(':')[1].split(',');
    
    lat = latlng[0];
    lng = latlng[1]; 
    
    GM_xmlhttpRequest({
       method: 'GET',
       url: 'http://ws.geonames.org/findNearestAddressJSON?lat='+lat+'&lng='+lng,
       headers: {
           'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
       },
       onload: function(response) {
           if( response.status == '200' ){
               var data = eval('(' + response.responseText + ')');
               if(data.address){
                   adr.innerHTML = data.address.placename + ', ' + data.address.adminName1 + ', ' + data.address.countryCode;
               }
           }
       }
    });
}

