// ==UserScript==
// @name           Show Flickr Contact Context
// @namespace      http://husk.org/code/
// @description    Show links to your Flickr contacts on photo pages
// @include        http://*flickr.com/photos/*/*
// @exclude        http://*flickr.com/photos/organize*
// ==/UserScript==

// Version 0.01
// Copyright (c) Paul Mison
// GPL licenced: http://www.gnu.org/copyleft/gpl.html

var api_key    = "58b056f621f6476ebf144f9f72a4d05c"
var flickr_url = "http://api.flickr.com/services/rest/?method=flickr.places.resolvePlaceId&api_key="+api_key+"&format=json&nojsoncallback=1&"

display = function display() {
  var placename = "on Dopplr";

  if (display.arguments.length > 0) {
    responseDetails = display.arguments[0];
    json = eval('('+responseDetails.responseText+')');
    if (json.location.locality._content) {
      placename = "to "+json.location.locality._content;
    } else if (json.location.county._content) {
      placename = "to "+json.location.county._content;
    }
  }

  var html =  '<br/><a href="http://dopplr.com/trip/id/'+trip_id+'">';
  html += '<img align="left" alt="Taken during a Dopplr trip" src="http://snaptrip.appspot.com/images/dopplr.png"/>';
  html += '</a> Taken during a ';
  html += '<a class="Plain" href="http://dopplr.com/trip/id/'+trip_id+'">trip '+placename+'</a>.<br/>';

  var privacy = document.evaluate(
                   "//p[@class='Privacy']",
                   document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null
                ).singleNodeValue;
  privacy.innerHTML += html;
}     

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

  return this.match;
}

// main
console.log("running Flickr context script");

console.log("r1: "+window.referer);
console.log("r2: "+window.referrer);
