﻿/*<html>
<head>
	<style type="text/css">
		.spanPagerControlUnselected, .spanPagerControlSelected
		{
			font-family: arial;
			font-size: 9pt;
			font-weight: bold;
			border: 1px solid black;
			margin-right: 2px;
			margin-left: 2px;
			padding-left: 8px;
			padding-right: 8px;
			padding-top: 4px;
			padding-bottom: 4px;
			text-decoration:none;
			color:black;
		}

		.spanPagerControlSelected:hover
		{
			background-color:gray;
		}

		.spanPagerControlSelected, .spanPagerControlUnselected:hover
		{
			background-color:#1e4490;
			color:white;
		}

		.hellip
		{
			font-weight: normal;
			font-size: 8pt;
		}
	</style>
</head>
<body>
	<div id="divPager"></div>
</body>
</html>

<script type="text/javascript">
*/
function PagerControl(parameters) {

	var PagerControl = new Object();

	// Public 

	PagerControl.page = function(pageNumber) {
		parameters.pagedCallback(pageNumber);
		this.currentPage = pageNumber;
		this.draw();
	};

	// Private

	PagerControl.currentPage = parameters.startPage;
	PagerControl.pageCount = parameters.pageCount;

	PagerControl.hasPreviousPage = function() {
		return (this.currentPage > 1);
	};

	PagerControl.hasNextPage = function() {
		return (this.currentPage < this.pageCount);
	};

	PagerControl.draw = function() {

		var createElement = function(page, selected, text) {	

			var elementNode = document.createElement(selected ? "span" : "a");

			if (!selected)
				elementNode.setAttribute("href", "javascript:" + parameters.varName + ".page(" + page + ")");
			else
				elementNode.setAttribute("id", parameters.selectedPageButtonId);

			elementNode.setAttribute("class", selected ? parameters.selectedCssClass : parameters.unselectedCssClass);
			elementNode.appendChild(document.createTextNode(text ? text : page));

			targetDiv.appendChild(elementNode);

		}

		var currentPage = this.currentPage;
		var pageCount = this.pageCount;
		var pagesToShow = parameters.pagesToShow;
		var hellipHTML = "<span class=\"" + parameters.hellipCssClass + "\">&hellip;</span>";

		// Reset the pager element; then paint it progressively

		var targetDiv = document.getElementById(parameters.targetDiv);
		targetDiv.innerHTML = "";

		var radius = Math.floor(parameters.pagesToShow/2);
		var start = currentPage - radius;
		var stop = currentPage + radius;

		if (start < 1) {
			start = 1;
			stop = (pagesToShow > pageCount)? pageCount : pagesToShow;
		}

		if (stop > pageCount) {
			stop = pageCount;
			var potentialStart = stop - pagesToShow;
			start = (potentialStart < 1)? 1 : potentialStart;
		}

		if (this.hasPreviousPage())
			createElement(parseInt(currentPage-1), false, parameters.previousPageLabel);

		if (parameters.alwaysShowFirst && start > 1) {
			createElement(1, false);
			if (start > 2)
				targetDiv.innerHTML += hellipHTML;
		}

		for (var i = start; i <= stop; i++)
			createElement(i, (i == currentPage));

		if (parameters.alwaysShowLast && stop < pageCount) {
			if (stop < pageCount - 1)
				targetDiv.innerHTML += hellipHTML;
			createElement(pageCount, false);
		}

		if (this.hasNextPage())
			createElement(parseInt(currentPage+1), false, parameters.nextPageLabel);

		// IE won't display class styles until this:
		targetDiv.innerHTML += "";
	};

	PagerControl.draw();

	return PagerControl;
}
/*
</script>

<script type="text/javascript">

function loadPage(page) {
	// Ssend an XMLHttpRequest off to display the new page's contents in the
	// HTML document.
}

var pager = new PagerControl({

	startPage:1,
	pageCount: 100,
	pagesToShow: 7,
	unselectedCssClass: "spanPagerControlUnselected",
	selectedCssClass: "spanPagerControlSelected",
	hellipCssClass: "hellip",
	previousPageLabel: "previous",
	nextPageLabel: "next",
	alwaysShowFirst: true,
	alwaysShowLast: true,
	varName: "pager",
	targetDiv: "divPager",
	pagedCallback: loadPage

});

</script>

*/

/*

Key for the parameters:

startPage (number) - The initial page that is selected.
pageCount (number) - The number of pages that exist.
pagesToShow (number) - The number of pages to show to the
	left and right of the current page for example, if
	startPage (above) is 10 and pagesToShow is 7, the
	buttons for pages 7 - 13 will be displayed.
unselectedCssClass (string) - The CSS class name for the
	page buttons that aren't the current page.
selectedCssClass (string) - The CSS class name for the
	page button that is curreltly selected.
hellipCssClass (string) - The CSS class name for <span>
	that displays "..." between the first and last page
	buttons if alwaysShowFirst and/or alwaysShowLast
	(below) are set to true.
previousPageLabel (string) - The text that appears in the
	page button for the previous page.
nextPageLabel (string) - The text that appears in the
	page button for the next page.
alwaysShowFirst (boolean) - Specify true value to always
	show a button for page 1 (and an ellipsis is
	displayed between page 1 and the curruent page minus
	half of the pagesToShow (above).
alwaysShowLast (boolean) -  Specify true value to always
	show a button for page 1 (and an ellipsis is displayed
	between the last page and the curruent page minus half
	of the pagesToShow (above).
varName (string) - The variable name of this new
	PageControl object.
targetDiv (string) - Where to draw the pager.
pagedCallback (function) - The function to fire when a
	surfer clicks a page.

*/