// ==UserScript==
// @name Image preloader
// @author Arve Bersvendsen 
// @namespace http://virtuelvis.com/
// @description  Improves image browsing by preloading images while
//			directory listings are displayed, so that
//			images appear instantly when loaded
// @ujs:category browser: enhancements
// @ujs:published 2005-05-20 21:23
// @ujs:modified 2005-09-19 09:19
// @ujs:documentation http://userjs.org/scripts/browser/enhancements/image-preloader 
// @ujs:download http://userjs.org/scripts/download/browser/enhancements/imagepreloader.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
 */


document.addEventListener("load",function(ev){
  if (document.title.indexOf("Index of") != -1 ) {

    // Max number of images to prefetch.
    // Hat tip: Keep this at 1/8th of your physical memory (in MB)
    // If you set this to -1, Opera will preload all images. This is not recommended.
    var prefetch_limit = 64;
    
    var d = document.links;
    var pl = new Array();
    var imgs = new Array();

    
    for (var i = 0; i < d.length; i++) {
      if (d[i].href.search(/^.*\.(jpe?g|png|bmp|gif)$/i) !=-1) {
        pl.push(d[i].href);
      }
    }

    if ((pl.length > 0) && (prefetch_limit != 0)){
      var ps = document.createElement("p");
      ps.id="preloader";
      ps.innerText = "Preloading";
      ps.style="font: menu;margin:0;padding:1px 3px;position:fixed;right:0;top:0;float:left;background: InfoBackground;color:InfoText;border:1px solid; border-color: InfoText');";
      document.body.appendChild(ps);
    }

    if ((pl.length<prefetch_limit) || (prefetch_limit == -1) ) {
      prefetch_limit=pl.length;
    } 

    var full_count=0;

    this.checkAll = function(){
      if (++full_count == prefetch_limit){
        ps.innerText="Done. Successfully preloaded "+lc+" images with "+lf+" failures";
      }
    }

    var self=this;
    var lc = 0;
    var lf = 0;

    for (var i = 0; i < prefetch_limit; i++) {
      imgs[i] = new Image();
      imgs[i].onload = function(){
        ps.innerText = "Preloading:"+(++lc)+" of "+prefetch_limit+". Failed: "+lf;
        self.checkAll();
      }
      imgs[i].onerror= function(){
        ps.innerText = "Preloading:"+lc+" of "+prefetch_limit+". Failed: "+(++lf);
        self.checkAll();
      }
      imgs[i].src= pl[i];
    }
  }
},false);