// +----------------------------------------------------------------------+ // | Author: Thomas Biegeleisen | // +----------------------------------------------------------------------+ // | For bug reports, visit: // | http://www.biegeldesigns.com/projects/preloader // +----------------------------------------------------------------------+ function Preload(images) { // initial settings this.images = images; // store the array this.nLoaded = 0 ; // number loaded this.loadedImages = new Object() ; // booleans for images this.firstImage = "" ; // the first image to load this.currentImage = "" ; // the current image being loaded this.nextImage = new Object() ; // array containing pointers to next image this.interrupt = false ; // whether user has interrupted or not // getting count (we have to iterate since jscript assoc. arrays aren't true Arrays) var count = 0; var lastKey = false ; for ( i in images ) { if ( count == 0 ) { this.firstImage = i ; } count++ ; this.loadedImages[i] = false ; if ( lastKey != false ) { this.nextImage[lastKey] = i ; } lastKey = i ; } // set count this.nImages = count ; // preload the first image this.preloadImage( this.firstImage ) ; } Preload.prototype.preloadImage = function( imageID ) { if ( this.interrupt == false ) { this.currentImage = imageID ; oImage = new Image() ; oImage.onload = Preload.prototype.onload ; // set a reference back to parent object oImage.parent = this ; oImage.src = this.images[imageID] ; // Safari workaround IHateSafari = imageID ; } } Preload.prototype.onload = function() { this.parent.loadedImages[this.parent.currentImage] = true ; if ( this.parent.interrupt == false ) { // see if there's another image var next = this.parent.nextImage[this.parent.currentImage] ; if ( next ) { this.parent.preloadImage(next) ; } else { } } } Preload.prototype.resume = function() { if ( this.nLoaded != this.nImages ) { this.preloadImage(this.currentImage) ; } } function ImagePreload( imageID, loaded ) { // load the intervening image var tempImage = new Image; tempImage.onload = ImagePreload.prototype.onload ; tempImage.display = ImagePreload.prototype.display ; tempImage.imageID = imageID ; this.imageID = imageID ; // Safari's DOM has holes in it! // we have to add another temp variable IHateSafari = imageID ; if ( loaded ) { this.display() ; } else { tempImage.src = PreloadList[imageID] ; } } ImagePreload.prototype.onload = function() { // this is a Safari workaround -- see Safari note above if ( !this.imageID ) { ip.loadedImages[IHateSafari] = true ; PreloadSwap(IHateSafari) ; return ; } div = document.getElementById('imageOutput' + this.imageID) ; div.innerHTML = "loaded!" ; ip.loadedImages[this.imageID] = true ; this.display() ; } ImagePreload.prototype.display = function() { ip.interrupt = false ; ip.resume() ; PreloadSwap(this.imageID) ; return ; } function PreloadSwap( imageID ) { if ( PreloadImg != "" ) { var thisImage = document.images[PreloadImg] ; thisImage.src = PreloadList[imageID] ; } else if ( PreloadDiv ) { var thisDiv = document.getElementById(PreloadDiv) ; thisDiv.innerHTML = "\"\"" ; } return; } function displayImage( imageID ) { // make sure page & script have been initialized if ( typeof(ip) == "undefined" ) { //ie 6.0 core error stops page from loading? //alert('Please wait until the thumbnails are all loaded.') ; //window.location.reload() ; return ; } // we're good to go ip.interrupt = true ; if ( ip.nImages == ip.nLoaded ) { PreloadSwap(imageID) ; return ; } var temp = new ImagePreload( imageID, ip.loadedImages[imageID] ) ; return ; }