/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

var domready = require('domready');

function osmify()
{
    var url = document.getElementById('url-input').value;
    var tokens = url.split('/');
    // E.g. node or way.
    objectType = tokens[tokens.length - 2];
    // Numeric ID.
    var objectId = tokens[tokens.length - 1];

    // Turn the ID into an address.
    var query = '[out:json];\n(\n    ' + objectType + '(' + objectId +
                ');\n);\nout body;';
    // TODO query overpass.
}

// Allow calling this from the button event handler.
window.osmify = osmify;

/// Look up name as a key in the query string.
function getParameterByName(name)
{
    name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
    var regex = new RegExp('[\\?&]' + name + '=([^&#]*)'),
        results = regex.exec(location.search);
    return results === null ? '' : results[1].replace(/\+/g, ' ');
}

domready(function() {
    var url = getParameterByName('url');
    if (url)
    {
        var urlInput = document.getElementById('url-input');
        urlInput.value = url;
    }
});

// vim: shiftwidth=4 softtabstop=4 expandtab:

@@ -238,16 +238,1041 @@
 ready');
+%0Avar request = require('browser-request');%0A%0Afunction queryTurbo(query)%0A%7B%0A    var url = %22http://overpass-api.de/api/interpreter%22;%0A%0A    request(%7Bmethod : 'POST', url : url, body : query, json : true%7D,%0A            function(er, response, body) %7B%0A                if (er)%0A                    throw er;%0A%0A                var element = body%5B'elements'%5D%5B0%5D;%0A                var city = element%5B'tags'%5D%5B'addr:city'%5D;%0A                var housenumber = element%5B'tags'%5D%5B'addr:housenumber'%5D;%0A                var postcode = element%5B'tags'%5D%5B'addr:postcode'%5D;%0A                var street = element%5B'tags'%5D%5B'addr:street'%5D;%0A                var addr =%0A                    postcode + ' ' + city + ', ' + street + ' ' + housenumber;%0A%0A                // Have the address, now talk to nominatim to get the%0A                // coordinates as well.%0A                queryNominatim(addr, city, street, housenumber)%0A            %7D);%0A%7D%0A%0Afunction queryNominatim(addr, city, street, housenumber)%0A%7B%0A    var url = %22http://nominatim.openstreetmap.org/search.php%22;%0A%7D
 %0A%0Afuncti
@@ -1663,31 +1663,26 @@
     
-// TODO query overpass.
+queryTurbo(query);
 %0A%7D%0A%0A

query overpass