+BookReader.prototype.lazyLoadThumbnails = function() {
+
+ // console.log('lazy load');
+
+ // We check the complete property since load may not be fired if loading from the cache
+ $('.BRlazyloading').filter('[complete=true]').removeClass('BRlazyloading');
+
+ var loading = $('.BRlazyloading').length;
+ var toLoad = this.thumbMaxLoading - loading;
+
+ // console.log(' ' + loading + ' thumbnails loading');
+ // console.log(' this.thumbMaxLoading ' + this.thumbMaxLoading);
+
+ var self = this;
+
+ if (toLoad > 0) {
+ // $$$ TODO load those near top (but not beyond) page view first
+ $('#BRpageview img.BRlazyload').filter(':lt(' + toLoad + ')').each( function() {
+ self.lazyLoadImage(this);
+ });
+ }
+}
+
+BookReader.prototype.lazyLoadImage = function (dummyImage) {
+ //console.log(' lazy load started for ' + $(dummyImage).data('srcURL').match('([0-9]{4}).jp2')[1] );
+
+ var img = new Image();
+ var self = this;
+
+ $(img)
+ .addClass('BRlazyloading')
+ .one('load', function() {
+ //if (console) { console.log(' onload ' + $(this).attr('src').match('([0-9]{4}).jp2')[1]); };
+
+ $(this).removeClass('BRlazyloading');
+
+ // $$$ Calling lazyLoadThumbnails here was causing stack overflow on IE so
+ // we call the function after a slight delay. Also the img.complete property
+ setTimeout(function() { self.lazyLoadThumbnails(); }, 100);
+ })
+ .one('error', function() {
+ // Remove class so we no longer count as loading
+ $(this).removeClass('BRlazyloading');
+ })
+ .attr( { width: $(dummyImage).width(),
+ height: $(dummyImage).height(),
+ src: $(dummyImage).data('srcURL')
+ });
+
+ // replace with the new img
+ $(dummyImage).before(img).remove();
+
+ img = null; // tidy up closure
+}
+
+