// ==UserScript==
// @name          EasyNews Enhancer
// @description   Enhance various aspects of EasyNews searches
// @namespace     http://www.gozer.org/mozilla/greasemonkey/
// @include       http://members.easynews.com/*
// @include       https://secure.members.easynews.com/*
// ==/UserScript==

// --- miscellaneous -----------------------------------------------------------

/* disable getFocus() onload handler */
if (document.body.getAttribute('onload') == 'getFocus()')
  document.body.setAttribute('onload', 'return false;');

if (document.location.href.indexOf('.com/global4/search.html') != -1)
  for (var i = 0; i < document.forms.length; i++) {
    /* disable check_submit() onsubmit form handler */
    if (document.forms[i].name == 'global')
      document.forms[i].removeAttribute('onsubmit');

    /* remove target for zip manager form */
    if (document.forms[i].name == 'zip')
      document.forms[i].removeAttribute('target');
  }

/* add a generic onclick handler */
document.addEventListener('click',
  function(event)
  {
    var node = event.target;

    if (node.nodeName == 'A') {
      /* remove link targets */
      node.removeAttribute('target');

      /* open a popup for audio samples */
      if (node.innerHTML == '[sample]') {
        window.open(node.href, '', 'toolbar=0,height=150,width=325');
        event.preventDefault();
      }
    }
  }, true);

// --- mark AutoUnRAR and AutoPAR links ----------------------------------------

var links = document.evaluate("//form[@name='zip']//td[@class='subject']/a[1]",
                              document,
                              null,
                              XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
                              null);

var regex = new RegExp(/Auto(UnRAR|PAR)/);

for (var i = 0; i < links.snapshotLength; i++) {
  var link = links.snapshotItem(i);

  if (regex.test(link.innerHTML))
    link.setAttribute('class', 'AutoUnRAR');
}

// --- mark recent posts -------------------------------------------------------

//var s = new Date().getTime();

var months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
               'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ];

var p = new Date();
var m = p.getMonth();
var d = new Date(p.getFullYear(), m, p.getDate(), 0, 0, 0);

var tds = document.evaluate("//form[@name='zip']//td[@class='timeStamp']",
                            document,
                            null,
                            XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
                            null);

var p = { i: null, s: null, d: 0 };

for (var i = 0; i < tds.snapshotLength; i++) {
  var td = tds.snapshotItem(i);
  var tr = td.parentNode;

  if (tr.hasAttribute('class')) {
    if (!p.i || p.i != td.innerHTML) {
      p.i = td.innerHTML;
      p.s = p.i.split('-');
      p.m = months.indexOf(p.s[1]);
      p.d = new Date(d.getFullYear(), p.m, p.s[0], 0, 0, 0);
    }

    if (p.m == m && p.d >= d)
      tr.setAttribute('class', tr.getAttribute('class') + 'Today');
    else
      break;
  }
}

//alert(new Date().getTime() - s);
