// ==UserScript==
// @name           EnhanceGCMaps
// @namespace      EnhanceGCMaps
// @description    Zeigt einen direktlink zu GoogleMaps in der Cachebeschreibung
// @include        http://www.geocaching.com/seek/cache_details.aspx*
// ==/UserScript==

// Pushpins:  -ylw -grn -red -wht -blue -pink -purple -ltblu
// http://maps.google.com/mapfiles/kml/pushpin/

//http://maps.google.com/mapfiles/kml/paddle/C.png

//--------------------------------------------------------
// The Think-Stuff
//--------------------------------------------------------

// Beschafft das erste auf den �bergebenen XPath passende Element
function getXEle(path) {
  var result=document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue;
  return result;
}

function getLat(ko) {
  koLaNS=ko.substring(0,1);
  koLaDeg=ko.substring(2,4);
  koLaMin=ko.substring(6,12);
  gKoLaDeg=""+((koLaNS == "N"?1:-1)*((1*koLaDeg)+(koLaMin/60)));
  var i=gKoLaDeg.indexOf(".");
  if (i+7 < gKoLaDeg.length) {
     gKoLaDeg=gKoLaDeg.substring(0,i+7);
  }
  return gKoLaDeg;
}

function getLng(ko) {
  var koLoNS=ko.substring(13,14);
  var koLoDeg=ko.substring(15,18);
  var koLoMin=ko.substring(20,26);
  var gKoLoDeg=""+((koLoNS == "E"?1:-1)*((1*koLoDeg)+(koLoMin/60)));
  var i=gKoLoDeg.indexOf(".");
  if (i+7 < gKoLoDeg.length) {
     gKoLoDeg=gKoLoDeg.substring(0,i+7);
  }
  return gKoLoDeg;
}

// Read Coords from Listing
latlon=document.getElementById("ctl00_ContentBody_LatLon");

// Found Coords? Go on and split them
if (null != latlon) {

  // First revert effects of KOUtil
  if (latlon.childNodes[0].nodeValue.length<6) {
    KOs=latlon.childNodes[1].nodeValue
  } else {
    KOs=latlon.childNodes[0].nodeValue
  }
  
  var gKoLaDeg=getLat(KOs);
  var gKoLoDeg=getLng(KOs);

  // Waypoints :)
  wps=getXEle("//strong[contains(text(),'Way')]/following-sibling::*/following::table[1]");
  waypoints="";

  if (null!=wps && wps.childNodes.length>0) {
    wps=wps.childNodes[3];
    wpidx=0;
    for (i=1;i<wps.childNodes.length-1;i+=4) {
      var wpko = wps.childNodes[i].childNodes[11].childNodes[0].textContent;
      var ico = wps.childNodes[i].childNodes[3].childNodes[0].getAttribute("src");
      var name = wps.childNodes[i].childNodes[9].childNodes[0].textContent;
      name = name.replace(/ /g, "+");
      ico=ico.substring(ico.lastIndexOf("/")+1,ico.lastIndexOf("."));
      if (wpko.length<6 && wpko.indexOf("???")==-1) {
         wpko = wps.childNodes[i].childNodes[11].childNodes[1].textContent;
      }
      if (wpko.indexOf("???")==-1) {
            waypoints+="&wp"+wpidx+"="+getLat(wpko)+","+getLng(wpko)+","+ico+","+name;
            wpidx++;
      }
    }
  }


  // Urls for embedded Maps

  //urlembed="http://rflexor.cachezentrale.de/gcgmplugin2.php?lat="+gKoLaDeg+"&lng="+gKoLoDeg;
  urlembed="http://rflexor.cachezentrale.de/gcgmplugin2.php?lat="+gKoLaDeg+"&lng="+gKoLoDeg+waypoints;
  urlembed2="http://rflexor.cachezentrale.de/gcgmplugin.php?small=1&lat="+gKoLaDeg+"&lng="+gKoLoDeg;

//--------------------------------------------------------

  // Substitution of stupid yahoo-embed


  iframe=document.createElement("iframe");
  ifwidth=document.createAttribute("width");
  ifwidth.nodeValue="350";
  ifheight=document.createAttribute("height");
  ifheight.nodeValue="350";
  ifframeborder=document.createAttribute("frameborder");
  ifframeborder.nodeValue="0";
  ifscrolling=document.createAttribute("scrolling");
  ifscrolling.nodeValue="no";
  ifmarginheight=document.createAttribute("marginheight");
  ifmarginheight.nodeValue="0";
  ifmarginwidth=document.createAttribute("marginwidth");
  ifmarginwidth.nodeValue="0"
  ifclass=document.createAttribute("class");
  ifclass.nodeValue="fr"
  ifsrc=document.createAttribute("src");
  ifsrc.nodeValue=urlembed;
  ifdummy=document.createTextNode("Your browser isn't iframe-compatible, sorry!");
  iframe.appendChild(ifdummy);

  iframe.setAttributeNode(ifwidth);
  iframe.setAttributeNode(ifheight);
  iframe.setAttributeNode(ifframeborder);
  iframe.setAttributeNode(ifscrolling);
  iframe.setAttributeNode(ifmarginheight);
  iframe.setAttributeNode(ifmarginwidth);
  iframe.setAttributeNode(ifclass);
  iframe.setAttributeNode(ifsrc);


window.setTimeout(function() {
  yahoo=document.getElementById("ctl00_ContentBody_uxlrgMap");
  yahoo.parentNode.replaceChild(iframe, yahoo);
  }, 2000);


//--------------------------------------------------------

  // Substitution of stupid yahoo-embed-small


  iframe2=document.createElement("iframe");
  if2width=document.createAttribute("width");
  if2width.nodeValue="200";
  if2height=document.createAttribute("height");
  if2height.nodeValue="180";
  if2frameborder=document.createAttribute("frameborder");
  if2frameborder.nodeValue="0";
  if2scrolling=document.createAttribute("scrolling");
  if2scrolling.nodeValue="no";
  if2marginheight=document.createAttribute("marginheight");
  if2marginheight.nodeValue="0";
  if2marginwidth=document.createAttribute("marginwidth");
  if2marginwidth.nodeValue="0"
  if2class=document.createAttribute("class");
  if2class.nodeValue="fr"
  if2src=document.createAttribute("src");
  if2src.nodeValue=urlembed2;
  if2dummy=document.createTextNode("Your browser isn't iframe-compatible, sorry!");
  iframe2.appendChild(if2dummy);

  iframe2.setAttributeNode(if2width);
  iframe2.setAttributeNode(if2height);
  iframe2.setAttributeNode(if2frameborder);
  iframe2.setAttributeNode(if2scrolling);
  iframe2.setAttributeNode(if2marginheight);
  iframe2.setAttributeNode(if2marginwidth);
  iframe2.setAttributeNode(if2class);
  iframe2.setAttributeNode(if2src);

  window.setTimeout(function() {

    yahoosml=document.getElementById("lnkSmallMap");
    yahoosml.parentNode.replaceChild(iframe2, yahoosml);
  }, 4000);
  

}


