/*!
// Infinite Scroll jQuery plugin
// copyright Paul Irish, licensed GPL & MIT
// version 1.5.101115

// home and docs: http://www.infinite-scroll.com
*/
(function (a) {
    a.fn.infinitescroll = function (r, o) {
        function e() {
            if (b.debug) {
                window.console && console.log.call(console, arguments)
            }
        }
        function h(t) {
            for (var s in t) {
                if (s.indexOf && s.indexOf("Selector") > -1 && a(t[s]).length === 0) {
                    e("Your " + s + " found no elements.");
                    return false
                }
                return true
            }
        }
        function n(s) {
            s.match(c) ? s.match(c)[2] : s;
            if (s.match(/^(.*?)\b2\b(.*?$)/)) {
                s = s.match(/^(.*?)\b2\b(.*?$)/).slice(1)
            } else {
                if (s.match(/^(.*?)2(.*?$)/)) {
                    if (s.match(/^(.*?page=)2(\/.*|$)/)) {
                        s = s.match(/^(.*?page=)2(\/.*|$)/).slice(1);
                        return s
                    }
                    e("Trying backup next selector parse technique. Treacherous waters here, matey.");
                    s = s.match(/^(.*?)2(.*?$)/).slice(1)
                } else {
                    if (s.match(/^(.*?page=)1(\/.*|$)/)) {
                        s = s.match(/^(.*?page=)1(\/.*|$)/).slice(1);
                        return s
                    }
                    e("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.");
                    k.isInvalidPage = true
                }
            }
            return s
        }
        function l() {
            return b.localMode ? (a(k.container)[0].scrollHeight && a(k.container)[0].scrollHeight) : a(document).height()
        }
        function f() {
            var s = 0 + l() - (b.localMode ? a(k.container).scrollTop() : (a(k.container).scrollTop() || a(k.container.ownerDocument.body).scrollTop())) - a(b.localMode ? k.container : window).height();
            e("math:", s, k.pixelsFromNavToBottom);
            return (s - b.bufferPx < k.pixelsFromNavToBottom)
        }
        function m() {
            k.loadingMsg.find("img").hide().parent().find("div").html(b.donetext).animate({
                opacity: 1
            }, 2000, function () {
                a(this).parent().slideUp("normal")
            });
            b.errorCallback()
        }
        function d() {
            if (k.isDuringAjax || k.isInvalidPage || k.isDone) {
                return
            }
            if (!f(b, k)) {
                return
            }
            a(document).trigger("retrieve.infscr")
        }
        function g() {
            k.isDuringAjax = true;
            k.loadingMsg.appendTo(b.loadMsgSelector).show(b.loadingMsgRevealSpeed, function () {
                a(b.navSelector).hide();
                k.currPage++;
                e("heading into ajax", q);
                j = a(b.contentSelector).is("table") ? a("<tbody/>") : a("<div/>");
                p = document.createDocumentFragment();
                j.load(q.join(k.currPage) + " " + b.itemSelector, null, i)
            })
        }
        function i() {
            if (k.isDone) {
                m();
                return false
            } else {
                var t = j.children();
                if (t.length == 0 || t.hasClass("error404")) {
                    return a.event.trigger("ajaxError", [{
                        status: 404
                    }])
                }
                while (j[0].firstChild) {
                    p.appendChild(j[0].firstChild)
                }
                a(b.contentSelector)[0].appendChild(p);
                k.loadingMsg.slideUp("normal");
                if (b.animate) {
                    var s = a(window).scrollTop() + a("#infscr-loading").height() + b.extraScrollPx + "px";
                    a("html,body").animate({
                        scrollTop: s
                    }, 800, function () {
                        k.isDuringAjax = false
                    })
                }
                o.call(a(b.contentSelector)[0], t.get());
                if (!b.animate) {
                    k.isDuringAjax = false
                }
            }
        }
        a.browser.ie6 = a.browser.msie && a.browser.version < 7;
        var b = a.extend({}, a.infinitescroll.defaults, r),
            k = a.infinitescroll,
            j, p;
        o = o ||
        function () {};
        if (!h(b)) {
            return false
        }
        k.container = b.localMode ? this : document.documentElement;
        b.contentSelector = b.contentSelector || this;
        b.loadMsgSelector = b.loadMsgSelector || b.contentSelector;
        var c = /(.*?\/\/).*?(\/.*)/,
            q = a(b.nextSelector).attr("href");
        if (!q) {
            e("Navigation selector not found");
            return
        }
        q = n(q);
        if (b.localMode) {
            a(k.container)[0].scrollTop = 0
        }
		
		var webkitStyle = '';
		
		if (a.browser.webkit) {
			webkitStyle = 'position:absolute;';
		}
		
        k.pixelsFromNavToBottom = l() + (k.container == document.documentElement ? 0 : a(k.container).offset().top) - a(b.navSelector).offset().top;
        k.loadingMsg = a('<div id="infscr-loading" style="text-align: center;'+webkitStyle+'"><img alt="Loading..." src="' + b.loadingImg + '" /><div>' + b.loadingText + "</div></div>");
        (new Image()).src = b.loadingImg;
        a(document).ajaxError(function (t, u, s) {
            e("Page not found. Self-destructing...");
            if (u.status == 404) {
                m();
                k.isDone = true;
                a(b.localMode ? this : window).unbind("scroll.infscr")
            }
        });
        a(b.localMode ? this : window).bind("scroll.infscr", d).trigger("scroll.infscr");
        a(document).bind("retrieve.infscr", g);
        return this
    };
    a.infinitescroll = {
        defaults: {
            debug: false,
            preload: false,
            nextSelector: "div.navigation a:first",
            loadingImg: "http://www.infinite-scroll.com/loading.gif",
            loadingText: "<em>Loading the next set of posts...</em>",
            donetext: "<em>Congratulations, you've reached the end of the internet.</em>",
            navSelector: "div.navigation",
            contentSelector: null,
            loadMsgSelector: null,
            loadingMsgRevealSpeed: "fast",
            extraScrollPx: 150,
            itemSelector: "div.post",
            animate: false,
            localMode: false,
            bufferPx: 40,
            errorCallback: function () {}
        },
        loadingImg: undefined,
        loadingMsg: undefined,
        container: undefined,
        currPage: 1,
        currDOMChunk: null,
        isDuringAjax: false,
        isInvalidPage: false,
        isDone: false
    }
})(jQuery);
