
// Gmap Extras
// version 0.4 BETA!
// 2005-06-19
// Copyright (c) 2005, Matt King
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// IF YOU ARE UPGRADING FROM A PREVIOUS VERSION OF gmap_extras, go to
// Tools/Manage User Scripts and manually uninstall the previous
// version before installing this one.  Sorry, this is a limitation
// of Greasemonkey.
// 
// To uninstall, go to Tools/Manage User Scripts,
// select "gmap_extras", and click Uninstall.
//
// --------------------------------------------------------------------
//
// WHAT IT DOES:  Extra goodies for maps.googgle.com:
//
//   * Display curent lat/lon on title bar
//   * Bookmarks to save lat/lon locations (requires Greasemonkey 0.3+)
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name            gmap_extras
// @namespace       http://www.gnik.com/gmaps/
// @description     Gmap Extras!
// @include         http://maps.google.com/*
// ==/UserScript==

window.addEventListener('load', function() {

// Insert our HTML into the page
var allDivs, titleTD;
allDivs = document.evaluate(
                            "//td[@class='title']",
                            document,
                            null,
                            XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
                            null);
var found = false;
for (var i = 0; i < allDivs.snapshotLength; i++) {
  var center = _m.map.getCenterLatLng();
  var x = Math.round(center.x * 1000000) / 1000000.0;
  var y = Math.round(center.y * 1000000) / 1000000.0;
  var loc = x + " " + y;
  titleTD = allDivs.snapshotItem(i);
  var html = "<form id=\"latlonform\">Maps";
  //html += "<span sytle=\"font-size: medium; font-weight: normal; padding-left: 3px;white-space: nowrap;\">";
  html += " <span style=\"font-size: small; font-weight: normal;\">&nbsp; &nbsp; Center: <span id=\"curlatlon\">" + loc + "</span></span>";
  if (GM_setValue) {
    html += " <input type=\"button\" value=\"Save\" onClick=\"_gmap_extras.bookmarkAdd();\">";
    html += " &nbsp; &nbsp; <select name=\"bookmarks\"><option value=\"\">--bookmarks--</option>";
    var bCount = GM_getValue("b.count", 0);
    for (var j = 0; j < bCount; j++) {
      var value = GM_getValue("b." + j);
      var values = value.split("|");
      var name = values[0];
      var location = values[1];
      html += "<option value=\"" + location + "\">" + name + "</option>";
    }
    html += "</select>";  
    html += " <input type=\"button\" value=\"Goto\" onClick=\"_gmap_extras.bookmarkGoto();\">";
    html += " <input type=\"button\" value=\"Delete\" onClick=\"_gmap_extras.bookmarkDelete();\">";
  }
  //html += " <a href=\"#\" onClick=\"_gmap_extras.launch(); return false;\">L</a>";
  html += "</form>";
  //html += "</span>";
  titleTD.innerHTML = html;
  found = true;
}
 if (found == false) { return; }


// Window object to hold our stuff
window._gmap_extras = new Object();
window._gmap_extras.controlPanel = null;

window._gmap_extras.launch = function() {
  if (window._gmap_extras.controlPanel && 
      window._gmap_extras.controlPanel.document) {
    window._gmap_extras.controlPanel.focus();
    return;
  }
  var w = window.open("", "controlPanelWindow", "width=400,height=300");
  var d = w.document;

  d.write('<html><head>');
  d.write('</head><body>');
  d.write('<h2>Google Maps Control Panel</h2>');
  
  d.write('<div class=""><h3>Current location</h3>');
  d.write('</div>');

  d.write('<div class=""><h3>Bookmarks</h3>');
  d.write('</div>');

  d.write('</body></html>');
  d.close();


  window._gmap_extras.controlPanel = w;
}

window._gmap_extras.bookmarkDelete = function() {
  var form = document.getElementById("latlonform");
  if (!form || !GM_setValue) { return; }
  var sel = form.bookmarks;
  if (sel.selectedIndex == 0) return;
  var bId = sel.selectedIndex-1;
  var bCount = GM_getValue("b.count", 0);
  for (var i = bId; i < bCount-1; i++) {
    var nexti = i+1;
    var nextloc = GM_getValue("b."+nexti);
    GM_setValue("b."+i, nextloc);
  }
  GM_setValue("b."+(bCount-1), ""); 
  GM_setValue("b.count", bCount-1);
  sel.options[sel.selectedIndex] = null;
}

window._gmap_extras.bookmarkGoto = function() {
  var form = document.getElementById("latlonform");
  if (!form || !GM_setValue) { return; }
  var sel = form.bookmarks;
  if (sel.selectedIndex == 0) return;
  var location = sel.options[sel.selectedIndex].value;  
  var name = sel.options[sel.selectedIndex].text;  
  document.location.href = location;
}

window._gmap_extras.bookmarkAdd = function() {
  var form = document.getElementById("latlonform");
  if (!form || !GM_setValue) { return; }

  var location = _m.permalink.href;
  var name = prompt("Save this location as", _m.vpage.title);
  if (name == "") { alert("You must supply a name"); return; }
  var bCount = GM_getValue("b.count", 0);
  GM_setValue("b."+bCount, name + "|" + location);
  bCount += 1;
  GM_setValue("b.count", bCount);
  var option = new Option(name, location);
  var sel = form.bookmarks;
  sel.options[sel.options.length] = new Option(name, location);
  sel.selectedIndex = sel.options.length-1;
}

// Add a listener to update the lat/lon when the map is moved
_Event.addListener(_m.map, "moveend", 
  function () {
    var center = _m.map.getCenterLatLng();
    var x = Math.round(center.x * 1000000) / 1000000.0;
    var y = Math.round(center.y * 1000000) / 1000000.0;
    var span = document.getElementById("curlatlon");
    span.innerHTML = y + " " + x;
  });

}, true); // window.addEventListener

/*
TODO:
* Bookmarks of a location should remember that location (addr, company, etc)

CHANGELOG:
0.4 - 2005-06-19 - Bookmarks now save "link to this page" location,
                   got rid of lat/lon input box (can do this from main search
                   box).  Fixed bug with GM @include directive.
0.3 - 2005-06-18 - Make sure we're using a GM_setValue compat GM for bookmarks
0.2 - 2005-06-18 - Bookmarks
0.1 - 2005-06-17 - initial release
*/

// END FILE
