/*
// Infinite Scroll jQuery plugin
// copyright Paul Irish, licensed GPL & MIT
// version 1.2.090804

// home and docs: http://www.infinite-scroll.com
*/
(function (A) {
    A.fn.infinitescroll = function (N, L) {
        function E() {
            if (B.debug) {
                window.console && console.log.call(console, arguments)
            }
        }
        function G(P) {
            for (var O in P) {
                if (O.indexOf && O.indexOf("Selector") && A(P[O]).length === 0) {
                    E("Your " + O + " found no elements.");
                    return false
                }
                return true
            }
        }
        function K(O) {
            O.match(C) ? O.match(C)[2] : O;
            if (O.match(/^(.*?)\b2\b(.*?$)/)) {
                O = O.match(/^(.*?)\b2\b(.*?$)/).slice(1)
            } else {
                if (O.match(/^(.*?)2(.*?$)/)) {
                    E("Trying backup next selector parse technique. Treacherous waters here, matey.");
                    O = O.match(/^(.*?)2(.*?$)/).slice(1)
                } else {
                    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.");
                    H.isInvalidPage = true
                }
            }
            return O
        }
        function I() {
            return B.localMode ? (A(H.container)[0].scrollHeight && A(H.container)[0].scrollHeight) : A(document).height()
        }
        function F(Q, P) {
            var O = I() - (Q.localMode ? A(P.container).scrollTop() : (A(P.container).scrollTop() || A(P.container.ownerDocument.body).scrollTop())) - A(Q.localMode ? P.container : window).height();
            E("math:", O, P.pixelsFromNavToBottom);
            return (O - Q.bufferPx < P.pixelsFromNavToBottom)
        }
        function J() {
            H.loadingMsg.find("img").hide().parent().find("div").html(B.donetext).animate({
                opacity: 1
            }, 2000).fadeOut("normal");
            B.errorCallback()
        }
        function D(R, Q, O, S) {
            if (O.isDuringAjax || O.isInvalidPage || O.isDone) {
                return
            }
            if (!F(Q, O)) {
                return
            }
            O.isDuringAjax = true;
            O.loadingMsg.appendTo(Q.contentSelector).show();
            A(Q.navSelector).hide();
            O.currPage++;
            E("heading into ajax", R);
            var P = A(Q.contentSelector).is("table") ? A("<tbody/>") : A("<div/>");
            P.attr("id", "infscr-page-" + O.currPage).addClass("infscr-pages").appendTo(Q.contentSelector).load(R.join(O.currPage) + " " + Q.itemSelector, null, function () {
                if (O.isDone) {
                    J();
                    return false
                } else {
                    if (P.children().length == 0) {
                        A.event.trigger("ajaxError", [{
                            status: 404
                        }])
                    }
                    O.loadingMsg.fadeOut("normal");
                    if (Q.animate) {
                        var T = A(window).scrollTop() + A("#infscr-loading").height() + Q.extraScrollPx + "px";
                        A("html,body").animate({
                            scrollTop: T
                        }, 800, function () {
                            O.isDuringAjax = false
                        })
                    }
                    S.call(P[0]);
                    if (!Q.animate) {
                        O.isDuringAjax = false
                    }
                }
            })
        }
        var B = A.extend({}, A.infinitescroll.defaults, N);
        var H = A.infinitescroll;
        L = L ||
        function () {};
        if (!G(B)) {
            return false
        }
        H.container = B.localMode ? this : document.documentElement;
        B.contentSelector = B.contentSelector || this;
        var C = /(.*?\/\/).*?(\/.*)/;
        var M = A(B.nextSelector).attr("href");
        if (!M) {
            E("Navigation selector not found");
            return
        }
        M = K(M);
        if (B.localMode) {
            A(H.container)[0].scrollTop = 0
        }
        H.pixelsFromNavToBottom = I() + A(H.container).offset().top - A(B.navSelector).offset().top;
		
        H.loadingMsg = A('<div id="infscr-loading" style="text-align: center;"><img alt="Loading..." src="' + B.loadingImg + '" /><div>' + B.loadingText + "</div></div>");
        (new Image()).src = B.loadingImg;
        A(document).ajaxError(function (P, Q, O) {
            E("Page not found. Self-destructing...");
            if (Q.status == 404) {
                J();
                H.isDone = true;
                A(B.localMode ? this : window).unbind("scroll.infscr")
            }
        });
        A(B.localMode ? this : window).bind("scroll.infscr", function () {
            D(M, B, H, L)
        }).trigger("scroll.infscr");
        return this
    };
    A.infinitescroll = {
        defaults: {
            debug: false,
            preload: false,
            nextSelector: "div.navigation a:first",
            loadingImg: "images/ajax-loader1.gif",
            loadingText: "<em>Loading the next set...</em>",
            donetext: "<em>End of portfolio entries.</em>",
            navSelector: "div.navigation",
            contentSelector: null,
            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);
