// ==UserScript==
// @name         Generic Auto Pager
// @namespace    http://iescripts.org
// @author       Protector one
// @include      *0
// @include      *1
// @include      *2
// @include      *3
// @include      *4
// @include      *5
// @include      *6
// @include      *7
// @include      *8
// @include      *9
// @include      *0.*
// @include      *1.*
// @include      *2.*
// @include      *3.*
// @include      *4.*
// @include      *5.*
// @include      *6.*
// @include      *7.*
// @include      *8.*
// @include      *9.*
// @description  Auto-loads next page by scroll-wheeling beyond a page's end.
// @date         8-6-2009
// @version      1.3
// ==/UserScript==


(function(){

  function removeProgbar(){
    var pb = document.getElementById('progbarP1');
    if (!pb) return;
    pb.parentNode.removeChild(pb);
  }
  
  function attachProgbar(){
    if(!document.styleSheets.length)
      document.body.appendChild(document.createElement('style'));
    if (document.compatMode=='BackCompat'){
      var scrollnow = document.body.scrollTop;
      document.styleSheets[0].addRule('v\\:rect','behavior:url(#default#VML)');
      document.body.scrollTop = scrollnow;
    }else{
      document.styleSheets[0].addRule('v\\:rect','behavior:url(#default#VML)');
    }
    var prog = document.createElement('div');
    prog.id = 'progbarP1';
    prog.style.position = 'relative';
    prog.innerHTML = '<v:rect style="position:absolute;top:-119px;right:18px;width:30px;height:102px;" ></v:rect>'+
      '<v:rect id="inbarP1" style="position:absolute;top:-118px;right:19px;width:28px;height:'+
      (100/SCROLLS)+'px;" fillcolor="#8888FF" strokecolor="#8888FF"></v:rect>';
    document.body.appendChild(prog);
    pbCont = document.getElementById('inbarP1');
  }
  
  function advanceProgbar(){
    if (!pbCont) return;
    pbCont.style.height = groundpound*100/SCROLLS;
  }
  
  function scrollChange(){
    var changed = false;
    var elem;
    for (var x in scrollElements){
      elem = document.all[x];
      if (elem.scrollTop != scrollElements[x]){
        scrollElements[x] = elem.scrollTop;
        changed = true;
      }
    }
    return changed;
  }

  function scroller(){
    if (scrollChange()){
      removeProgbar();
      groundpound = 0;
      return;
    }
    var de = document.documentElement;
    var db = document.body;
    if(!working &&
        ((document.compatMode=='BackCompat' &&
          (((db.scrollHeight-db.scrollTop)==db.clientHeight)) || (db.scrollHeight<db.clientHeight)) || 
        ((de.scrollHeight<de.clientHeight) || ((de.scrollHeight-de.scrollTop)==de.clientHeight)))){
      if (event.wheelDelta > 0){
        removeProgbar();
        groundpound = 0;
        window.scrollBy(0,event.wheelDelta);
        return;
      }
      ++groundpound;
      if (groundpound == 1){
        attachProgbar();
      }else{
        advanceProgbar();
      }
      if (groundpound < SCROLLS){
        return;
      }
      db.style.cursor = 'wait';
      location = url+nin+opurl;
      document.onmousewheel = null;
      return;
    }
  }
  
  var pbCont;
  var SCROLLS = 5;
  var scrollElements = {};
  for (var x in document.all){
    if (document.all[x].scrollHeight && document.all[x].clientHeight &&
        document.all[x].scrollHeight > document.all[x].clientHeight)
      scrollElements[x] = document.all[x].scrollTop;
  }
  var index = location.href.match(/(.+?)([0-9]+?)([^0-9]*?)(#|$)/i);
  if (!index || index.length < 3) return;
  var url = index[1];
  var opurl = index[3];
  if (opurl.charAt(0) == '#') opurl = '';
  var nin = (parseInt(index[2])+1)+'';
  for (var i=nin.length, j=index[2].length; i<j; ++i)
    nin = '0'+nin;
  var groundpound = 0;
  var working = false;
  document.onmousewheel = scroller;

})();
  