// ==UserScript==
// @name           Camp Directions
// @namespace      http://www.leaky.org/
// @description    Gives directions to all 6 camps on Shartak
//                 Will take you to within 4 blocks of the Shaman for any camp.
// @include        http://www.shartak.com/game.cgi
// ==/UserScript==

/* Thanks to Murk's UBER MAPPER script for the GPS code */

var gpscoords = "";

var gpsformtags = document.evaluate("//form//input[@value='gps unit']//parent::form", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
if (gpsformtags.snapshotLength) {
  gpscoords = gpsformtags.snapshotItem(0).textContent.replace(/[\[\]\.\s\+]/ig,"");

  if (gpscoords != 'NoSignal') {

    var gpsxy = gpscoords.split(',');
    var msg = '<b>Camp Directions:</b><br><table><tr>';
    msg += add_bearing("Durham", -70648, 26343, gpsxy);
    msg += add_bearing("Dalpok", -70437, 26443, gpsxy);
    msg += '</tr><tr>';
    msg += add_bearing("York", -70371, 26314, gpsxy);
    msg += add_bearing("Raktam", -70323, 26470, gpsxy);
    msg += '</tr><tr>';
    msg += add_bearing("Derby", -70095, 26350, gpsxy);
    msg += add_bearing("Wiksik", -70239, 26405, gpsxy);
    msg += '</tr><tr>';
	msg += add_bearing("Shipwreck", -70541, 26546, gpsxy);
    msg += '</tr></table>';

    var buttonsdivtag = document.evaluate("//div[@class='buttons']", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    if (buttonsdivtag.snapshotLength == 1) {
      var status = document.createElement('div');
      buttonsdivtag.snapshotItem(0).parentNode.insertBefore(status, buttonsdivtag.snapshotItem(0).nextSibling);
      status.innerHTML = msg;
    }
  }
}

function add_bearing(name,x,y,coords) {
  var direction = '';

  if (coords[1] < (y-4)) {
     direction += 'north('+(y - coords[1])+') ';
  } else if (coords[1] > (y+4)) {
    direction += 'south('+(coords[1] - y)+') ';
/*  } else {
    direction += 'y:'+y+' gpsy:'+coords[1]; */
  }
  if (coords[0] < (x-4)) {
    direction += 'east('+(x-coords[0])+')';
  } else if (coords[0] > (x+4)) {
    direction += 'west('+(coords[0]-x)+')';
/*  } else {
    direction += 'x:'+x+' gpsx:'+coords[0]; */
  }
  if (direction == '') {
    direction += 'HERE';
  }
  return '<td>'+name+': '+direction+'</td>';
}
