/*
 * *  JqueryAsynchImageLoader (JAIL) : plugin for jQuery
 * *
 * * Developed by
 * * Sebastiano Armeli-Battana (@sebarmeli) - http://sebarmeli.com | http://blog.sebarmeli.com
 * *
 * * Licensed under MIT
 * * @version 0.5 
 * */
(function($){var $window=$(window);$.fn.asynchImageLoader=function(options){options=$.extend({timeout:10,effect:false,speed:400,selector:null,event:"load+scroll",callback:jQuery.noop,placeholder:false},options);var images=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={_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);options.callback.call(this,options);$.asynchImageLoader._purgeStack(images);},_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);options.callback.call(this,options,images);},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);}else{var initalTriggerEl=(options.selector)?$(options.selector):$window;initalTriggerEl.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);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);if(options.event==="load+scroll"){options.event="scroll";$.asynchImageLoader.onEvent(options,images);}},options.timeout);},_loadImageIfVisible:function(options,image,triggerEl){var $img=$(image),container=(options.event==="scroll"?triggerEl:$window);if($.asynchImageLoader._isInTheScreen(container,$img)){$.asynchImageLoader._loadImage(options,$img);}},_isInTheScreen:function($ct,$img){var is_ct_window=$ct[0]===window,ct_offset=$ct.offset()||{top:0,left:0},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();return ct_top<=img_offset.top&&ct_bottom>=img_offset.top&&ct_left<=img_offset.left&&ct_right>=img_offset.left;},_loadImage:function(options,$img){$img.attr("src",$img.attr("data-href"));$img.removeAttr("data-href");if(options.effect){$img[options.effect](options.speed);}}};}(jQuery));

