// Greasemonkey http://greasemonkey.mozdev.org/
//
// ==UserScript==
// @name           CSPAN Direct 1.0
// @description    Direct links to realvideo, improved search form.
// @namespace      http://deoxy.org/gm
// @include        http://c-span.org*
// @include        http://*.c-span.org*
// ==/UserScript==
//
// Edit form improvements:
// Select date sort and 25 results, and focus the input.

(function() {
  var loc = location.href;
  var clips = new Array();
  var scripts = document.getElementsByTagName('script');
  if (scripts) {
    for (var i = 0; i < scripts.length; i++) {
      var js = scripts[i].innerHTML;
      var lines = js.split('\n'); 
      for (var n = 0; n < lines.length; n++) {
        var line = lines[n];
        if (line.match(/clip/)) {
          var num = line.replace(/^.*?(clip[0-9]+).*$/,'$1');
          var url = line.replace(/^.*?(rtsp[^'"\?]+).*$/,'$1');
          if (url.match(/\s/)) url = '';
          if (num && url) {
            clips[num] = url;
          }
        }
      }
    }
  }
  var links = document.getElementsByTagName('a');
  if (links) {
    for (var i = 0; i < links.length; i++) {
      var uri = links[i].href;
      if (uri.match(/^javascript:playClip\('rtsp/)) {
        uri = uri.replace(/^.*?(rtsp[^']+).*$/,'$1');
        links[i].href = uri;
      }
      if (uri.match(/^javascript:playClip\(clip/)) {
        uri = uri.replace(/^.*?(clip\d+).*$/,'$1');
        links[i].href = clips[uri];
      }
    }
  }
  if (loc.match(/advancedsearchform\.asp$/)) {
    // select sort by date
    document.forms[0].SortBy.selectedIndex = 1;
    // select 25 results
    document.forms[0].ResultCount.selectedIndex = 1;
    // focus input
    document.forms[0].AdvancedQueryText.focus();
  }
})();

