/**
 * Render the pager according to the ListView.pager template and add listeners on it.
 * Set this.$pager with the produced jQuery element
 * @param {jQuery} [$node] a jQuery node where the rendered pager should be inserted
 * $node may be undefined, in which case the ListView inserts the pager into this.options.$pager
 * or into a div of its template
 */
render_pager: function($node) {
    if (!this.$pager && this.options.pager) {
        var self =  this;
 
        this.$pager = $(QWeb.render("ListView.pager", {'widget': self}));
        this.$pager
            .on('click', 'a[data-pager-action]', function () {
                var $this = $(this);
                var max_page_index = Math.ceil(self.dataset.size() / self._limit) - 1;
                switch ($this.data('pager-action')) {
                    case 'first':
                        self.page = 0;
                        break;
                    case 'last':
                        self.page = max_page_index;
                        break;
                    case 'next':
                        self.page += 1;
                        break;
                    case 'previous':
                        self.page -= 1;
                        break;
                }
                if (self.page < 0) {
                    self.page = max_page_index;
                } else if (self.page > max_page_index) {
                    self.page = 0;
                }
                self.reload_content();
            }).find('.oe_list_pager_state')
                .click(function (e) {
                    e.stopPropagation();
                    var $this = $(this);
 
                    var $select = $('<select>')
                        .appendTo($this.empty())
                        .click(function (e) {e.stopPropagation();})
                        .append('<option value="80">80</option>' +
                                '<option value="200">200</option>' +
                                '<option value="500">500</option>' +
                                '<option value="2000">2000</option>' +
                                '<option value="NaN">' + _t("Unlimited") + '</option>')
                        .change(function () {
                            var val = parseInt($select.val(), 10);
                            self._limit = (isNaN(val) ? null : val);
                            self.page = 0;
                            self.reload_content();
                        }).blur(function() {
                            $(this).trigger('change');
                        })
                        .val(self._limit || 'NaN');
                });
        this.configure_pager(this.dataset);
 
        $node = $node || this.options.$pager;
        if ($node) {
            this.$pager.appendTo($node);
        } else {
            this.$('.oe_list_pager').replaceWith(this.$pager);
        }
    }
}