﻿var ListPager = Class.create();
ListPager.prototype = {
	initialize: function (curItem, pageCount, pagerContainer, callback) {
		this.curItem = curItem;
		this.pagerContainer = pagerContainer;
		this.pageCount = pageCount;
		this.callback = callback;
		this.loadPager();
	},
	loadPager: function () {
		if (this.pageCount == 0) return;
		if (this.curItem != 1) {
			var topArr = document.createElement("img");
			topArr.src = "/Images/wTArr.gif";
			topArr.onclick = this.loadPage.bindAsEventListener(this, this.curItem - 1);
			this.pagerContainer.appendChild(topArr);
		}
		var span = document.createElement("span");
		if (this.pageCount <= 13) for (var i = 1; i <= this.pageCount; i++) this.addPage(i, span);
		else {
			for (var i = 1; i <= 3; i++) this.addPage(i, span);
			if (this.curItem > 3 && this.curItem < this.pageCount - 5) {
				var first = (this.curItem > 7) ? this.curItem - 3 : 4;
				var last = (this.curItem > 7 && this.curItem) ? this.curItem + 3 : 10;
			} else {
				var first = (this.curItem <= 3) ? 4 : this.pageCount - 9;
				var last = (this.curItem <= 3) ? 10 : this.pageCount - 3;
			}
			for (var i = first; i <= last; i++) this.addPage(i, span);
			for (var i = this.pageCount - 2; i <= this.pageCount; i++) this.addPage(i, span);
		}
		this.pagerContainer.appendChild(span);
		if (this.curItem != this.pageCount) {
			var botArr = document.createElement("img");
			botArr.src = "/Images/wBArr.gif";
			botArr.onclick = this.loadPage.bindAsEventListener(this, this.curItem + 1);
			this.pagerContainer.appendChild(botArr);
		}
	},
	addPage: function (index, span) {
		if (index == this.pageCount - 2 && this.pageCount >= 13) {
			var img = document.createElement("img");
			img.src = "/Images/points2.gif";
			span.appendChild(img);
		}
		var a = document.createElement("a");
		a.href = "javascript:void(0)";
		a.appendChild(document.createTextNode(index));
		a.onclick = this.loadPage.bindAsEventListener(this, index);
		if (index == this.curItem) Element.addClassName(a, "active");
		span.appendChild(a);
		if (index == 3 && this.pageCount >= 13) {
			var img = document.createElement("img");
			img.src = "/Images/points2.gif";
			span.appendChild(img);
		}
	},
	loadPage: function (evt, index) {
		if (index != this.curItem) this.callback.call(this, this, index);
	}
};
