﻿function ListViewPagination(objListView)
{
	var THIS = this;
	this.ListView = objListView;
	this.PageSize = 5;
	this.PageIndex = 0;
	this.PageCount = 0;
	this.Items = new Array();

	this.Container = document.createElement("DIV");
	this.Container.className = "pagination";
	this.Container.style.display = "none";

	this.PaginationPrefix = document.createElement("SPAN");
	this.PaginationPrefix.innerHTML = "Pages: ";
	this.PaginationPrefix.className = "paginationPrefix";
	this.Container.appendChild(this.PaginationPrefix);

	this.PaginationPostfix = document.createElement("SPAN");
	this.PaginationPostfix.innerHTML = " of ";
	this.PaginationPostfix.className = "paginationPostfix";
	this.Container.appendChild(this.PaginationPostfix);

	var cboPageSize = document.createElement("SELECT");
	cboPageSize.className = "pageSize";
	cboPageSize.onchange = cboPageSize_Change;
	this.Container.appendChild(cboPageSize);

	this.PageSizeSuffix = document.createElement("SPAN");
	this.PageSizeSuffix.innerHTML = " items per page";
	this.Container.appendChild(this.PageSizeSuffix);

	AddPageSize(3);
	AddPageSize(5);
	AddPageSize(10);
	AddPageSize(15);
	AddPageSize(20);
	AddPageSize(50);

	cboPageSize.value = this.PageSize;

	var arrPages = new Array();
	var intPageSize = 0;
	var intPageCount = 0;
	var intPageIndex = 0;

	var recordset = null;

	//====================================================
	// Add a page size to the drop down
	//====================================================
	function AddPageSize(intPageSize)
	{
		try
		{
			var optPageSize = new Option();
			optPageSize.value = intPageSize;
			optPageSize.text = intPageSize;

			cboPageSize.options.add(optPageSize);
		}
		catch (exc)
		{
			HtmlPopup.Error(exc);
		}
	}

	//====================================================
	// The page size has changed
	//====================================================
	function cboPageSize_Change()
	{
		try
		{
			var optPageSize = cboPageSize.options[cboPageSize.selectedIndex];
			var intPageSize = Number(optPageSize.value);

			THIS.PageSize = intPageSize;
			THIS.PageIndex = 0;
			THIS.Refresh();
		}
		catch (exc)
		{
			HtmlPopup.Error(exc);
		}
	}

	//====================================================
	// import the data
	//====================================================
	this.Import = function Import(strXML)
	{
		try
		{
			intPageCount = 0;
			intPageIndex = 0;
			intPageSize = 0;

			recordset = new Recordset();
			recordset.LoadXML(strXML);

			this.Refresh();
		}
		catch (exc)
		{
			HtmlPopup.Error(exc);
		}
	}

	//====================================================
	// refresh the items
	//====================================================
	this.Refresh = function Refresh()
	{
		try
		{
			var intPageIndexTemp = this.PageIndex;
			var intPageCountTemp = this.PageCount;

			this.ListView.Clear();

			this.PageIndex = intPageIndexTemp;
			this.PageCount = intPageCountTemp;

			var intIndex = 0;
			recordset.MoveFirst();

			while (recordset.EOF() == false)
			{
				if (CanDisplayItem(intIndex))
				{
					if (this.ListView.ValidateItem == null || this.ListView.ValidateItem(recordset) == true)
						this.ListView.Add(recordset);
				}

				intIndex++;
				recordset.MoveNext();
			}

			SetPageCount(intIndex);

			if (intPageCount != this.PageCount || intPageIndex != this.PageIndex || intPageSize != this.PageSize)
			{
				//remove the existing pages
				while (arrPages.length > 0)
				{
					var objPage = arrPages.shift();
					objPage.Remove();
				}

				intPageCount = this.PageCount;
				intPageIndex = this.PageIndex;
				intPageSize = this.PageSize;

				if (intPageCount == 0)
					this.Container.style.display = "none";
				else
					this.Container.style.display = "";

				if (intPageIndex > 0)
					arrPages.push(new ListViewPage(this, -2));

				//add the new pages
				for (var intPage = 0; intPage < intPageCount; intPage++)
				{
					arrPages.push(new ListViewPage(this, intPage));
				}

				if (intPageIndex < intPageCount - 1)
					arrPages.push(new ListViewPage(this, -1));

				this.PaginationPostfix.innerHTML = "of " + intPageCount + " page(s); ";
				cboPageSize.value = this.PageSize;
			}
		}
		catch (exc)
		{
			HtmlPopup.Error(exc);
		}
	}

	//====================================================
	// Move to the next page
	//====================================================
	this.NextPage = function NextPage()
	{
		try
		{
			if (this.PageIndex < this.PageCount - 1)
			{
				this.PageIndex++;
				this.Refresh();
				return true;
			}

			return false;
		}
		catch (exc)
		{
			HtmlPopup.Error(exc);
		}
	}

	//====================================================
	// Move to the previous page
	//====================================================
	this.PreviousPage = function PreviousPage()
	{
		try
		{
			if (this.PageIndex > 0)
			{
				this.PageIndex--;
				this.Refresh();
				return true;
			}

			return false;
		}
		catch (exc)
		{
			HtmlPopup.Error(exc);
		}
	}

	//====================================================
	// Can this item be displayed
	//====================================================
	function CanDisplayItem(intIndex)
	{
		try
		{
			var intStartOffset = THIS.PageIndex * THIS.PageSize;
			var intEndOffset = intStartOffset + THIS.PageSize;
		
			if (intIndex >= intStartOffset && intIndex < intEndOffset)
				return true;
				
			return false;
		}
		catch (exc)
		{
			HtmlPopup.Error(exc);
		}
	}

	//====================================================
	// Set the number of pages based on the number of items
	//====================================================
	function SetPageCount(intCount)
	{
		try
		{
			var intPages = Math.round((intCount / THIS.PageSize) - 0.5);

			if (intCount < THIS.PageSize)
				intPages = 0;
			
			var intRemainder = intCount % THIS.PageSize;
			
			if (intRemainder != 0)
				intPages++;

			intPageCount = intPages;
			THIS.PageCount = intPages;
		}
		catch (exc)
		{
			HtmlPopup.Error(exc);
		}
	}
}

function ListViewPage(objListViewPagination, intPageIndex)
{
	var THIS = this;
	this.Pagination = objListViewPagination;
	this.PageIndex = intPageIndex;

	var spnSplitter = null;
	if (intPageIndex > 0 || intPageIndex == -1 || (intPageIndex == 0 && this.Pagination.PageIndex > 0))
	{
		spnSplitter = document.createElement("SPAN");
		spnSplitter.innerHTML = "|";
		spnSplitter.className = "pageSplitter";
		this.Pagination.Container.insertBefore(spnSplitter, this.Pagination.PaginationPostfix);
	}

	var aPage = document.createElement("A");

	if (intPageIndex == -2)
		aPage.innerHTML = "&lt;&lt;";
	else if (intPageIndex == -1)
		aPage.innerHTML = "&gt;&gt;";
	else
		aPage.innerHTML = intPageIndex + 1;
	
	aPage.onclick = aPage_Click;
	aPage.className = "pageLink";

	if (intPageIndex == this.Pagination.PageIndex)
		aPage.className += " activePage";

	this.Pagination.Container.insertBefore(aPage, this.Pagination.PaginationPostfix);

	//====================================================
	// The page link has been clicked
	//====================================================
	function aPage_Click(evt)
	{
		try
		{
			if (THIS.PageIndex == -2)
				THIS.Pagination.PreviousPage();
			else if (THIS.PageIndex == -1)
				THIS.Pagination.NextPage();
			else
			{
				THIS.Pagination.PageIndex = THIS.PageIndex;
				THIS.Pagination.Refresh();
			}
		}
		catch (exc)
		{
			HtmlPopup.Error(exc);
		}
	}

	//====================================================
	// Remove this page link
	//====================================================
	this.Remove = function Remove()
	{
		try
		{
			if (spnSplitter != null)
				this.Pagination.Container.removeChild(spnSplitter);

			if (aPage != null)
				this.Pagination.Container.removeChild(aPage);
		}
		catch (exc)
		{
			HtmlPopup.Error(exc);
		}
	}
}
