// ==UserScript==
// @name         Scroll Pager
// @namespace    http://iescripts.org
// @author       Protector one
// @include      *
// @description  Auto-loads next/previous page by scroll-wheeling beyond a page's end/start.
// @date         7-7-2009
// @version      1.5
// ==/UserScript==

(function(){
  function removeProgbar(){
    var pb = document.getElementById('progbarP1');
    if (!pb) return;
    pb.parentNode.removeChild(pb);
  }
  function attachProgbar(down){
    initialDown = down;
    if(!document.styleSheets.length)
      document.body.appendChild(document.createElement('style'));
    if (bc){
      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'),
        ps = prog.style;
    prog.id = 'progbarP1';
    ps.position = 'absolute';
    down? ps.bottom = 120-(bc? document.body.scrollTop: document.documentElement.scrollTop): ps.top = 20;
    ps.right = 50;
    prog.innerHTML = '<v:rect style="position:absolute;width:30px;height:102px;" ></v:rect>'+
      '<v:rect id="inbarP1" style="position:absolute;left:1px;top:'+(down?1:101-Math.floor(100/SCROLLS))+'px;width:28px;height:'+
      (100/SCROLLS)+'px;" fillcolor="'+(!pressed? blueNormal: redNormal)+'" strokecolor="'+(!pressed? blueNormal: redNormal)+'"></v:rect>';
    document.body.appendChild(prog);
  }
  function advanceProgbar(down){
    var pbCont = document.getElementById('inbarP1');
    if (!pbCont) return;
    if (down){
      pbCont.style.height = groundpound*100/SCROLLS;
    }else{
      var sec = Math.floor(groundpound*100/SCROLLS);
      pbCont.style.top = 101 - sec;
      pbCont.style.height = sec;
    }
  }

  function scroller(){
    if (firstscroll){
      firstscroll = false;
      getScrollables();
    }
    var wheel = event.wheelDelta;
    if (groundpound > 0 && (wheel < 0) != initialDown){
      removeProgbar();
      groundpound = 0;
      window.scrollBy(0,wheel);
      return;
    }
    didscroll = true;
    ival = setTimeout(function(){scrollerPart2(wheel);},10);
  }
  function scrollerPart2(wheel){
    if (!didscroll || working) return;
    var nin, inc, bar,
        scrolldown = wheel < 0;
    if (groundpound++ == 0){
      attachProgbar(scrolldown);
    }else{
      advanceProgbar(scrolldown);
    }
    clearTimeout(reminv);
    reminv = setTimeout(function(){
      removeProgbar();
      groundpound = 0;
    },1300);
    if (groundpound < SCROLLS){
      return;
    }
    working = true;
    bar = document.getElementById('inbarP1');
    bar.fillcolor = bar.strokecolor = !pressed? blueWorking: redWorking;
    document.body.style.cursor = 'wait';
    if (pressed){
      inc = 10*(pressed-48)
      if (inc == 0) inc = 100;
    }else{
      inc = 1;
    }
    nin = parseInt(num,10) + (2*scrolldown-1)*inc;
    if (nin<0) nin += 10*inc;
    nin += '';
    if ((diff = num.length - nin.length) > 0) nin = (Math.pow(10,diff)+'').slice(1) + nin;
    location.href = url+nin+opurl;
    document.onmousewheel = null;
    return;
  }
  function checkScroll(){
    if (!didscroll) return;
    didscroll = false;
    if (groundpound == 0) return;
    removeProgbar();
    groundpound = 0;
  }
  function getScrollables(){
    for (var x in document.all){
      if (!(x in scrollables) && document.all[x] &&
          document.all[x].scrollHeight && document.all[x].clientHeight &&
          document.all[x].scrollHeight > document.all[x].clientHeight &&
          document.all[x].attachEvent){
        document.all[x].attachEvent('onscroll',checkScroll);
        scrollables[x] = 0;
      }
    }
  }
  function clicked(){
    firstscroll = true;
  }
  function keydown(){
    var bar, key = window.event.keyCode;
    if (key >= 48 && key <= 57){
      pressed = key;
      if (!working && (bar = document.getElementById('inbarP1')))
        bar.fillcolor = bar.strokecolor = redNormal;
    }
  }
  function keyup(){
    var bar, key = window.event.keyCode;
    if (key >= 48 && key <= 57){
      pressed = false;
      if (!working && (bar = document.getElementById('inbarP1')))
        bar.fillcolor = bar.strokecolor = blueNormal;
    }
  }
  
  var reminv, nin, min, diff, url, num, opurl, zeros, oldload, ival, initialDown, pressed,
      SCROLLS = 5, // change this value to scroll more/less
      blueNormal =  '#8888FF',
      blueWorking = '#5555FF',
      redNormal = '#FF8888',
      redWorking = '#FF5555',
      scrollables = {},
      didscroll = false,
      firstscroll = true,
      bc = document.compatMode=='BackCompat',
      groundpound = 0,
      working = false,
      index = location.href.replace(/#.*/,'');
  index = index.match(/(.+?)([0-9]+?)([^0-9]*?)$/i);
  if (!index || index.length < 3) return;
  url = index[1];
  num = index[2];
  opurl = index[3];
  opurl = opurl.replace(/#.*$/,'');
  window.attachEvent('onscroll',checkScroll);
  window.document.attachEvent('onmousewheel',scroller);
  window.document.attachEvent('onclick',clicked);
  window.document.attachEvent('onkeydown',keydown);
  window.document.attachEvent('onkeyup',keyup);
})();
