// ==UserScript==
// @name          Reddit Navigational Access Keys (0.1)
// @namespace     http://pablotron.org/
// @description   Add navigational access keys (NAK) to Reddit.  By default, previous is alt-comma and next is alt-period.
// @include       http://*reddit.com/*
// ==/UserScript==

(function () {
  var RedditNAK = {
    // enable debugging output?
    debug: false,

    // list of key matches
    keys : [
      { key: ',', regex: /prev$/ },
      { key: '.', regex: /^next/ },
    ],

    /*
     * xpath - return the elements matching the given XPath query.
     */
    xpath: function (path) {
      return document.evaluate(
        path, 
        document, 
        null, 
        XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, 
        null
      );
    },

    /*
     * init - add list of accesskeys to anchor with specific title
     */
    init: function (key_matches) {
      var i, j, e, m, links, links_len, key_matches_len;

      // get size of key matches array
      key_matches_len = key_matches.length;

      // get links 
      links = this.xpath("//p[@class='menu']/a");

      // add access key to each link
      links_len = links.snapshotLength;
      for (i = 0; i < links_len; i++) {
        e = links.snapshotItem(i);

        // iterate over key matches and check each one; if the regex
        // matches the element, then apply the associated access key
        for (j = 0; j < key_matches_len; j++) {
          m = key_matches[j];

          if (e.innerHTML.match(m.regex))
            e.setAttribute('accesskey', m.key);
        }
      }

      // return number of links munged
      return links_len;
    },
  };

  // add links
  var num_keys = RedditNAK.init(RedditNAK.keys);

  // if debugging is enabled, then print out result message
  if (RedditNAK.debug) 
    alert('Reddit-NAK Debug: keys = ' + num_keys);
})();

