/*!
   --------------------------------
   Infinite Scroll
   --------------------------------
   + https://github.com/paulirish/infinite-scroll
   + version 2.1.0
   + Copyright 2011/12 Paul Irish & Luke Shumard
   + Licensed under the MIT license

   + Documentation: http://infinite-scroll.com/
*/
!function(e){"function"==typeof define&&define.amd?define(["jquery"],e):e(jQuery)}(function(e,i){"use strict";e.infinitescroll=function(i,t,o){this.element=e(o),this._create(i,t)||(this.failed=!0)},e.infinitescroll.defaults={loading:{finished:i,finishedMsg:"<em>Congratulations, you've reached the end of the internet.</em>",img:"data:image/gif;base64,[encoded data, 10432 characters]",msg:null,msgText:"<em>Loading the next set of posts...</em>",selector:null,speed:"fast",start:i},state:{isDuringAjax:!1,isInvalidPage:!1,isDestroyed:!1,isDone:!1,isPaused:!1,isBeyondMaxPage:!1,currPage:1},debug:!1,behavior:i,binder:e(window),nextSelector:"div.navigation a:first",navSelector:"div.navigation",contentSelector:null,extraScrollPx:150,itemSelector:"div.post",animate:!1,pathParse:i,dataType:"html",appendCallback:!0,bufferPx:40,errorCallback:function(){},infid:0,pixelsFromNavToBottom:i,path:i,prefill:!1,maxPage:i},e.infinitescroll.prototype={_binding:function(e){var t=this,o=t.options;if(o.v="2.0b2.120520",o.behavior&&this["_binding_"+o.behavior]!==i)this["_binding_"+o.behavior].call(this);else{if("bind"!==e&&"unbind"!==e)return this._debug("Binding value  "+e+" not valid"),!1;"unbind"===e?this.options.binder.off("smartscroll.infscr."+t.options.infid):this.options.binder.on("smartscroll.infscr."+t.options.infid,function(){t.scroll()}),this._debug("Binding",e)}},_create:function(t,o){var n=e.extend(!0,{},e.infinitescroll.defaults,t);this.options=n;var a=e(window);if(!this._validate(t))return!1;var s=e(n.nextSelector).attr("href");if(!s)return this._debug("Navigation selector not found"),!1;n.path=n.path||this._determinepath(s),n.contentSelector=n.contentSelector||this.element,n.loading.selector=n.loading.selector||n.contentSelector,n.loading.msg=n.loading.msg||e('<div id="infscr-loading"><img alt="Loading..." src="'+n.loading.img+'" /><div>'+n.loading.msgText+"</div></div>"),(new Image).src=n.loading.img,n.pixelsFromNavToBottom===i&&(n.pixelsFromNavToBottom=e(document).height()-e(n.navSelector).offset().top,this._debug("pixelsFromNavToBottom: "+n.pixelsFromNavToBottom));var r=this;return n.loading.start=n.loading.start||function(){e(n.navSelector).hide(),n.loading.msg.insertAfter(n.loading.selector).show(n.loading.speed,e.proxy(function(){this.beginAjax(n)},r))},n.loading.finished=n.loading.finished||function(){n.state.isBeyondMaxPage||n.loading.msg.fadeOut(n.loading.speed)},n.callback=function(t,s,r){n.behavior&&t["_callback_"+n.behavior]!==i&&t["_callback_"+n.behavior].call(e(n.contentSelector)[0],s,r),o&&o.call(e(n.contentSelector)[0],s,n,r),n.prefill&&a.on("resize.infinite-scroll",t._prefill)},t.debug&&(!Function.prototype.bind||"object"!=typeof console&&"function"!=typeof console||"object"!=typeof console.log||["log","info","warn","error","assert","dir","clear","profile","profileEnd"].forEach(function(e){console[e]=this.call(console[e],console)},Function.prototype.bind)),this._setup(),n.prefill&&this._prefill(),!0},_prefill:function(){var i=this,t=e(window);function o(){return e(i.options.contentSelector).height()<=t.height()}this._prefill=function(){o()&&i.scroll(),t.on("resize.infinite-scroll",function(){o()&&(t.off("resize.infinite-scroll"),i.scroll())})},this._prefill()},_debug:function(){!0===this.options.debug&&("undefined"!=typeof console&&"function"==typeof console.log?1===Array.prototype.slice.call(arguments).length&&Array.prototype.slice.call(arguments)[0]:Function.prototype.on||"undefined"==typeof console||"object"!=typeof console.log||Function.prototype.call.call(console.log,console,Array.prototype.slice.call(arguments)))},_determinepath:function(e){var t=this.options;if(t.behavior&&this["_determinepath_"+t.behavior]!==i)return this["_determinepath_"+t.behavior].call(this,e);if(t.pathParse)return this._debug("pathParse manual"),t.pathParse(e,this.options.state.currPage+1);if(e.match(/^(.*2?)\b2\b(.*?$)/))e=e.match(/^(.*2?)\b2\b(.*?$)/).slice(1);else if(e.match(/^(.*?)\b2\b(.*?$)/))e=e.match(/^(.*?)\b2\b(.*?$)/).slice(1);else if(e.match(/^(.*?)2(.*?$)/)){if(e.match(/^(.*?page=)2(\/.*|$)/))return e=e.match(/^(.*?page=)2(\/.*|$)/).slice(1);e=e.match(/^(.*?)2(.*?$)/).slice(1)}else{if(e.match(/^(.*?page=)1(\/.*|$)/))return e=e.match(/^(.*?page=)1(\/.*|$)/).slice(1);this._debug("Sorry, we couldn't parse your Next (Previous Posts) URL. Verify your the css selector points to the correct A tag. If you still get this error: yell, scream, and kindly ask for help at infinite-scroll.com."),t.state.isInvalidPage=!0}return this._debug("determinePath",e),e},_error:function(e){var t=this.options;t.behavior&&this["_error_"+t.behavior]!==i?this["_error_"+t.behavior].call(this,e):("destroy"!==e&&"end"!==e&&(e="unknown"),this._debug("Error",e),("end"===e||t.state.isBeyondMaxPage)&&this._showdonemsg(),t.state.isDone=!0,t.state.currPage=1,t.state.isPaused=!1,t.state.isBeyondMaxPage=!1,this._binding("unbind"))},_loadcallback:function(t,o,n){var a,s=this.options,r=this.options.callback,l=s.state.isDone?"done":s.appendCallback?"append":"no-append";if(s.behavior&&this["_loadcallback_"+s.behavior]!==i)this["_loadcallback_"+s.behavior].call(this,t,o,n);else{switch(l){case"done":return this._showdonemsg(),!1;case"no-append":if("html"===s.dataType&&(o=e(o="<div>"+o+"</div>").find(s.itemSelector)),0===o.length)return this._error("end");break;case"append":var c=t.children();if(0===c.length)return this._error("end");for(a=document.createDocumentFragment();t[0].firstChild;)a.appendChild(t[0].firstChild);this._debug("contentSelector",e(s.contentSelector)[0]),e(s.contentSelector)[0].appendChild(a),o=c.get()}if(s.loading.finished.call(e(s.contentSelector)[0],s),s.animate){var h=e(window).scrollTop()+e(s.loading.msg).height()+s.extraScrollPx+"px";e("html,body").animate({scrollTop:h},800,function(){s.state.isDuringAjax=!1})}s.animate||(s.state.isDuringAjax=!1),r(this,o,n),s.prefill&&this._prefill()}},_nearbottom:function(){var t=this.options,o=0+e(document).height()-t.binder.scrollTop()-e(window).height();return t.behavior&&this["_nearbottom_"+t.behavior]!==i?this["_nearbottom_"+t.behavior].call(this):(this._debug("math:",o,t.pixelsFromNavToBottom),o-t.bufferPx<t.pixelsFromNavToBottom)},_pausing:function(e){var t=this.options;if(!t.behavior||this["_pausing_"+t.behavior]===i){switch("pause"!==e&&"resume"!==e&&null!==e&&this._debug("Invalid argument. Toggling pause value instead"),e=!e||"pause"!==e&&"resume"!==e?"toggle":e){case"pause":t.state.isPaused=!0;break;case"resume":t.state.isPaused=!1;break;case"toggle":t.state.isPaused=!t.state.isPaused}return this._debug("Paused",t.state.isPaused),!1}this["_pausing_"+t.behavior].call(this,e)},_setup:function(){var e=this.options;if(!e.behavior||this["_setup_"+e.behavior]===i)return this._binding("bind"),!1;this["_setup_"+e.behavior].call(this)},_showdonemsg:function(){var t=this.options;t.behavior&&this["_showdonemsg_"+t.behavior]!==i?this["_showdonemsg_"+t.behavior].call(this):(t.loading.msg.find(".fusion-loading-spinner").hide().parent().find(".fusion-loading-msg").html(t.loading.finishedMsg).animate({opacity:1},2e3,function(){e(this).parent().fadeOut(t.loading.speed),e(this).parent().parent().find(".fusion-load-more-button").fadeOut(t.loading.speed)}),t.errorCallback.call(e(t.contentSelector)[0],"done"))},_validate:function(i){for(var t in i)if(t.indexOf&&t.indexOf("Selector")>-1&&0===e(i[t]).length)return this._debug("Your "+t+" found no elements."),!1;return!0},bind:function(){this._binding("bind")},destroy:function(){return this.options.state.isDestroyed=!0,this.options.loading.finished(),this._error("destroy")},pause:function(){this._pausing("pause")},resume:function(){this._pausing("resume")},beginAjax:function(t){var o,n,a,s,r=this,l=t.path;if(t.state.currPage++,t.maxPage!==i&&t.state.currPage>t.maxPage)return t.state.isBeyondMaxPage=!0,void this.destroy();switch(l="string"==typeof l?[l]:l,o=e(t.contentSelector).is("table, tbody")?e("<tbody/>"):e("<div/>"),n="function"==typeof l?l(t.state.currPage):l.join(t.state.currPage),r._debug("heading into ajax",n),a="html"===t.dataType||"json"===t.dataType?t.dataType:"html+callback",t.appendCallback&&"html"===t.dataType&&(a+="+callback"),a){case"html+callback":r._debug("Using HTML via .load() method"),o.load(n+" "+t.itemSelector,i,function(e){r._loadcallback(o,e,n)});break;case"html":r._debug("Using "+a.toUpperCase()+" via $.ajax() method"),e.ajax({url:n,dataType:t.dataType,complete:function(e,i){(s=void 0!==e.isResolved?e.isResolved():"success"===i||"notmodified"===i)?r._loadcallback(o,e.responseText,n):r._error("end")}});break;case"json":r._debug("Using "+a.toUpperCase()+" via $.ajax() method"),e.ajax({dataType:"json",type:"GET",url:n}).done(function(e,a,l){if(s=void 0!==l.isResolved?l.isResolved():"success"===a||"notmodified"===a,t.appendCallback)if(t.template!==i){var c=t.template(e);o.append(c),s?r._loadcallback(o,c):r._error("end")}else r._debug("template must be defined."),r._error("end");else s?r._loadcallback(o,e,n):r._error("end")}).fail(function(){r._debug("JSON ajax request failed."),r._error("end")})}},retrieve:function(t){t=t||null;var o=this.options;if(o.behavior&&this["retrieve_"+o.behavior]!==i)this["retrieve_"+o.behavior].call(this,t);else{if(o.state.isDestroyed)return this._debug("Instance is destroyed"),!1;o.state.isDuringAjax=!0,o.loading.start.call(e(o.contentSelector)[0],o)}},scroll:function(){var e=this.options,t=e.state;e.behavior&&this["scroll_"+e.behavior]!==i?this["scroll_"+e.behavior].call(this):t.isDuringAjax||t.isInvalidPage||t.isDone||t.isDestroyed||t.isPaused||this._nearbottom()&&this.retrieve()},toggle:function(){this._pausing()},unbind:function(){this._binding("unbind")},update:function(i){e.isPlainObject(i)&&(this.options=e.extend(!0,this.options,i))}},e.fn.infinitescroll=function(i,t){switch(typeof i){case"string":var o=Array.prototype.slice.call(arguments,1);this.each(function(){var t=e.data(this,"infinitescroll");return!!t&&(!(!e.isFunction(t[i])||"_"===i.charAt(0))&&void t[i].apply(t,o))});break;case"object":this.each(function(){var o=e.data(this,"infinitescroll");o?o.update(i):(o=new e.infinitescroll(i,t,this)).failed||e.data(this,"infinitescroll",o)})}return this};var t,o=e.event;o.special.smartscroll={setup:function(){e(this).on("scroll",o.special.smartscroll.handler)},teardown:function(){e(this).off("scroll",o.special.smartscroll.handler)},handler:function(i,o){var n=this,a=arguments;i.type="smartscroll",t&&clearTimeout(t),t=setTimeout(function(){e(n).trigger("smartscroll",a)},"execAsap"===o?0:100)}},e.fn.smartscroll=function(e){return e?this.on("smartscroll",e):this.trigger("smartscroll",["execAsap"])}});