// ==UserScript==
// @name           GoogleMaps Geocache BackURL to geocaching.com
// @namespace      cache-test-dummies.de
// @description    Add a backlink to geocache-details from GMaps
// @include        http://maps.google.de/*
// ==/UserScript==
// Version:   1.2.0
// Date:      02.09.2007
// Modify:    16.07.2010
// Author:    Thomas Schneider
// Copyright: Thomas Schneider
// URL:       http://www.cache-test-dummies.de/
// Licence:   GPL, latest version

window.setInterval(show_gc_link, 2000);

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

function show_gc_link() {
	// inline functions
	function build_gc_link(title) {
    var wp_code = title.match(/^GC[\S]{1,}/);
	  if (wp_code) {
			// we have a valid geocache bubble
	    var newHTML = document.createElement('span');
		  newHTML.setAttribute("id","gc_com_detail");
		newHTML.style.marginRight="10px";
	    newHTML.innerHTML = "<a style=\"font-size: small;\" id=\"gc_com_detail_lnk\" href=\"http://www.geocaching.com/seek/cache_details.aspx?wp=" + wp_code +"\" title=\"Geocachedetails für " + wp_code + " bei Geocaching.com\" target=\"_gc_com_detail\">GC-Details</a>";
	    var gc_link = document.getElementById('gc_com_detail_lnk');
		 //	var kml_link = document.getElementById('view_kml');
		var rss_link = document.getElementById('view_rss');
		if ( ! gc_link ) {
 	 			// kml_link.parentNode.insertBefore(newHTML, kml_link);
				rss_link.parentNode.insertBefore(newHTML, rss_link);
      } else {
				// we have a link, check if this is the right link
				var gc_link_wp = gc_link.href.slice(53);
				if ( wp_code != gc_link_wp ) {
		      var my_gc_link = document.getElementById('gc_com_detail');
					if (my_gc_link) {
						// a geocache-detail-link is shown - delete this
						my_gc_link.parentNode.removeChild(my_gc_link);
					}
					// show the new link
	 	 			// kml_link.parentNode.insertBefore(newHTML, kml_link);	
					rss_link.parentNode.insertBefore(newHTML, rss_link);	
				}
			}
    } else {
			// don't have a valid geocache bubble
      var my_gc_link = document.getElementById('gc_com_detail');
			if (my_gc_link) {
				// a geocache-detail-link is shown - delete this
				my_gc_link.parentNode.removeChild(my_gc_link);
			}
		}
	}
	// end of inline-functions
  var title ="";
	var all_cont_xpath = "";
  var bold_rtf_btn = document.getElementById('tb_Bold_msrt');
	//unsafeWindow.console.info('me');
	//console.log("running"); 
  if (bold_rtf_btn) {
	// console.log("rtf mode"); 
		// edit mode rtf
	    //all_cont_xpath = xpath('//*[@class="title msinputnoborder"]');
	    all_cont_xpath = xpath('//*[@class="title"]');
		if (all_cont_xpath.singleNodeValue) {
      title = all_cont_xpath.singleNodeValue.value;
			build_gc_link(title);
  	}
	} else {
		// not rtf edit mode - check if a bubble is shown
		all_cont_xpath = xpath('//*[@class="msinfotitle"]');
		if (all_cont_xpath.singleNodeValue) {
			// a bubble is shown
      title = all_cont_xpath.singleNodeValue.innerHTML;
			build_gc_link(title);
		} else {
			// no bubble is shown - check if a gc-detail-link is shown
			var my_gc_link = document.getElementById('gc_com_detail');
			if (my_gc_link) {
				// a geocache-detail-link is shown - delete this				
				// unsafeWindow.console.info('gc_link exists ');
				my_gc_link.parentNode.removeChild(my_gc_link);
			}
		}		
	}
}

