// ==UserScript==
// @name           Panoramio_Nuvi_Oregon
// @namespace      http://www.fun2code.de
// @include        http://www.panoramio.com/photo/*
// ==/UserScript==



// Image URL (includes title)
var imageTag = document.getElementById("main-photo").innerHTML;

var imageReg = /.*?src="(.*?)".*?alt="(.*?)"/;
var imageRes = imageReg.exec(imageTag);

var imageUrl = imageRes[1];
var title = imageRes[2];

// Get author
var authorTag = document.getElementById("author").innerHTML;
var authorReg = /.*?>(.*?)<\/a>/;
var authorRes = authorReg.exec(authorTag);
var author = authorRes[1];

// Get coordinates
var allLinks, thisLink, lon, lat;
allLinks = document.evaluate(
    '//a[contains(@href, "map/#lt=")]',
    document,
    null,
    XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
    null);

for (var i = 0; i < allLinks.snapshotLength; i++) {
    var coordReg = /.*?map\/#lt=(.*?)&ln=(.*?)&/;
    var coordRes = coordReg.exec(allLinks.snapshotItem(i));
    lat = coordRes[1];
    lon = coordRes[2];
}

var nuviText, oregonText;

if(navigator.language == "de") {
  nuviText = "F&uuml;r N&uuml;vi";
  oregonText = "F&uuml;r  Oregon";
}
else {
  nuviText = "For N&uuml;vi";
  oregonText = "For Oregon";
}

var location = document.getElementById('location');
if (location) {

   // Oregon
   var oregonHref = document.createElement("a");
    oregonHref.setAttribute("href", "http://www.fun2code.de/geotag/oregon_geotag/geotag.php?url=" + escape   (imageUrl) + "&lon=" + lon +"&lat=" + lat);
    oregonHref.setAttribute('style','margin-left: 5px;');
    oregonHref.innerHTML = oregonText;
    location.parentNode.insertBefore(oregonHref, location.nextSibling);

  var oregonImg = document.createElement("img");
  oregonImg.setAttribute("src", "http://www.fun2code.de/geotag/oregon_geotag/images/oregon_mini.png");
  oregonImg.setAttribute("align", "middle");
  oregonImg.setAttribute("style","margin-left: 15px;");
  location.parentNode.insertBefore(oregonImg, location.nextSibling);
  

  // Nuvi
  var nuviHref = document.createElement("a");
    nuviHref.setAttribute("href", "http://www.fun2code.de/geotag/nuvi_geotag/convert.php?url=" + escape(imageUrl) + "&name=" + escape(title) + "&author=" + escape(author) + "&lon=" + lon +"&lat=" + lat);
    nuviHref.setAttribute("style","margin-left: 5px;");
    nuviHref.innerHTML = nuviText;
    location.parentNode.insertBefore(nuviHref, location.nextSibling);

  var nuviImg = document.createElement("img");
  nuviImg.setAttribute("src", "http://www.fun2code.de/geotag/nuvi_geotag/images/nuvi_mini.png");
  nuviImg.setAttribute("align", "middle");
  nuviImg.setAttribute("style","margin-left: 5px;");
  location.parentNode.insertBefore(nuviImg, location.nextSibling);
}



