;(function($){var $window=$(window);$.fn.asynchImageLoader=$.fn.jail=function(options){options=$.extend({timeout:10,effect:false,speed:400,selector:null,offset:0,event:'load+scroll',callback:jQuery.noop,callbackAfterEachImage:jQuery.noop,placeholder:false,container:window},options);var images=this;$.jail.initialStack=this;this.data('triggerEl',(options.selector)?$(options.selector):$window);if(options.placeholder!==false){images.each(function(){$(this).attr("src",options.placeholder);});}if(/^load/.test(options.event)){$.asynchImageLoader.later.call(this,options);}else{$.asynchImageLoader.onEvent.call(this,options,images);}return this;};$.asynchImageLoader=$.jail={_purgeStack:function(stack){var i=0;while(true){if(i===stack.length){break;}else{if(stack[i].getAttribute('data-href')){i++;}else{stack.splice(i,1);}}}},_loadOnEvent:function(e){var $img=$(this),options=e.data.options,images=e.data.images;$.asynchImageLoader._loadImage(options,$img);$img.unbind(options.event,$.asynchImageLoader._loadOnEvent);$.asynchImageLoader._purgeStack(images);if(!!options.callback){$.asynchImageLoader._purgeStack($.jail.initialStack);$.asynchImageLoader._launchCallback($.jail.initialStack,options);}},_bufferedEventListener:function(e){var images=e.data.images,options=e.data.options,triggerEl=images.data('triggerEl');clearTimeout(images.data('poller'));images.data('poller',setTimeout(function(){images.each(function _imageLoader(){$.asynchImageLoader._loadImageIfVisible(options,this,triggerEl);});$.asynchImageLoader._purgeStack(images);if(!!options.callback){$.asynchImageLoader._purgeStack($.jail.initialStack);$.asynchImageLoader._launchCallback($.jail.initialStack,options);}},options.timeout));},onEvent:function(options,images){images=images||this;if(options.event==='scroll'||options.selector){var triggerEl=images.data('triggerEl');if(images.length>0){triggerEl.bind(options.event,{images:images,options:options},$.asynchImageLoader._bufferedEventListener);if(options.event==='scroll'||!options.selector){$window.resize({images:images,options:options},$.asynchImageLoader._bufferedEventListener);}return;}else{if(!!triggerEl){triggerEl.unbind(options.event,$.asynchImageLoader._bufferedEventListener);}}}else{images.bind(options.event,{options:options,images:images},$.asynchImageLoader._loadOnEvent);}},later:function(options){var images=this;if(options.event==='load'){images.each(function(){$.asynchImageLoader._loadImageIfVisible(options,this,images.data('triggerEl'));});}$.asynchImageLoader._purgeStack(images);$.asynchImageLoader._launchCallback(images,options);setTimeout(function(){if(options.event==='load'){images.each(function(){$.asynchImageLoader._loadImage(options,$(this));});}else{images.each(function(){$.asynchImageLoader._loadImageIfVisible(options,this,images.data('triggerEl'));});}$.asynchImageLoader._purgeStack(images);$.asynchImageLoader._launchCallback(images,options);if(options.event==='load+scroll'){options.event='scroll';$.asynchImageLoader.onEvent(options,images);}},options.timeout);},_launchCallback:function(images,options){if(images.length===0&&!$.jail.isCallback){options.callback.call(this,options);$.jail.isCallback=true;}},_loadImageIfVisible:function(options,image,triggerEl){var $img=$(image),container=(/scroll/i.test(options.event))?triggerEl:$window;if($.asynchImageLoader._isInTheScreen(container,$img,options.offset)){$.asynchImageLoader._loadImage(options,$img);}},_isInTheScreen:function($ct,$img,optionOffset){var is_ct_window=$ct[0]===window,ct_offset=(is_ct_window?{top:0,left:0}:$ct.offset()),ct_top=ct_offset.top+(is_ct_window?$ct.scrollTop():0),ct_left=ct_offset.left+(is_ct_window?$ct.scrollLeft():0),ct_right=ct_left+$ct.width(),ct_bottom=ct_top+$ct.height(),img_offset=$img.offset(),img_width=$img.width(),img_height=$img.height();return(ct_top-optionOffset)<=(img_offset.top+img_height)&&(ct_bottom+optionOffset)>=img_offset.top&&(ct_left-optionOffset)<=(img_offset.left+img_width)&&(ct_right+optionOffset)>=img_offset.left;},_loadImage:function(options,$img){$img.hide();$img.attr("src",$img.attr("data-href"));$img.removeAttr('data-href');if(options.effect){if(options.speed){$img[options.effect](options.speed);}else{$img[options.effect]();}}else{$img.show();}options.callbackAfterEachImage.call(this,$img,options);}};}(jQuery));