// ==UserScript==
// @name           Show ffffound Links
// @namespace      http://husk.org/code/
// @description    Uses machine tags to show where an image is ffffound
// @include        http://*flickr.com/photos/*/*
// @exclude        http://*flickr.com/photos/organize*
// ==/UserScript==

display = function display(ffffound_ids) {
  var html = "<br/>"
  for (var index = 0; index < ffffound_ids.length; index++) {
    ffffound_id = ffffound_ids[index];

    html +=  '<a href="http://ffffound.com/image/'+ffffound_id+'">';
    html += '<img align="left" alt="Posted to ffffound" src="http://husk.org/images/ffffound.gif"/>';
    html += '</a> Posted to ';
    html += '<a class="Plain" href="http://ffffound.com/image/'+ffffound_id+'">ffffound</a><br/>';
  }
 
  var privacy = document.evaluate(
                   "//p[@class='Privacy']",
                   document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null
                ).singleNodeValue;
  privacy.innerHTML += html;
}     

find_in_list = function find_in_list(links, regex) {
  ids = []

  this.match = false;
  for(var i = 0; i < links.length; i++) {  
    link = links[i];
    matches = link.href.match(regex);
    if (matches) {
      ids.push(matches[1]);
    }
  }

  return ids;
}

// main
var f_machinetags = document.getElementById('themachinetags')
var f_links = f_machinetags.getElementsByClassName('globe'); 
// gets only some hrefs, but not duplicates

if (f_links.length > 0) {
  // first find trip id: we can use this without a location if necessary
  ffffound_ids = find_in_list(f_links, 'ffffound%3Aid%3D([0-9a-f]+)');
  if (ffffound_ids) {
    display(ffffound_ids);
  }
}

