// $('#content a').not($('.tabbedInterface ul.nav a'))
//                .not($('.expandingSpace a.trigger'))
//                .not($('.doNotPrint'))
//                .printPage();

(function($) {
  var links = {}, linkNum = 0;

  function urlExists(u) {
    if (links[u]) return true;
    return false;
  }
  function addUrl(u) {
    if (urlExists(u)) return;
    links[u] = ++linkNum;
  }
  function numForLink(u) {
    if (!urlExists(u)) addUrl(u);
    return links[u];
  }
  function fullyQualified(u,s) {
    s = s || window.location.href;
    
    // Get rid of any print arg
    if (s.indexOf("?") > -1 )
      s = s.substr(0, s.indexOf("?"));
    
    // Trim a filename off if we have one.
    var r = s.match(/^(.*\/)+[^\/]+\.\w+$/);
    if (r != null)
      s = r[1];
      
    if (u == null) return null;
    
    // If u is fully qualified (http://, https://, ftp://, file://, etc) return it
    if (u.match(/^\w{3,5}:\/\//)) return u;
    
    // If u starts with / just prepend the site
    if (u.substr(0,1) == '/') return window.location.href.substr(0, window.location.href.indexOf('/', 9)) + u;
    
    // If u starts with ../ trim and try again
    if (u.substr(0,3) == '../')
    {
      var curr_dir = s;
      curr_dir = curr_dir.substr(0, curr_dir.lastIndexOf('/'));
      var parent_dir = curr_dir.substr(0, curr_dir.lastIndexOf('/'));
      if (parent_dir.substr(-1) != '/') parent_dir += '/';
      return fullyQualified(u.substr(3), parent_dir);
    }
    
    // If u starts with ./ it's in the curr directory
    if (u.substr(0,2) == './') u = u.substr(2);
    
    // If we're here, we have something in the current dir
    return s + u;
  }
  $.fn.printPage = function(o) {
    // setup configuration
		o = $.extend({}, arguments.callee.defaults, o);
		
    if (o.skipUrlCheck || $.getURLParam(o.urlParam) == o.urlParamValue)
    {
      var footnoteLinks = $('<ol id="footnoteLinks"></ol>');
      $('body').addClass(o.bodyClass);
      this.not('#'+o.printLinkId).each(function() {
        var u = fullyQualified($(this).attr('href'));
        if (u == null) return null;
        if (! urlExists(u))
          $('<li>'+u+'</li>').appendTo(footnoteLinks);
        $('<sup>['+ numForLink(u) +']</sup>').insertAfter($(this));
      });
      if (linkNum > 0) {
        // Only add footnotes if we have links to footnote
        $('<h2 id="footnoteLinksTitle">Links</h2>').appendTo("#content");
        footnoteLinks.appendTo("#content");
      }
      if (o.printDialog) print();
    }
    return this;
  };
  function addArg(k,v) {
      var printPage = $('#printPage');
      if (! printPage) return; 
      printPage.attr('href', printPage.attr('href')+'&'+encodeURIComponent(k)+'='+encodeURIComponent(v));
  }

  $.fn.printPage.defaults = {
    bodyClass: 'print', // class to add to body element when in "print" mode
    urlParam: 'print', // parameter to look for in the querystring as an indication we're in print mode
    urlParamValue: 1, // value to look for
    printLinkId: 'printPage', // id of the printPage element so that we don't footnote it
    printDialog: true, // popup the print page dialog
    skipUrlCheck: false // skip any url checking and just go into print mode
  };
  
})(jQuery);
