// ==UserScript==
// @name           geocache-export-to-gmaps
// @namespace      http://www.cache-test-dummies.de
// @description    Export some geocache data to Google Maps
// @include        http://www.geocaching.com/*
// ==/UserScript==
// Version:   1.5.1
// Date:      27.05.2011
// Author:    Thomas Schneider
// Copyright: Thomas Schneider
// URL:       http://www.cache-test-dummies.de/2007/08/22/geocache-export-zu-googlemaps/
// Licence:   GPL, latest version

// the basic xpath expressions and defaults
var maps_xpath = '//a[starts-with(@href, "http://maps.google") and starts-with(text(), "Google Maps")]';
var gcname_xpath = '//*[@id="ctl00_ContentBody_CacheName"]';
var gccode_xpath = '//*[@id="ctl00_ContentBody_uxWaypointName"]';
var paragr_style = 'font-size: 12px; margin-right: 10px;'

function xpath(xpath, old) {
  if (typeof old == "undefined") {
    old = null;
  }
  return document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, old);
}

// clean up some name
function format_name (gc_name) {
  var n_name = gc_name.replace(/&amp;/ig, "&");
	n_name = n_name.replace(/\(/g, "[");
	n_name = n_name.replace(/\)/g, "]");
	n_name = n_name.replace(/\snach\s/g, " n. ");
  n_name = encodeURIComponent(n_name);
  return n_name;
}

produce_export();

function produce_export() {
	// get the google maps link
	var gc_cont_xpath = xpath(maps_xpath, gc_cont_xpath);
	if (gc_cont_xpath.singleNodeValue) {
		//unsafeWindow.console.info(gc_cont_xpath.singleNodeValue);
		var gc_name;
		var gc_latlon_href;
		var maplink = gc_cont_xpath.singleNodeValue;
		//unsafeWindow.console.info(gc_cont_xpath.singleNodeValue);
		maplink.setAttribute('class', 'ctd_export');
		gc_latlon_href = maplink.href;	  
		gc_latlon_href = gc_latlon_href.replace("maps.google.com","maps.google.de");
		gc_latlon_href = gc_latlon_href.replace(/q=.*%40/gi,"q=");		// entfernt "GCxxx@" aus GM-Link
		gc_latlon_href = gc_latlon_href.replace(/\+\(.*\)\+/gi,"+");	// entfernt "+(GCxxx)+" aus GM-Link
		gc_cont_xpath = xpath(gcname_xpath);
		if (gc_cont_xpath.singleNodeValue) {
			gc_cont_xpath.singleNodeValue.setAttribute('class', 'ctd_export');
			gc_name = format_name(gc_cont_xpath.singleNodeValue.innerHTML);
			gc_cont_xpath = xpath(gccode_xpath, gc_cont_xpath);
			//unsafeWindow.console.info(gc_cont_xpath.singleNodeValue);
			if (gc_cont_xpath.singleNodeValue) {
				// gc_cont_xpath.singleNodeValue.setAttribute('class', 'ctd_export');
				var gc_code = gc_cont_xpath.singleNodeValue.innerHTML;
				gc_code = gc_code.replace(/ */gi,"");					// entfernt Überschüssige Leerzeichen
				//unsafeWindow.console.info(gc_code);
				var link = document.createElement('a');
				link.setAttribute('href', gc_latlon_href + '(' + gc_code + ' - ' + gc_name + ')');
				link.setAttribute('target', '_mymaps');
				link.setAttribute('title', 'Export zu Google Maps (Neues Fenster)');
				link.setAttribute('id', 'gmexportlink');
				link.style.textDecoration="none";
				link.innerHTML="GMaps";
				var paragr = document.createElement('span');
				paragr.setAttribute('style', paragr_style);
				paragr.setAttribute('id', 'gmexportspan');
				paragr.setAttribute('class', 'ctd_export');
				paragr.appendChild(link);
				//gc_cont_xpath.singleNodeValue.parentNode.replaceChild(paragr,gc_cont_xpath.singleNodeValue);
				gc_cont_xpath.singleNodeValue.parentNode.parentNode.appendChild(paragr,gc_cont_xpath.singleNodeValue);
			}
		}
	}
}

