// ==UserScript==
// @name           geocaching.com + Google maps | Hiking.sk | Freemap.sk | Amapy.cz
// @namespace      http://gc.risko.org/rozne/gc_skripty/
// @description    Pridá Google mapu, Google satelitnú mapu, Google terénnu mapu, turistickú mapu z hiking.sk, freemap.sk, Amapy.cz. Naviac možnosť mapy disablovať (nie v Chrome).
// @include        *geocaching.com/map*
// @version        1.2.0
// ==/UserScript==

if (navigator.userAgent.indexOf("Chrome") != -1) 
{
  var script = document.createElement('script');
  script.setAttribute("type", "application/javascript");

  script.textContent  = '';
  script.textContent += 'L.TileLayer.TileCache=L.TileLayer.extend({getTileUrl:function(a,b){function e(a,b){a=String(a);var c=[];for(var d=0;d<b;++d)c.push("0");return c.join("").substring(0,b-a.length)+a}var c=this.options.subdomains,d=(a.x+a.y)%c.length;s=this.options.subdomains[d];var f=this._getOffsetZoom(b);var g=e(parseInt(a.x/1e6),3)+"/"+e(parseInt(a.x/1e3)%1e3,3)+"/"+e(parseInt(a.x)%1e3,3);var h=Math.pow(2,f)-1-a.y;var i=e(parseInt(h/1e6),3)+"/"+e(parseInt(h/1e3)%1e3,3)+"/"+e(parseInt(h)%1e3,3);var j=L.Util.template(this._url,L.Util.extend({s:s,z:f,x:g,y:i},this.options));return j}});';
  script.textContent += 'Groundspeak.Map.MapLayers.unshift(';
  script.textContent += '{tileUrl:"http://mt.google.com/vt?&x={x}&y={y}&z={z}",name:"googlemaps",alt:"Google maps",attribution:"Google maps",subdomains:"1234",tileSize:256,minZoom:0,maxZoom:22},';
  script.textContent += '{tileUrl:"http://mt.google.com/vt?lyrs=y&x={x}&y={y}&z={z}&scaleControl=true",name:"googlemapshyb",alt:"Google hybrid",attribution:"Google hybrid",subdomains:"1234",tileSize:256,minZoom:0,maxZoom:22},';
  script.textContent += '{tileUrl:"http://mt.google.com/vt?lyrs=s&x={x}&y={y}&z={z}&scaleControl=true",name:"googlemapssat",alt:"Google satelit",attribution:"Google satelit",subdomains:"1234",tileSize:256,minZoom:0,maxZoom:22},';
  script.textContent += '{tileUrl:"http://mt.google.com/vt?lyrs=p&x={x}&y={y}&z={z}",name:"googlemapssat",alt:"Google terrain",attribution:"Google terrain",subdomains:"1234",tileSize:256,minZoom:0,maxZoom:22},';
//  script.textContent += '{tileUrl:"http://mapy.hiking.sk/layers/tzt/{z}/{x}/{y}.png",layerType:"TileCache",name:"hiking_tzt",alt:"hiking.sk",attribution:"hiking",tileSize:256,minZoom:13,maxZoom:15},';
  script.textContent += '{tileUrl:"http://b.freemap.sk/data/layers/presets/T/{z}/{x}/{y}.jpeg",name:"freemapsk",alt:"Freemap SK",attribution:"FreeMapSk",tileSize:256,minZoom:0,maxZoom:16},';
  script.textContent += '{tileUrl:"http://b.freemap.sk/data/layers/presets/C/{z}/{x}/{y}.jpeg",name:"freemapskc",alt:"Freemap Cyklo",attribution:"FreeMapSkc",tileSize:256,minZoom:0,maxZoom:16},';
  script.textContent += '{tileUrl:"http://maps11.i0.cz/mps/ch_turis/{z}/{x}/{x}_{y}.gif",name:"amapy",alt:"Amapy turistická",attribution:"Amapy turistická",tileSize:256,minZoom:13,maxZoom:16}';
  script.textContent += ');';
  script.textContent += 'for (var a = {}, b = [], c = 0, d = Groundspeak.Map.MapLayers.length; c < d; c++)';
  script.textContent += '{';
  script.textContent += '  var e = Groundspeak.Map.MapLayers[c];';
  script.textContent += '  g = (e.layerType == "TileCache") ? new L.TileLayer.TileCache(e.tileUrl, e) : new L.TileLayer(e.tileUrl, e);';
  script.textContent += '  b.push(g);';
  script.textContent += '  a[e.alt] = g;';
  script.textContent += '}';
  script.textContent += 'a = new L.Control.Layers(a);';
  script.textContent += 'MapSettings.Map.removeLayer(MapSettings.Map._layers[1]);';
  script.textContent += '$("a[title=Layers]").parent().remove();';
  script.textContent += 'MapSettings.Map.addControl(a);';
  script.textContent += '$(a._form.getElementsByTagName("input")[0]).click();';
  
  script.textContent += 'MapSettings.Map.on(';
  script.textContent += '  "layeradd", ';
  script.textContent += '  function(e)';
  script.textContent += '  {';
  script.textContent += '    if (e.layer.options.name == "hiking_tzt")';
  script.textContent += '    {';
  script.textContent += '      hiking_map = {tileUrl:"http://mapy.hiking.sk/layers/topo/{z}/{x}/{y}.png",name:"hiking",alt:"hiking.sk",attribution:"hiking",tileSize:256,minZoom:13,maxZoom:15};';
  script.textContent += '      hiking_layer = new L.TileLayer(hiking_map.tileUrl,hiking_map);';
  script.textContent += '      MapSettings.Map.addLayer(hiking_layer, true);';
  script.textContent += '    }';
  script.textContent += '  }';
  script.textContent += ');';
  
  script.textContent += 'MapSettings.Map.on(';
  script.textContent += '  "layerremove", ';
  script.textContent += '  function(e)';
  script.textContent += '  {';
  script.textContent += '    if (e.layer.options.name == "hiking_tzt") { MapSettings.Map.removeLayer(hiking_layer); }';
  script.textContent += '  }';
  script.textContent += ');';

  document.body.appendChild(script); 
}
else
{
}

// make hiddne iframe with hiking.sk :-)
//ifrm = document.createElement("IFRAME"); 
//ifrm.setAttribute("src", "http://mapy.hiking.sk/"); 
//ifrm.style.display = "none"
//document.body.appendChild(ifrm);
