/**
 @class Generic pager functionality
 @description After creating a Pager you need to attach it's container to the DOM yourself.
 @param [options]
 @param {String} options.selClass Classname used to indicate a selected page. Default: 'sel'
 @param {String} options.firstClass Classname used to indicate the first page. Default: 'first'
 */
netr.ui.Pager = function (options) {
	this.options = $.extend({}, netr.ui.Pager.defaultOptions, options || {});
	var t = this;
	this.container = $('<div class="pager"></div>');
	this.list = $('<ul>').appendTo(this.container);
	this.pages = [];
};
netr.ui.Pager.prototype = {
	/** 
	 Add a page to the pager
	 @param {String} format HTML string with this page's content
	 @param {Function} callback Function to execute when this page becomes active
	 */
	addPage: function (format, callback) {
		var t = this;
		var page = $('<li>');
		var index = this.pages.push(page) - 1;
		if (index === 0) {
			page.addClass(this.options.firstClass);
		}
		page.append(format);
		page.mouseenter(function (e) {
			e.preventDefault();
			t.showPage(index);
			callback(t, page, index);
		});
		page.find('a').focus(function (e) {
			e.preventDefault();
			t.showPage(index);
			callback(t, page, index);
		});
		page.appendTo(this.list);
	},
	/**
	 Show a page
	 @param {Number} page Index of the page you want to show
	 */
	showPage: function (page) {
		var t = this;
		// Don't iterate if page is the currently active page
		if (page !== this.activePage) {
			$.each(this.pages, function (index) {
				if (index == page) {
					this.addClass(t.options.selClass);
					t.activePage = page;
				} else {
					this.removeClass(t.options.selClass);
				}
			});
		}
	}
};
netr.ui.Pager.defaultOptions = {
	selClass: 'sel',
	firstClass: 'first'
};
