/**
 *    @require jQuery 1.3
 *   
 */
var ReviewPagination = {
    setup: function() {
        $(document).ready( function() {
            /* pagination variables */
                //all pagintion item except previous and next
                top_paginator_div = '.top-pagination',
                bottom_paginator_div = '.bottom-pagination',
                number_of_items = $( top_paginator_div + ' ul li' ).length,
                top_pagination_items = $( top_paginator_div + ' ul li' ).not('.next').not('.prev'),
                bottom_pagination_items = $( bottom_paginator_div + ' ul li' ).not('.next').not('.prev'),
                top_pagination_links = $( 'a', top_pagination_items ),
                bottom_pagination_links = $( 'a', bottom_pagination_items ),
                lists = $('ul.review-list'), //ex ul.feed_lists
                previous_links = [ $( 'a', top_paginator_div + ' ul li:first'), $('a', bottom_paginator_div + ' ul li:first') ],
                next_links = [ $( 'a', top_paginator_div + ' ul li:last'), $('a', bottom_paginator_div + ' ul li:last') ],
                /**
                 * hides the current visible feed list, and show the requested list
                 *
                 * @param {number} index an integer indicating which item was click, and
                 *         consequently which list of posts should be shown
                 *
                 */
                switch_page = function( index ) {
                    /* css is used, because sometimes hide(), show() borks */
                    lists.filter(':visible').css('display','none');
                    lists.eq( index ).css('display', 'block');
                },
                /**
                 *    @type Array
                 */
                get_active_items = function() {
                    var arr = [top_pagination_items.filter('.active'), bottom_pagination_items.filter('.active') ];
                    return arr;
                },
                /**
                 * deactivates the current pagination item on both the top and bottom
                 * pagination navigations, and activate the click navigation item
                 *
                 * @param {number} index - an integer indication the index of the clicke anchore
                 * @param {jQueryObject} item - the current list item to make active
                 *
                 */
                switch_paginator_items = function( index, item ) {

                    jQuery.map( get_active_items(), function(item) {
                        item.removeClass('active');
                    });

                    top_pagination_items.eq( index  ).addClass('active');

                    bottom_pagination_items.eq( index ).addClass('active');

                    item.addClass('active');
                },
                /**
                 *  builds a function that calls the previous two
                 *  function by supplying the  index of the
                 *  clicked anchor
                 *
                 *  @param {jQueryObject} links - contains the links in a paginator list
                 *  @param {boolean} click_link - false says that the function should prevent the click
                 *                   event from processing the href
                 *
                 *  @return function that can response to a pagination click event
                 *    @type Function
                 */
                build_pagination_handler = function( links, click_link ) {
                    return function() {

                        var index = links.index( this );

                        switch_page( index );

                        switch_paginator_items( index, $(this).parent('li') );

                        return click_link;

                    }
                };


            //adds click handler to pagination links
            top_pagination_links.click( build_pagination_handler( top_pagination_links, false ) );
            bottom_pagination_links.click( build_pagination_handler( bottom_pagination_links, true ) );

            //add click events to previous links
            //default behavior clicks the bottom link (not the best solution)
            jQuery.map( previous_links, function( link ) {
                link.click( function() {

                    bottom_pagination_links.eq( ( bottom_pagination_items.index( get_active_items()[1] ) - 1 ) ).click();

                });
            });

            jQuery.map( next_links, function( link ) {
                link.click( function() {
                   
                    bottom_pagination_links.eq( ( bottom_pagination_items.index( get_active_items()[1] ) + 1 ) ).click();

                });
            });


        });
    }()

};
