// ==UserScript==
// @name Enhance AWStats result pages
// @author Arve Bersvendsen 
// @namespace http://virtuelvis.com/
// @description  Enhance the output on AWStats keyphrase and keywords
//			result pages.
// @ujs:category general: enhancements
// @ujs:published 2005-05-11 15:11
// @ujs:modified 2005-09-19 09:19
// @ujs:documentation http://userjs.org/scripts/general/enhancements/enhance-awstats 
// @ujs:download http://userjs.org/scripts/download/general/enhancements/enhanceawstats.js
// ==/UserScript==

/* 
 * Copyright © 2005 by Arve Bersvendsen
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */


// Check that we're actually on an awstats result page
if ( (window.location.href.indexOf("awstats.pl?") >- 1) 
  && (window.location.href.indexOf("output=key") > -1) ){
  document.addEventListener("load",function(e){
    // Create the filter form
    this.createFilterForm = function(){
      var f = document.createElement("form");
      f.addEventListener("submit", function(e){return false},false);
      f.setAttribute("style","background: white;position:fixed;left:0;top:0;padding: 3px; border-width: 0 1px 1px 0; border-style: solid; border-color: #555;");
      var p = document.createElement("p");
      ft = document.createElement("input");
      // Add keyup event listener for the attached text input
      ft.addEventListener("keyup", function(e){
        var searchtext = e.target.value;
        var innode = document.getElementsByTagName('table')[4].getElementsByTagName('tr');
        var sum1 = 0;
        var sum2 = 0;

        // search table rows, except from the first and last
        for (var i = 1; i < innode.length-1; i++){
          if ((searchtext.length == 0) || (innode[i].innerText.indexOf(searchtext) != -1)){
            innode[i].style.display = "table-row"; 
            sum1++;
            sum2 += parseInt(innode[i].childNodes[1].innerText);
          } else {
            // hide non-matching table rows
            innode[i].style.display = "none"; 
          }
        }
        innode[0].childNodes[1].innerText = "Search ("+sum1+"/"+sum2+")";
      },false);
      
      ft.title = "Plain text or regular expressions accepted";
      p.appendChild(ft);
      f.appendChild(p);
      return f;
    }

    f = this.createFilterForm();
    document.body.appendChild(f);
    return false;

  },false);
} 
