﻿// ----------------------------------------------
// File:		PageNavigator.js
// Author:		Nathan Derksen
// Description:	Visual component for navigating between pages of results
// Example:
// var pageNav = new PageNavigator(document.getElementById("pageNavHolder"));
// ----------------------------------------------


// ----------------------------------------------
// Function:	PageNavigator()
// Author:		Nathan Derksen
// Description:	Base class
// Inputs:		<DOMElement> containerElement: The DOM element to act as a holder for
//					the HTML generated by this component.
// Returns:		<Nothing>
// ----------------------------------------------
function PageNavigator(containerElement)
{
	this.pContainerElement = containerElement;
	this.pNumPages = 0;
	this.pCurrentPage = 0;
	this.maxNumPages = 5;
}


// ----------------------------------------------
// Function:	PageGrid.drawComponent()
// Author:		Nathan Derksen
// Description:	Redraw the HTML that lays out the page navigation component
// Inputs:		<None>
// Returns:		<Nothing>
// ----------------------------------------------
PageNavigator.prototype.drawComponent = function()
{
	var componentHTML = "";
	var startIndex = 0;
	var endIndex = 5;
	
	startIndex = Math.floor(this.pCurrentPage/5) * 5;
	endIndex = startIndex + 5;
	if (endIndex >= this.pNumPages)
	{
		endIndex = this.pNumPages;
	}	
	
	if (this.pNumPages > 0)
	{
		if (this.pCurrentPage > 0)
		{
			componentHTML += '<a href="javascript:handlePageLeft();" onmouseover="MM_swapImage(\'arrowTrimmedLeft\',\'\',\'../Shared/images/misc/arrowTrimmed_left_on.gif\',0)" onmouseout="MM_swapImgRestore()" onfocus="this.blur()" class="paginationArrow"><img src="../Shared/images/misc/arrowTrimmed_left.gif" alt="Previous Page" title="" name="arrowTrimmedLeft" border="0" id="arrowTrimmedLeft" /></a>';
		}
		else
		{
			componentHTML += '';
		}
		
		for (var i = startIndex; i < endIndex; i++)
		{
			if (i != this.pCurrentPage)
			{
				componentHTML += '<a href="javascript:gotoPage(' + i + ');" onfocus=""this.blur()">' + (i+1) + '</a>';
			}
			else
			{
				componentHTML += '<a href="javascript:gotoPage(' + i + ');" class="pageHere" onfocus=""this.blur()">' + (i+1) + '</a>';
			}
		}

		if (endIndex < this.pNumPages)
		{
			componentHTML += '<img src="/Shared/images/misc/paginationEllipsis.gif" alt="" title="" id="paginationEllipsis" border="0" />';
		}
		
		if (this.pCurrentPage < this.pNumPages - 1)
		{
			componentHTML += '<a href="javascript:handlePageRight();" onmouseover="MM_swapImage(\'arrowTrimmedRight\',\'\',\'/Shared/images/misc/arrowTrimmed_right_on.gif\',0)" onmouseout="MM_swapImgRestore()" onfocus="this.blur()" class="paginationArrow"><img src="/Shared/images/misc/arrowTrimmed_right.gif" alt="Next Page" title="" name="arrowTrimmedRight" border="0" id="arrowTrimmedRight" /></a>';
		}
		else
		{
			componentHTML += '';
		}
	}
	this.pContainerElement.innerHTML = componentHTML;
};

// ----------------------------------------------
// Function:	PageGrid.setNumPages()
// Author:		Nathan Derksen
// Description:	Set how many pages should be visible in the component
// Inputs:		<None>
// Returns:		<Nothing>
// ----------------------------------------------
PageNavigator.prototype.setNumPages = function(numPages)
{
	this.pNumPages = numPages;
	this.drawComponent();
};

// ----------------------------------------------
// Function:	PageGrid.setCurrentPage()
// Author:		Nathan Derksen
// Description:	Sets the page currently being viewed
// Inputs:		<None>
// Returns:		<Nothing>
// ----------------------------------------------
PageNavigator.prototype.setCurrentPage = function(currentPage)
{
	this.pCurrentPage = currentPage;
	this.drawComponent();
};


// ----------------------------------------------
// Function:	PageGrid.isLeftArrowVisible()
// Author:		Nathan Derksen
// Description:	Returns if the left navigation arrow is currently being displayed
// Inputs:		<None>
// Returns:		<Boolean>
// ----------------------------------------------
PageNavigator.prototype.isLeftArrowVisible = function()
{
	return (this.pCurrentPage > 0);
};

// ----------------------------------------------
// Function:	PageGrid.isRightArrowVisible()
// Author:		Nathan Derksen
// Description:	Returns if the right navigation arrow is currently being displayed
// Inputs:		<None>
// Returns:		<Boolean>
// ----------------------------------------------
PageNavigator.prototype.isRightArrowVisible = function()
{
	return (this.pCurrentPage < this.pNumPages - 1);
};

