function Pagination(){
	this.settings = {
		'current_page': 1,
		'per_page': 20,
		'url': '',
		'total': 1,
		'loading_text': 'Loading...',
		'button_label': 'Show more results',
		'container': '.cache.current',
		'pagination': '.paginator'
	};
	this.sender = null;
	this.page_counter = {};
	this.page_total = {};
}

Pagination.prototype = {
	more: function(obj, url, total, loading_text){
		this.sender = jQuery(obj);
		this.settings.button_label = jQuery(obj).val();
		this.settings.loading_text = loading_text;
		this.settings.total = parseInt(total);
		this.page_total[this.settings.url] = parseInt(total);
		if (url != this.settings.url){
			this.settings.current_page = this.page_counter[this.settings.url];
			this.settings.total = this.page_total[this.settings.url];
		}
		this.settings.url = url;

		this.load();

		return false;
	},
	getUrl: function(){
		if (!(this.settings.url in this.page_counter)){
			this.page_counter[this.settings.url] = 1;
		}
		var url = this.settings.url + (this.page_counter[this.settings.url]+1);

		return url;
	},
	load: function(){
		var self = this;

		this.block();

		jQuery.ajax({
			dataType: 'json',
			data: {'ajax':1},
			url: self.getUrl(),
			success: function(response){
				if (response.error){
					messenger.error(response.error);
				}
				if (response.data || response.html){
					var html = response.data || response.html;
					jQuery(self.settings.container).find('div.mid').append(html);
				}
			},
			complete: function(response){
				self.unblock();
				self.page_counter[self.settings.url] += 1;
				if (self.page_counter[self.settings.url] >= self.page_total[self.settings.url]){
					self.sender.closest(self.settings.pagination).hide();
				}
			}
		});
	},
	block: function(){
		this.sender.attr('value', this.settings.loading_text);
		this.sender.attr('disabled', 'disabled');
	},
	unblock: function(){
		this.sender.attr('value', this.settings.button_label);
		this.sender.removeAttr('disabled');
	}
};
var pagination = new Pagination();

