(function ($) {
    $.fn.extend({
        MyPagination: function (options) {
            var defaults = {
                height: 2000,
                fadeSpeed: 400
            };
            var options = $.extend(defaults, options);

            //Creating a reference to the object
            var objContent = $(this);

            // other inner variables
            var fullPages = new Array();
            var subPages = new Array();
            var height = 0;
            var lastPage = 1;
            var paginatePages;

            // initialization function
            init = function () {
                objContent.children().each(function (i) {
                    if (height + this.clientHeight > options.height) {
                        fullPages.push(subPages);
                        subPages = new Array();
                        height = 0;
                    }

                    height += this.clientHeight;
                    subPages.push(this);
                });

                if (height > 0) {
                    fullPages.push(subPages);
                }

                // wrapping each full page
                $(fullPages).wrap("<div class='page'></div>");

                // hiding all wrapped pages
                objContent.children().hide();

                // making collection of pages for pagination
                paginatePages = objContent.children();

                // draw controls
                showPagination($(paginatePages).length);

                // show first page
                showPage(lastPage);
            };

            // update counter function
            updateCounter = function (i) {
                $('#page_number').html(i);

                $(".pagination li a").removeClass("selected");
                $(".pagination li:eq(" + i + ") a").addClass("selected");
            };

            // show page function
            showPage = function (page) {
                i = page - 1;
                if (paginatePages[i]) {

                    // hiding old page, display new one
                    $(paginatePages[lastPage]).fadeOut(options.fadeSpeed);
                    lastPage = i;
                    $(paginatePages[lastPage]).fadeIn(options.fadeSpeed);

                    // and updating counter
                    updateCounter(page);
                }
            };

            // show pagination function (draw switching numbers)
            showPagination = function (numPages) {
                var pagins = '';
                for (var i = 1; i <= numPages; i++) {
                    pagins += '<li><a href="javascript:void(0);" onclick="showPage(' + i + '); return false;">' + (i * 8) + '</a></li>';
                }
                $('.pagination li:first-child').after(pagins);
            };

            // perform initialization
            init();

            // and binding 2 events - on clicking to Prev
            $('.pagination #prev').click(function () {
                showPage(lastPage);
            });
            // and Next
            $('.pagination #next').click(function () {
                showPage(lastPage + 2);
            });

        }
    });
})(jQuery);

// custom initialization
jQuery(window).load(function() {
    $('#content').MyPagination({height: 2000, fadeSpeed: 400});
});
