// ==UserScript==
// @name           Groundspeak_Nuvi_Oregon
// @namespace      http://www.fun2code.de
// @include        http://www.geocaching.com/seek/cache_details.aspx*
// ==/UserScript==

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

var authorReg = /by\s*<a.*?;ds=2">\s*(.*?)<\/a>/;
var authorRes = authorReg.exec( document.body.innerHTML);
var author = authorRes[1];

// Coordinates
var coordinatesTag = document.getElementById("ctl00_ContentBody_LatLon").innerHTML;
var coordinatesReg = /^([NS]\s[0-9]+).(\s.*?)\s([EW]\s[0-9]+).(\s.*?)$/;
var coordinatesRes = coordinatesReg.exec(coordinatesTag);

if(coordinatesRes) {
	var lat = coordinatesRes[1] + coordinatesRes[2];
	var lon = coordinatesRes[3] + coordinatesRes[4];
	
	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";
	}
	
	// Loop all images
	var allLinks, thisLink, lon, lat;
	allLinks = document.evaluate(
		'//a[contains(@href, "img.geocaching.com/cache/")]',
		document,
		null,
		XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
		null);

	for (var i = 0; i < allLinks.snapshotLength; i++) {
		var imageUrl = allLinks.snapshotItem(i);
		
		var titleReg = />\s*(.*?)\s*$/;
		var titleRes = titleReg.exec(allLinks.snapshotItem(i).innerHTML);
		var  title = titleRes[1];

		
		// 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;
		allLinks.snapshotItem(i).parentNode.insertBefore(oregonHref, allLinks.snapshotItem(i).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;");
		allLinks.snapshotItem(i).parentNode.insertBefore(oregonImg, allLinks.snapshotItem(i).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;
		allLinks.snapshotItem(i).parentNode.insertBefore(nuviHref, allLinks.snapshotItem(i).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;");
		allLinks.snapshotItem(i).parentNode.insertBefore(nuviImg, allLinks.snapshotItem(i).nextSibling);
	}
}
