/* A JavaScript plugin for lazy-loading responsive Google Adsense ads. - By Osvaldas Valutis, www.osvaldas.info Available for use under the MIT License */ ;( function( $, window, document, undefined ) { var $win = $( window ), throttle = function(a,b){var c,d;return function(){var e=this,f=arguments,g=+new Date;c&&g winHeight * laziness || winScroll - offset - $this.outerHeight() - ( winHeight * laziness ) > 0 ) return true; $adsToLoad = $adsToLoad.not( $this ); $adsLoaded = $adsLoaded.add( $this ); $this .data( 'alOriginalHTML', $this.html()) .data( 'alWidth', getAdWidth( this )) .children( ':first' ) .addClass( 'adsbygoogle' ); if( typeof adsbygoogle !== 'undefined' ) loadAd( $this ); else $adsPending = $adsPending.add( $this ); if( !scriptLoaded ) { scriptLoaded = true; $.ajax( { url: scriptUrl, async: true, cache: true, dataType: 'script', success: function() { $adsPending.each( function() { loadAd( $( this )); }); $adsPending = $([]); } }); } }); }, resizeAds = function() { if( !$adsLoaded.length ) return true; var anyNew = false; $adsLoaded.each( function() { var $this = $( this ); if( $this.data( 'alWidth' ) != getAdWidth( this )) { $adsLoaded = $adsLoaded.not( $this ); $this.html( $this.data( 'alOriginalHTML' )); $adsToLoad = $adsToLoad.add( $this ); anyNew = true; } }); if( anyNew ) initAds(); }; $win .on( 'scroll resize', throttle( throttleTO, initAds )) .on( 'resize', throttle( throttleTO, resizeAds )); $.fn.adsenseLoader = function( options ) { if( typeof options !== 'string' ) { options = $.extend({}, { laziness: 1, onLoad: false }, options ); } this.each( function() { var $this = $( this ); if( options === 'destroy' ) { $this.html( $this.data( 'alOriginalHTML' )); $adsToLoad = $adsToLoad.not( $this ); $adsLoaded = $adsLoaded.not( $this ); $adsPending = $adsPending.not( $this ); } else { $this.data( 'alOptions', options ); $adsToLoad = $adsToLoad.add( $this ); } }); if( options !== 'destroy' ) initAds(); return this; }; $.adsenseLoaderConfig = function( options ) { if( typeof options.scriptUrl !== 'undefined' ) scriptUrl = options.scriptUrl; if( typeof options.throttle !== 'undefined' ) throttleTO = options.throttle; }; })( jQuery, window, document );