﻿(function($) {
    $.fn.pwPagination = function(options) {

        var opts = $.extend({}, $.fn.pwPagination.defaults, options);

        var _contain,
            _items,
            _start = opts.start_page,
            _pages;

        return this.each( function() {

            _contain = $(this);

            _start = opts.start_page;

            _items = $(opts.items);

            if(_items.length <= opts.items_per_page)
                return _contain;

            _pages = Math.ceil((_items.length / opts.items_per_page));

            if(_start > _pages)
                _start = _pages;

            create_pagination_links(_start);

            $('li.'+ opts.list_item_class).bind("click", function() {

                var _current = $(this).attr("rel");

                $('li.'+ opts.list_item_class).removeClass(opts.active_class).filter(":not(.prev,.next)[rel="+ _current +"]").addClass(opts.active_class);

                update_links(_current);

                animate_items(_current);

            }).filter(":first").click();

            return _contain;

        });

        function update_links(_current) {

            _current = parseInt(_current);

            var _prev = ((_current - 1) < 1) ? 1 : _current - 1 ;

            var _next = ((_current + 1) > _pages) ? _pages : _current + 1 ;

            $('li.'+ opts.list_item_class +'.prev').attr("rel", _prev);

            $('li.'+ opts.list_item_class +'.next').attr("rel", _next);

        }

        function create_pagination_links(_current) {

            $('.'+ opts.contain_class).remove();

            var _surround = $('<div />').attr("class", opts.contain_class +' light-grey-bar'); // lazy

            var _list = $('<ul></ul>').attr("class", opts.list_class);

            var _previous = ((_current - 1) < 1) ? 1 : _current - 1 ;
            $('<li></li>').attr("class", opts.list_item_class +" prev").attr("rel", _previous).html("").appendTo(_list);

            for(var i = 1; i <= _pages; i++)
                $('<li></li>').attr("class", opts.list_item_class).attr("rel", i).text(i).appendTo(_list);

            var _next = ((_current + 1) > _pages) ? _pages : _current + 1 ;
            $('<li></li>').attr("class", opts.list_item_class +" next").attr("rel", _next).html("").appendTo(_list);

            _surround.append(_list);

            if(opts.menu_before)
                _contain.before(_surround.clone().addClass("top"));

            if(opts.menu_after)
                _contain.after(_surround.clone().addClass("bottom"));

            animate_items(_current);

        };

        function animate_items(_current) {

            var _start = ((_current - 1) * opts.items_per_page); // (page_num x items_per_page) = starting point
            var _end = _start + opts.items_per_page; // start + items_per_page = end (one page)

            $(opts.items).hide(0).slice(_start, _end).show(0);

        };
    };
    $.fn.pwPagination.defaults = {
        items : 'div',
        items_per_page : 25,
        start_page : 1,
        menu_before : true,
        menu_after : true,
        contain_class : 'paginate',
        list_class : 'paginate',
        list_item_class : 'paginate',
        active_class : 'active'
    };
})(jQuery);
