/*
 * 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');
var request = require('browser-request');

function queryTurbo(query)
{
    var url = "http://overpass-api.de/api/interpreter";

    request({method : 'POST', url : url, body : query, json : true},
            function(er, response, body) {
                if (er)
                    throw er;

                var element = body['elements'][0];
                var city = element['tags']['addr:city'];
                var housenumber = element['tags']['addr:housenumber'];
                var postcode = element['tags']['addr:postcode'];
                var street = element['tags']['addr:street'];
                var addr =
                    postcode + ' ' + city + ', ' + street + ' ' + housenumber;

                // Have the address, now talk to nominatim to get the
                // coordinates as well.
                queryNominatim(addr, city, street, housenumber)
            });
}

function queryNominatim(addr, city, street, housenumber)
{
    var url = "http://nominatim.openstreetmap.org/search.php";
}

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;';
    queryTurbo(query);
}

// 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:

@@ -239,16 +239,66 @@
 eady');%0A
+var querystring = require('querystring-browser');%0A
 var requ
@@ -435,14 +435,16 @@
 st(%7B
+'
 method
+'
  : '
@@ -454,19 +454,21 @@
 T', 
+'
 url
+'
  : url, 
 body
@@ -463,20 +463,22 @@
  : url, 
+'
 body
+'
  : query
@@ -479,20 +479,22 @@
  query, 
+'
 json
+'
  : true%7D
@@ -1317,17 +1317,530 @@
 arch.php
-%22
+?%22;%0A    url += querystring.stringify(%0A        %7B'q' : housenumber + ' ' + street + ', ' + city, 'format' : 'json'%7D);%0A    request(%7B'method' : 'GET', 'url' : url, 'json' : true%7D,%0A            function(er, response, body) %7B%0A                if (er)%0A                    throw er;%0A%0A                var element = body%5B0%5D;%0A                var lat = element%5B%22lat%22%5D;%0A                var lon = element%5B%22lon%22%5D;%0A%0A                // Show the result.%0A                var result = lat + ',' + lon + ' (' + addr + ')';%0A            %7D)
 ;%0A%7D%0A%0Afun

query nominatim