/*
*  JqueryAsynchImageLoader (JAIL) : plugin for jQuery
*
* Developed by
* Sebastiano Armeli-Battana (@sebarmeli) - http://sebarmeli.com | http://blog.sebarmeli.com
*
* Licensed under MIT
*/
(function($){$.fn.asynchImageLoader=function(options){options=$.extend({timeout:10,effect:"fadein",speed:400,selector:"",event:"load",callback:false,placeholder:false},options);var images=this;if(options.placeholder!==false){images.each(function(){$(this).attr("src",options.placeholder);});}switch(options.event){case"click":$.asynchImageLoader.onEvent.apply(this,Array.prototype.slice.call(arguments));break;case"mouseover":$.asynchImageLoader.onEvent.apply(this,Array.prototype.slice.call(arguments));break;case"scroll":$.asynchImageLoader.onScroll.apply(this,Array.prototype.slice.call(arguments));break;default:$.asynchImageLoader.later.apply(this,[options]);}return images.each(function(){return this;});};$.asynchImageLoader={onEvent:function(options){var images=$(this);if($(options.selector).length===0){$(images).bind(options.event,function(e){if($.data(this,"loaded")!=="true"){$.asynchImageLoader._loadImage(options,this);}if(!options.callback){return false;}return options.callback.call(this,options);});}else{$(options.selector).bind(options.event,function(e){$(images).each(function(){if($.data(this,"loaded")!=="true"){$.asynchImageLoader._loadImage(options,this);}});if(!options.callback){return false;}return options.callback.call(this,options,images);});}},later:function(options){var images=$(this);setTimeout(function(){images.each(function(){$.asynchImageLoader._checkTheImageInTheScreen(options,this);});didScroll=false;$(window).scroll(function(){didScroll=true;});setInterval(function(){if(didScroll){didScroll=false;images.each(function(){if($.data(this,"loaded")!=="true"){$.asynchImageLoader._checkTheImageInTheScreen(options,this);}});}},250);},options.timeout);},onScroll:function(options){var images=$(this);didScroll=false;$(window).scroll(function(){didScroll=true;});setInterval(function(){if(didScroll){didScroll=false;images.each(function(){$.asynchImageLoader._checkTheImageInTheScreen(options,this);});}},250);},_checkTheImageInTheScreen:function(options,image){if($.data(image,"loaded")==="true"){return;}if($.asynchImageLoader._isInTheScreen(window,image)){$.asynchImageLoader._loadImage(options,image);}},_isInTheScreen:function(windowEl,image){return($(windowEl).scrollTop()<=$(image).offset().top)&&(($(windowEl).scrollTop()+$(windowEl).height())>=($(image).offset().top)&&($(windowEl).scrollLeft()<=$(image).offset().left)&&(($(windowEl).scrollLeft()+$(windowEl).width())>=$(image).offset().left));},_loadImage:function(options,image){if(options.effect.match("/fadein/ig")){$(image).attr("src",$(image).attr("data-href")).fadeIn(options.speed);$.data(image,"loaded","true");}else{$(image).attr("src",$(image).attr("data-href")).show();$.data(image,"loaded","true");}}};}(jQuery));
