NetR = typeof NetR === 'undefined' ? {} : NetR;

NetR.Pager = function (options) {
	this.options = $.extend({}, NetR.Pager.defaultOptions, options || {});
	var t = this;
	this.elements = {};
	this.elements.container = $('<div class="paging cf"></div>');
	this.elements.p = $('<p></p>');
	this.elements.heading = $('<strong class="structural">' + this.options.heading + '</strong>');
	this.elements.links = [];
	this.elements.container.append(this.elements.p.append(this.elements.heading));
	
	for(var index = 0; index < this.options.numberOfPages; index++) {
		this.createLink(index);
	}
	
	this.showPage(1);
	
};
NetR.Pager.prototype = {
	createLink: function (index) {
		var t = this,
		    link = $('<a href="/"><span>' + (index + 1) + '</span></a>');
		link.click(function (e) {
			e.preventDefault();
			t.showPage(index + 1);
		});
		this.elements.links[index] = link;
		this.elements.p.append(link);
		if (typeof this.options.onLinkSetup == 'function') {
			this.options.onLinkSetup(this, link, index);
		}
	},
	showPage: function(pageNum) {
		var t = this;
		// Clear previously selected elements
		$.each(this.elements.links, function (index) {
			this.removeClass(NetR.Pager.selectedClass);
		});
		// Select the chosen page
		this.elements.links[pageNum - 1].addClass(this.options.selectedClass);
		if (typeof this.options.onPageShow == 'function') {
			this.options.onPageShow(t, this.elements.links[pageNum - 1], pageNum);
		}
	}
};
NetR.Pager.defaultOptions = {
	numberOfPages: 3,
	heading: 'Visa sida:',
	selectedClass: 'sel',
	onLinkSetup: function (pager, link, index) {},
	onPageShow: function (pager, link, pageNum) {}
};
