// ==UserScript==
// @name          Gmail Select Conversations and Replies
// @author        Raffles
// @namespace     http://ratherodd.com/
// @description   Adds two items to the end of the "Select:" collection (after Starred and Unstarred): Conversations (selects conversations) and Replies (select messages with Re: in the subject line)
// @include       http*://mail.google.tld/*
// ==/UserScript==
window.addEventListener('load', function() {  
  if (unsafeWindow.gmonkey) {
    unsafeWindow.gmonkey.load('1.0', function(gmail) {
      var tl, win = window.top.document.getElementById('canvas_frame').contentWindow;
      function addLinks() {
        if (gmail.getActiveViewType() !== 'tl') return;
        tl = gmail.getActiveViewElement();
        var selectors = win.document.evaluate('//div[starts-with(text(), "Select:")]', tl, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
        if (selectors.snapshotLength > 0) {
          for (var j = 0; j < selectors.snapshotLength; j++) {
            var selector = selectors.snapshotItem(j);
            if (selector.lastChild.childNodes.length > 11) continue;
            var items = {'Conversations': document.createElement('span'), 'Replies': document.createElement('span')};
            for (var i in items) {
              items[i].appendChild(document.createTextNode(i));
              items[i].setAttribute('selector', i.toLowerCase());
              selector.lastChild.appendChild(document.createTextNode(', '));
              selector.lastChild.appendChild(items[i]);
              items[i].addEventListener('click', selectItems, false);
            }
          }
        }
      }
      function selectItems() {
        var xp = this.getAttribute('selector') === 'conversations' ?
          '//tr/td[child::div/text()[last()][starts-with(., " (")]]/preceding-sibling::td//input[@type="checkbox"]' :
          '//tr/td[child::div/div/div/span/text()[starts-with(., "re:") or starts-with(., "Re:") or starts-with(., "RE:")]]/preceding-sibling::td//input[@type="checkbox"]'
        var checkboxes = win.document.evaluate(xp, tl.getElementsByTagName('table')[0], null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
        var chlen = checkboxes.snapshotLength;
        if (!chlen) return;
        var scrol = win.pageYOffset;
        win.document.body.style.position = 'fixed';
        for (var i = 0; i < chlen; i++) {
          checkboxes.snapshotItem(i).click();
        }
        win.document.body.style.position = '';
        win.scroll(0,scrol);
      }
      gmail.registerViewChangeCallback(addLinks);
      addLinks();
    });
  }
}, false);