// greasemonkey script for adding a josm remote control link
// to Openstreetmap.org node/way/relation browser
//
// (c)2009 Sven Geggus <sven@geggus.net>
//
// Released under the GNU GENERAL PUBLIC LICENSE
// http://www.gnu.org/copyleft/gpl.html
//
// $Id: josm_remote.user.js 2 2009-10-12 10:39:33Z sven $
//
// ==UserScript==
// @name          josm remote
// @namespace     http://blog.gegg.us
// @description   add a josm remote control link to Openstreetmap.org node/way/relation browser
// @include       http://*.openstreetmap.org/browse/relation/*
// @include       http://*.openstreetmap.org/browse/way/*
// @include       http://*.openstreetmap.org/browse/node/*
// ==/UserScript==

var api_base_url="http://www.openstreetmap.org/api/0.6";

// get OSM object ID and type (node,way,relation) from URL
var arr=String(document.location).split("/");
var osm_obj_id=arr[arr.length-1];
var osm_obj_type=arr[arr.length-2];

// create OSM API URL for josm-remote plugin
if (osm_obj_type != "node") {
  var osm_api_url=api_base_url+"/"+osm_obj_type+"/"+osm_obj_id+"/full";
} else {     
  var osm_api_url=api_base_url+"/"+osm_obj_type+"/"+osm_obj_id;
}
 
josm_remote_url="http://localhost:8111/import?url="+osm_api_url;

// Search for the Edit link and replace it with two links

// get all document links
allLinks = document.evaluate(
    '//a[@href]',
    document,
    null,    
    XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
    null);
      
// iterate trough all the links and check if it matches
// the edit link (for node and way only)
if (osm_obj_type != "relation") {
  for (var i = 0; i < allLinks.snapshotLength; i++) {
    thisLink = allLinks.snapshotItem(i);
    if (String(thisLink).match(/\/edit\?/)) {
      // found the link

      // replace text of original edit link (Potlatch)
      edittxt=thisLink.innerHTML;
      thisLink.innerHTML = edittxt+' (Potlatch)';

      // add separator
      if (osm_obj_type == "node") {
        separator = document.createTextNode(", ");
      } else {
	separator = document.createTextNode(" | ");
      }
      thisLink.parentNode.insertBefore(separator,thisLink.nextSibling);

      // add new edit link (JOSM)
      var josmlink=document.createElement('a');
      josmlink.appendChild(document.createTextNode(edittxt+' (JOSM)'));
      josmlink.setAttribute("href", josm_remote_url);
      separator.parentNode.insertBefore(josmlink,separator.nextSibling);
      break;
    }
  }
// the relation browser does not have an edit link so we add the
// Edit in JOSM link after the history link
} else {
  for (var i = 0; i < allLinks.snapshotLength; i++) {
    thisLink = allLinks.snapshotItem(i);
    if (String(thisLink).match(/\/history/)) {
      // found the link

      // add <hr>
      thehr = document.createElement('hr');
      thisLink.parentNode.insertBefore(thehr, thisLink.nextSibling);

      // add new edit link (JOSM)
      var josmlink=document.createElement('a');
      josmlink.appendChild(document.createTextNode('Edit in JOSM'));
      josmlink.setAttribute("href", josm_remote_url);
      thehr.parentNode.insertBefore(josmlink,thehr.nextSibling);      

      break;
    }
  }
}

// we need to intersept the click event on our newly added edit anchor
// and call GM_xmlhttpRequest instead
document.addEventListener('click', function(event) {
    // event.target is the element that was clicked

    // do whatever you want here
    if (event.button==0) {
      if (event.target.href==josm_remote_url) {
        GM_xmlhttpRequest({
        url: josm_remote_url,
        method: "GET"
        });
        event.stopPropagation();
        event.preventDefault();
      };
   };
}, true);
