/*! jQuery.ImgLoader (https://github.com/Takazudo/jQuery.ImgLoader) * lastupdate: 2013-08-01 * version: 0.5.0 * author: Takeshi Takatsudo 'Takazudo' * License: MIT */ (function(){var t=[].slice,e={}.hasOwnProperty,r=function(t,r){function o(){this.constructor=t}for(var n in r)e.call(r,n)&&(t[n]=r[n]);return o.prototype=r.prototype,t.prototype=new o,t.__super__=r.prototype,t};(function(e,o){var n,i;return n=e.ImgLoaderNs={},n.support={},n.support.xhr2=null!=o.FormData,n.createCachedFunction=function(t){var r;return r={},function(o,n){return r[o]||(r[o]=e.Deferred(function(e){return t(e,o,n)}).promise()),r[o]}},n.fetchImg=n.createCachedFunction(function(t,r,o){var i,s,u,a;return u=new Image,s=function(){return u.onload=u.onerror=null},t.always(s),i=e(u),u.onload=function(){return t.resolve(i)},u.onerror=function(){return t.reject(i)},n.support.xhr2&&(null!=o?o.useXHR2:void 0)?(a=new n.Xhr2Request(r,{timeout:o.timeout}),t.xhr=a,a.on("progress",function(){return t.notify(a.currentLoadedInfo())}),a.on("loadend timeout",function(){return u.src=r}),a.send()):u.src=r}),function(){var t;return t={},n.loadImg=function(r,o,i){return e.Deferred(function(e){return n.fetchImg(r,{useXHR2:o,timeout:i}).progress(function(t){return e.notify(t)}).then(function(o){var n,i;return t[r]||(t[r]=o),n=t[r],i=n.clone(),t[r]=i,e.resolve(n)},function(t){return e.reject(t)})}).promise()}}(),n.loadImgWoCache=function(t,r,o){return e.Deferred(function(e){return n.fetchImg(t,{useXHR2:r,timeout:o}).progress(function(t){return e.notify(t)}).then(function(t){return e.resolve(t)},function(t){return e.reject(t)})}).promise()},i=function(t){return e.Deferred(function(e){return setTimeout(function(){return e.resolve()},t)})},n.Event=function(){function e(){this._callbacks={}}return e.prototype.on=function(t,e){var r,o,n,i,s;for(r=t.split(" "),i=0,s=r.length;s>i;i++)o=r[i],(n=this._callbacks)[o]||(n[o]=[]),this._callbacks[o].push(e);return this},e.prototype.one=function(t,e){return this.on(t,function(){return this.off(t,arguments.callee),e.apply(this,arguments)})},e.prototype.trigger=function(){var e,r,o,n,i,s,u;if(e=arguments.length>=1?t.call(arguments,0):[],o=e.shift(),n=null!=(u=this._callbacks)?u[o]:void 0){for(i=0,s=n.length;s>i&&(r=n[i],r.apply(this,e)!==!1);i++);return this}},e.prototype.off=function(t,e){var r,o,n,i,s,u;if(!t)return this._callbacks={},this;if(n=null!=(u=this._callbacks)?u[t]:void 0,!n)return this;if(!e)return delete this._callbacks[t],this;for(o=i=0,s=n.length;s>i;o=++i)if(r=n[o],r===e){n=n.slice(),n.splice(o,1),this._callbacks[t]=n;break}return this},e.prototype.bind=function(){return this.on.apply(this,arguments)},e.prototype.unbind=function(){return this.off.apply(this,arguments)},e}(),n.Xhr2Request=function(t){function o(t,r){this.url=t,o.__super__.constructor.apply(this,arguments),this.options=e.extend({timeout:1e4},r),this._prepare()}return r(o,t),o.prototype._prepare=function(){var t,e=this;return t=!1,this._request=new XMLHttpRequest,this._request.open("GET",this.url),this._request.timeout=this.options.timeout,this._request.onloadend=function(){return e.trigger("loadend")},this._request.onprogress=function(r){return t||(t=!0,e.totalSize=r.totalSize,e.trigger("firstprogress")),e.loadedSize=r.loaded,e.loadedRatio=e.loadedSize/e.totalSize,e.trigger("progress")},this._request.ontimeout=function(){return e.options.timeout},this},o.prototype.currentLoadedInfo=function(){return{totalSize:this.totalSize,loadedSize:this.loadedSize,loadedRatio:this.loadedRatio}},o.prototype.send=function(){return this._request.send(),this},o}(n.Event),n.LoaderItem=function(t){function o(t,e,r){this.src=t,this._useXHR2=null!=e?e:!0,this._timeout=null!=r?r:1e4,o.__super__.constructor.apply(this,arguments)}return r(o,t),o.prototype.load=function(){var t=this;return e.Deferred(function(e){return n.loadImg(t.src,t._useXHR2,t._timeout).progress(function(e){return t.trigger("progress",e)}).then(function(r){return t.$img=r,t.trigger("success",t.$img),t.trigger("complete",t.$img),e.resolve(t.$img)},function(r){return t.$img=r,t.trigger("error",t.$img),t.trigger("complete",t.$img),e.reject(t.$img)})})},o}(n.Event),n.AbstractLoader=function(t){function e(){e.__super__.constructor.apply(this,arguments)}return r(e,t),e.prototype._prepareProgressInfo=function(){var t,e,r;return t=this.items||this._presets,e=t.length,this.progressInfo=r={loadedFileCount:0,totalFileCount:e,loadedRatio:0},this.ratioPerItem=1/e,this},e.prototype._updateProgressInfo=function(t,e){var r,o;return o=this.progressInfo,r=e.loadedRatio*this.ratioPerItem,o.loadedRatio=o.loadedRatio+r-(t.lastLoadedRatio||0),o.loadedRatio>1&&(o.loadedRatio=1),t.lastLoadedRatio=r,this},e}(n.Event),n.BasicLoader=function(o){function i(t,e){this._useXHR2=null!=t?t:!0,this._timeout=null!=e?e:1e4,i.__super__.constructor.apply(this,arguments),this.items=[]}return r(i,o),i.prototype.add=function(t){var r;return"string"===e.type(t)&&(r=t,t=new n.LoaderItem(r,this._useXHR2,this._timeout)),this.items.push(t),t},i.prototype.load=function(){var r,o,i=this;return this._prepareProgressInfo(),o=this.progressInfo,r=e.map(this.items,function(t){return t.on("progress",function(e){return i._updateProgressInfo(t,e),i.trigger("progress",o)}),t.on("complete",function(t){return o.loadedFileCount+=1,n.support.xhr2&&i._useXHR2||(o.loadedRatio=o.loadedFileCount/o.totalFileCount,i.trigger("progress",o)),i.trigger("itemload",t,o)}),t.load()}),e.Deferred(function(n){return e.when.apply(i,r).always(function(){var r,s;return s=arguments.length>=1?t.call(arguments,0):[],r=e(s),o.loadedRatio=1,i.trigger("progress",o),i.trigger("allload",r,o),n.resolve(r,o)})}).promise()},i.prototype.kill=function(){var t,e,r,o;for(o=this.items,e=0,r=o.length;r>e;e++)t=o[e],t.off();return this.trigger("kill"),this.off(),this},i}(n.AbstractLoader),n.ChainLoader=function(t){function o(t,r,n,i){this._pipesize=t,this._delay=null!=r?r:0,this._useXHR2=n,this._timeout=i,o.__super__.constructor.apply(this,arguments),this._presets=[],this._inLoadCount=0,this._allDoneDefer=e.Deferred()}return r(o,t),o.prototype._finished=function(){return this.progressInfo.loadedFileCount===this._presets.length},o.prototype._nextLoadAllowed=function(){return this._inLoadCounte;e++)t=o[e],t.item.off();return this.trigger("kill"),this.off(),this},o}(n.AbstractLoader),n.LoaderFacade=function(){function r(t){var r,o,i,s,u;for(this.options=r=e.extend({srcs:[],pipesize:0,delay:100,timeout:1e4,useXHR2:!1},t),this.loader=r.pipesize?new n.ChainLoader(r.pipesize,r.delay,r.useXHR2,r.timeout):new n.BasicLoader(r.useXHR2,r.timeout),u=r.srcs,i=0,s=u.length;s>i;i++)o=u[i],this.loader.add(o)}var o,i,s,u,a;for(i=["bind","trigger","on","off","load","one","unbind","add","kill"],s=function(e){return r.prototype[e]=function(){var r;return r=arguments.length>=1?t.call(arguments,0):[],this.loader[e].apply(this.loader,r)}},u=0,a=i.length;a>u;u++)o=i[u],s(o);return r}.call(this),e.loadImg=n.loadImg,e.loadImgWoCache=n.loadImgWoCache,e.ImgLoader=n.LoaderFacade,e.calcNaturalWH=n.calcNaturalWH,e.calcRectFitImgWH=n.calcRectFitImgWH})(jQuery,this,this.document)}).call(this);