﻿function ImageLoader(a){this.image=new Image;var b=this.image;var c=false;this.getSrc=function(){return a};this.load=function(d){c=true;b.src=a;if(b.complete){d.apply(b)}else{$(b).load(d)}};this.unload=function(){c=false;b=this.image=new Image};this.isLoaded=function(){return c&&b.complete}}function ImgLoader(a,b,c){function p(b){var c=(b+1+Math.pow(10,i.indexSize)).toString(10).substr(1);return a.replace("{index}",c)}function o(){g=true;m(i.onComplete,this)}function n(){setTimeout(function(){d.loadNext()},$.browser.mozilla||$.browser.msie?50:5)}function m(a,b){if(!!a)a.apply(b)}var d=this;var e=[];var f=0;var g=false;this.length=b;var h={indexSize:4,initialStep:64,onComplete:null,onProgress:null,stopAt:1};var i={};$.extend(i,h,c);var j=i.initialStep;var k=0;var l=false;this.stop=function(){j=i.stopAt/2};this.reset=function(){g=false;f=0;j=i.initialStep;k=0;l=false;this.nearestIndex=-1;$.each(e,function(a,b){!!b&&b.unload()})};this.getAt=function(a){return e[a].image};this.nearestIndex=-1;this.getNearest=function(a){a=Math.floor(a);var b=0;var c,d;for(b=0;b<e.length;b++){c=a+b;if(c>=0&&c<e.length){d=e[c];if(d&&d.isLoaded()){this.nearestIndex=c;return d.image}}c=a-b;if(c>=0&&c<e.length){d=e[c];if(d&&d.isLoaded()){this.nearestIndex=c;return d.image}}}return null};this.getNumLoaded=function(){return f};this.getLoadProgress=function(){return f*i.stopAt/d.length};this.isLoaded=function(a){if(a===undefined){return f==d.length}else{return e[a].isLoaded()}};this.loadPosition=function(a,b){a=Math.min(1,Math.max(0,a));var c=a*(d.length-1);c=Math.round(c);d.loadIndex(c,b)};this.loadIndex=function(a,b){if(a<0||a>=d.length)return false;if(a!=Math.floor(a))return false;var c=e[a];if(c==null){var h=p(a);c=new ImageLoader(h);e[a]=c}c.load(function(){f++;if(!g){m(i.onProgress,this)}m(b,this)})};this.loadNext=function(a){function c(){o();m(a,this)}function b(){n();m(a,this)}if(j<i.stopAt)return;k+=j;if(k>=d.length){if(l)j/=2;l=true;k=j/2;if(k>=i.stopAt){d.loadIndex(k,b)}else{o()}}else{d.loadIndex(k,b)}};this.getImageLoader=function(a){return e[a]};this.load=function(){d.loadIndex(0,n)}}
