/* Controls the pagination currently in use for Customer Care */
function customerCarePager() {
    var ccPager = {
        initialize: function() {
            $(document).ready(function() {
                pager = {
                    pagination: $('<div id="pagination"><div class="toFirst"><<</div><div class="prev"><a href="javascript:void(0);">Previous</a></div><div class="pageCount"><span class="low"></span><span class="high"></span></div><div class="next"><a href="javascript:void(0);">Next Page</a></div><div class="toLast">>></div></div>')
                    ,isPaginated: $('.paginated', FS.dom.primaryContent)
                }
                var groupStore = pager.isPaginated.html();
                ccPager.onPager(groupStore);
                $(window).resize($.debounce(300,
                function() {
                    ccPager.onPager(groupStore);
                }));
            });
        },

        buildPagination: function() {
            FS.dom.contentPadding.before(pager.pagination);
        },

        onPager: function(groupStore) {
            pager.isPaginated.empty().html(groupStore).width('auto');

            var groups = $('.ccColumn', pager.isPaginated)
            ,pagerSize = FS.respond.currentThreshold == '0' || FS.respond.currentThreshold == '1' ? 1: FS.respond.currentThreshold;

            for (var i = 0; i < groups.length; i += pagerSize) {
                groups.slice(i, i + pagerSize).wrapAll('<div class="pageGroup" />');

            }
            var pageGroup = $('.pageGroup'),
            needsPaginated = pageGroup.length > 1;
            if (needsPaginated && pager.isPaginated.length) {
                ccPager.buildPagination();
                pageGroup.append('<div class="clearfix bottomPager">CONTINUE TO NEXT PAGE &gt;</div>');
            } else if (pager.pagination) {
                pager.pagination.remove();
            }
            
            $('.bottomPager', pageGroup).last().hide();
            if (needsPaginated) {
                pager.isPaginated.cycle({
                    fx: 'none'
                    ,prev: '#pagination .prev'
                    ,next: '#pagination .next, #pagination .toFirst, #primaryContent .pageGroup .bottomPager'
                    ,timeout: 0
                    ,after: function() {
                        pageGroup.removeClass('activePage');
                        $(this).addClass('activePage');
                        ccPager.onAfter(pageGroup);
                    }
                });
            }
        },

        onAfter: function(pageGroup) {
            var thisPage = $('.activePage', FS.dom.primaryContent).index() + 1
            ,high = $('.high', pager.pagination)
            ,low = $('.low', pager.pagination)
            ,pageLength = pageGroup.length
            ,isLast = pageLength === thisPage ? 1: 0
            ,isFirst = thisPage === 1 ? 1: 0
            ,toFirst = $('.toFirst', pager.pagination)
            ,toLast = $('.toLast', pager.pagination);

            toLast.css('visibility', 'visible');
            toFirst.css('visibility', 'visible');

            high.text(pageLength);
            low.text('Page ' + thisPage + '/');
            if (isLast) {
                toLast.css('visibility', 'hidden');
            } else if (isFirst) {
                toFirst.css('visibility', 'hidden');
            }

            toFirst.click(function() {
                pager.isPaginated.cycle(0);
            });
            toLast.click(function() {
                var cycleLast = pageLength - 1;
                pager.isPaginated.cycle(cycleLast);
            });


        }
    }

    return ccPager.initialize();
}
customerCarePager();
