/*
*	Paginator
*	Paginates a list
*	
*	Requires Trapeze jQuery distribution
*	
*	Taylan Pince (tpince at trapeze dot com) - February 25, 2009
*/

$.namespace("trapeze.Paginator");

trapeze.Paginator = $.Class.extend({
    
    selector : "",
    active_page : 0,
    total_pages : 0,
    page_list : null,
    page_size : 1,
    query_key : "page",
    default_page : 1,
    
    page_template : '<div class="page-wrapper"></div>',
    page_list_template : '<p class="pagination-small"></p>',
    page_link_template : '<a href="javascript:void(0);" class="%(class) page-%(page)">%(page)</a>',
    page_prev_template : '<a href="javascript:void(0);">&lsaquo;</a>',
    page_next_template : '<a href="javascript:void(0);">&rsaquo;</a>',
    
    page_click : function(evt) {
        this.show_page($.data(evt.target, "page_number"));
    },
    
    show_previous_page : function() {
        if (this.active_page > 1) {
            this.show_page(parseInt(this.active_page) - 1);
        } else {
            this.show_page(this.total_pages);
        }
    },
    
    show_next_page : function() {
        if (this.active_page < this.total_pages) {
            this.show_page(parseInt(this.active_page) + 1);
        } else {
            this.show_page(1);
        }
    },
    
    show_page : function(page) {
        if (page != this.active_page) {
            var index = (page - 1) * this.page_size;
            var items = $(this.selector).find("ul").slice(index, index + this.page_size);
            var position = $(items).position();
            
            $(this.selector).children("div.page-wrapper").animate({
                "top" : -1 * position.top
            });
            
            var height = 0;
            
            for (var i = 0; i < items.size(); i++) {
                height += items.slice(i).height();
            }
            
            if (height != $(this.selector).height()) {
                $(this.selector).animate({
                    "height" : height
                });
            }
            
            this.page_list.find("a").removeClass("active");
            this.page_list.find("a.page-" + page).addClass("active");
            
            var params = {};
            params[this.query_key] = (page == this.default_page) ? "" : page;
            
            trapeze.update_query(params);
            
            this.active_page = page;
        }
    },
    
    setup : function() {
        this.total_pages = Math.ceil($(this.selector).children("ul").size() / this.page_size);
        
        $(this.selector).addClass("paginated").children().wrapAll(this.page_template);
        
        if (this.page_list) {
            this.page_list.remove();
        }
        
        this.page_list = $(this.page_list_template).insertAfter(this.selector);
        
        for (var i = 1; i <= this.total_pages; i++) {
            var page_link = $(trapeze.render_template(this.page_link_template, {
                "page" : i,
                "class" : (i == this.active_page) ? "active" : ""
            })).appendTo(this.page_list).get(0);
            
            $.data(page_link, "page_number", i);
        }
        
        this.page_list.find("a").click(this.page_click.bind(this));
        
        $(this.page_prev_template).prependTo(this.page_list).click(this.show_previous_page.bind(this));
        $(this.page_next_template).appendTo(this.page_list).click(this.show_next_page.bind(this));
        
        var params = trapeze.parse_query(window.location.toString());
        
        if (this.query_key in params) {
            this.show_page(params[this.query_key]);
        } else {
            this.show_page(1);
        }
    },
    
    init : function(selector, options) {
        this.selector = selector;
        
        if (options) {
            this.page_size = (options.page_size) ? options.page_size : 1;
            this.query_key = (options.query_key) ? options.query_key : "page";
        }
        
        this.setup();
    }
    
});
