
function Pagination(container, pageSize, classFilter, offset, hidePageableNotMatchingFilter) {
	
	var that = this;
	
	this .countPages = function () {
		return that.getPages() .size();
	}
	
	this .getOffset = function () {
		return offset;
	}
	
	this .setOffset = function (o) {
		offset = o;
		that.updatePager();
	}
	
	this .getPageSize = function () {
		return pageSize;
	}
	
	this .getPages = function () {
		var filter = '.pageable' + classFilter;
		return $(filter, container);
	}
	
	this .getContainer = function () {
		return container;
	}
	
	this .setClassFilter = function (s) {
		classFilter = s;
	}
	
	this .getHidePageablesNotMatchingFilter = function () {
		return hidePageableNotMatchingFilter;
	}
	
	this .updatePager = function () {
		$('.pager', container) .text(((offset / pageSize) + 1) + ' of ' + Math.ceil(that.countPages() / pageSize));
	}
	
	this .setNumItems = function (n) {
		numItems = n;
	}
	
	var hidePageableNotMatchingFilter = true;
	
	var container = container;
	
	var offset = offset ? offset : 0;
	
	var pageSize = pageSize ? pageSize : 3;
	
	var numItems;
	
	var classFilter = classFilter ? classFilter : '';
	
	var init = function () {
		if (! container) {
			throw 'Tried to initialize pagination for a non existing container!';
		}
		// Find all prev and next links:
		$('.next', container) .click(function (e) {
			that.next();
			return false;
		});
		$('.prev', container) .click(function (e) {
			that.prev();
			return false;
		});
		
		that.updateFilter(classFilter);
	}
	
	init();
}

Pagination.prototype.next = function () {
	var o = this .getOffset() + this .getPageSize();
	if (o >= this .countPages()) {
		o = this .getOffset();
	}
	this .setOffset(o);
	this .refresh();
}

Pagination.prototype.prev = function () {
	var o = this .getOffset() - this .getPageSize();
	if (o < 0) {
		o = 0;
	}
	this .setOffset(o);
	this .refresh();
}

Pagination.prototype.refresh = function () {
	var offset = this .getOffset();
	var pageSize = this .getPageSize();
	
	this .getPages() .each(function (i) {
		if (i >= offset && i < (offset + pageSize)) {
			$(this) .show();
		} else {
			$(this) .hide();
		}
	});
}

Pagination.prototype.updateFilter = function (newfilter) {
	if (this .getHidePageablesNotMatchingFilter()) {
		$('.pageable', this .getContainer()) .hide();
	}
	
	this .setClassFilter(newfilter);
	this .setNumItems(this .countPages());
	this .setOffset(0);
	this .refresh();
}