﻿
var ClientPaging = Class.create();

ClientPaging.prototype = {

	mintTotalItems: 0, /* The total number of items available to display  */
	mintPageIndex: 1, /* The currently selected page */
	mintMaxPageLink: 5, /* The number of page links to display */
	mintItemsPerPage: 10, /* The maximum number of items to display per page */
	mintPageTotal: 0, /* The total number of pages required to display all of the items -Calculated- */
	mobjLi: null,
	mOnPageChange: null,
	mParentObject: null,
	mParent: null,
	mintFirst: 1,
	mintLast: 0,
	mintPrev: 0,
	mintNext: 0,
	mintPageEnd: 0,
	mLocalisationSet: null,
	mAuto: false,
	mItems: null,
	mblnDisplayFirst: true,
	mblnDisplayLast: true,
	mblnDisplayNext: true,
	mblnDisplayPrevious: true,

	/*
	parentUl, The ul to append the paging toolbar to.
	intTotalItems, The number of items in total (pass null if in auto mode (e.g. items is set).
	onPageChange, The function to call when paging is changed (pass null if in auto mode). 
	localisationSet, The localistion data set (pass null to use default).
	items, The list of items to page (sets mode to auto).
	intItemsPerPage, The number of items per page.
	intMaxPageLink, The number of page links.
	*/
	initialize: function(parentUl, intTotalItems, onPageChange, localisationSet, items, intItemsPerPage, intMaxPageLink, displayFirst, displayLast, displayPrevious, displayNext, parentObject) {
		this.mobjLi = $CE('li', { className: 'ActivePager' }, {});
		this.mParent = parentUl;

		if ((typeof (localisationSet) != 'undefined') && (localisationSet != null)) {
			this.mLocalisationSet = localisationSet;
		}
		else {
			this.mLocalisationSet = { Page: 'Page', GoToFirst: 'Go to first page', GoToLast: 'Go to last page', GoToNext: 'Go to next page', GoToPrev: 'Go to prev page', Displaying: 'Displaying', Of: 'of', To: 'to' };
		}
		if ((typeof (items) != 'undefined') && (items != null)) {
			this.mAuto = true;
			this.mItems = items;
		}
		if ((typeof (intItemsPerPage) != 'undefined') && (intItemsPerPage != null)) {
			this.mintItemsPerPage = intItemsPerPage;
		}
		if ((typeof (intMaxPageLink) != 'undefined') && (intMaxPageLink != null)) {
			this.mintMaxPageLink = intMaxPageLink;
		}
		if (typeof (displayFirst) == 'boolean') {
			this.mblnDisplayFirst = displayFirst;
		}
		if (typeof (displayLast) == 'boolean') {
			this.mblnDisplayLast = displayLast;
		}
		if (typeof (displayNext) == 'boolean') {
			this.mblnDisplayNext = displayNext;
		}
		if (typeof (displayPrevious) == 'boolean') {
			this.mblnDisplayPrevious = displayPrevious;
		}
		if ((typeof (parentObject) != 'undefined') && (parentObject != null)) {
			this.mParentObject = parentObject;
		}
		parentUl.appendChild(this.mobjLi);

		if (this.mAuto == true) {
			this._onPageChange(1);
		}
		else {
			this.SetupPaging(intTotalItems);
			this.mOnPageChange = onPageChange;
		}

	},
	SetupPaging: function(intTotalItems) {
		var intMaxPage = this.mintMaxPageLink;

		if ((typeof (intTotalItems) != 'undefined') && (intTotalItems != null)) {
			this.mintTotalItems = intTotalItems;
		}

		while (this.mobjLi.firstChild) {
			this.mobjLi.removeChild(this.mobjLi.firstChild);
		}

		if (this.mintTotalItems > 0) {
			this.mintPageTotal = Math.ceil(this.mintTotalItems / this.mintItemsPerPage);

			this.mintFirst = 1;
			this.mintLast = this.mintPageTotal;

			this.mintPrev = this.mintPageIndex - 1;
			if (this.mintPrev < 1) this.mintPrev = 1;

			this.mintNext = this.mintPageIndex + 1;
			if (this.mintNext > this.mintPageTotal) this.mintNext = this.mintPageTotal;

			if (intMaxPage > this.mintPageTotal || intMaxPage < 1) {
				intMaxPage = this.mintPageTotal;
			}

			this.mintPageEnd = Math.min(Math.ceil(Math.max(this.mintPageIndex - (intMaxPage * 0.5), 1)) + intMaxPage, this.mintPageTotal);
			var intPageStart = Math.max(this.mintPageEnd - intMaxPage, 1);

			this.SetupCount();
			if (this.mblnDisplayFirst) {
				this.SetupLink(this.mintFirst, 'first', this.mLocalisationSet.GotToFirst, '&lt;&lt;');
			}
			if (this.mblnDisplayPrevious) {
				this.SetupLink(this.mintPrev, 'prev', this.mLocalisationSet.GoToPrev, '&lt;');
			}
			for (var intIndex = intPageStart; intIndex <= this.mintPageEnd; intIndex++) {
				this.SetupLink(intIndex, 'page', this.mLocalisationSet.Page + ' ' + intIndex, intIndex.toString(), (intIndex == this.mintPageEnd))
			}
			if (this.mblnDisplayNext) {
				this.SetupLink(this.mintNext, 'next', this.mLocalisationSet.GoToNext, '&gt;');
			}
			if (this.mblnDisplayLast) {
				this.SetupLink(this.mintLast, 'last', this.mLocalisationSet.GoToLast, '&gt;&gt;');
			}
		}
	},
	SetupCount: function() {
		var objA = $CE('a', { name: '', className: 'Total Last' }, {});
		var intPageStart = (this.mintItemsPerPage * (this.mintPageIndex - 1)) + 1;
		var intPageEnd = Math.min((intPageStart - 1) + this.mintItemsPerPage, this.mintTotalItems);

		objA.innerHTML = '<em>' + this.mLocalisationSet.Displaying + ' ' + intPageStart.toString() + ' ' + this.mLocalisationSet.To + ' ' + intPageEnd.toString() + ' ' + this.mLocalisationSet.Of + ' ' + this.mintTotalItems.toString() + '</em>';
		this.mobjLi.appendChild(objA);
	},
	SetupLink: function(intIndex, strClass, strTitle, strText, blnIsLast) {
		var objA = $CE('a', { title: strTitle }, {});
		objA.innerHTML = strText;
		objA.className = strClass;
		objA.title = strTitle;
		if (this.mintPageIndex == intIndex) {
			objA.className += ' Disabled';
		}
		else {
			Event.observe(objA, 'click', this.SetActivePage.bindAsEventListener(this, intIndex, true), false);
		}
		if (blnIsLast) {
			objA.className += ' Last';
		}
		this.mobjLi.appendChild(objA);
	},
	SetActivePage: function(sender, intPage, raiseEvent) {
		this.mintPageIndex = intPage;
		if ((raiseEvent) && (this.mOnPageChange)) {
			var intEndIndex = this.mintItemsPerPage * intPage;
			var intStartIndex = intEndIndex - this.mintItemsPerPage;
			intEndIndex = Math.min(intEndIndex, this.mintTotalItems);
			if (this.mParentObject == null) {
				this.mOnPageChange(intPage, intStartIndex, intEndIndex);
			}
			else {
				var fOnPageChange = this.mOnPageChange.bind(this.mParentObject);
				fOnPageChange(intPage, intStartIndex, intEndIndex, this.mParentObject);
			}
		}
		else if ((raiseEvent) && (this.mAuto == true)) {
			this._onPageChange(intPage);
		}
	},
	_onPageChange: function(intPage) {
		var intCount = this.mItems.length;
		if (intCount > 0) {

			var endIndex = this.mintItemsPerPage * intPage;
			var startIndex = endIndex - this.mintItemsPerPage;

			this.mItems.each(function(node, index) {
				if (node == this.mobjLi) {
					intCount--;
				}
				else if (index >= startIndex && index < endIndex && index < intCount) {
					node.show();
				}
				else {
					node.hide();
				}
			});
		}
		this.SetupPaging(intCount);
	},
	SetActiveItem: function(intItemIndex) {

		var endIndex = this.mintItemsPerPage * this.mintPageIndex;
		var startIndex = endIndex - this.mintItemsPerPage;
		var intPage = 1;

		if ((intItemIndex > endIndex) || (intItemIndex < startIndex)) {
			endIndex = this.mintItemsPerPage * intPage;
			startIndex = endIndex - this.mintItemsPerPage;
			while ((intItemIndex > endIndex) || (intItemIndex < startIndex)) {
				intPage++;
				endIndex = this.mintItemsPerPage * intPage;
				startIndex = endIndex - this.mintItemsPerPage;
			}
			this.SetActivePage(this, intPage, true);
		}

	}
};

        