function loadPage(page)
{
	var totalElements = this.elements.length;
	var beginningItemCount = (this.pageSize * this.currentPage) + 1;
	var endItemCount = ((beginningItemCount - 1 + this.pageSize) < totalElements) ? beginningItemCount - 1 + this.pageSize : totalElements;
	var display = this.elements.slice(beginningItemCount - 1, endItemCount);
	var output = "";

	output = this.formatPage(display);
	document.getElementById(this.targetDisplay).innerHTML = output;
	this.updateNav();
}

function formatPage(display)
{
	/*
		This is the default setting for drawing the page.
		This function should be overwritten by the user
		if they want a custom format.
	*/
	var output = "";
	var i;
	
	for(i = 0; i < display.length; i++)
	{
		output += display[i].draw();
	}
	
	return output
}

function nextPage()
{
	var maxPage = this.getMaxPage();
	if(this.currentPage != maxPage)
	{
		this.currentPage++;
		this.loadPage(this.currentPage);
	}
}

function prevPage()
{
	if(this.currentPage != 0)
	{
		this.currentPage--;
		this.loadPage(this.currentPage);
	}
}

function firstPage()
{
	this.currentPage = 0;
	this.loadPage(this.currentPage);
}

function lastPage()
{
	this.currentPage = this.getMaxPage();
	this.loadPage(this.currentPage);
}

function newElements(elements)
{
	this.elements = elements
	this.currentPage = 0;
	this.loadPage(this.currentPage);
}

function addElements(elements)
{
	this.elements.concat(elements);
	this.loadPage(this.currentPage);
}

function getMaxPage()
{
	if(this.pageSize != 0 || this.pageSize != "")
	{
		return Math.ceil(this.elements.length / this.pageSize) - 1;
	}

	return 0;
}

function updateNav()
{
	var totalElements = this.elements.length;
	var beginningItemCount = (this.pageSize * this.currentPage) + 1;
	var endItemCount = ((beginningItemCount - 1 + this.pageSize) < totalElements) ? beginningItemCount - 1 + this.pageSize : totalElements;

	document.getElementById(this.targetNav).innerHTML = beginningItemCount + " - " + endItemCount + " of " + totalElements + " <a href = 'javascript:void(0);' onClick = 'paging.firstPage();'>First</a> | <a href = 'javascript:void(0);' onClick = 'paging.prevPage();'>< Previous</a> | <a href = 'javascript:void(0);' onClick = 'paging.nextPage();'>Next ></a> | <a href = 'javascript:void(0);' onClick = 'paging.lastPage();'>Last</a>";
}

function ItemPagination(pageSize, targetNav, targetDisplay, elements, formatProc)
{
	this.currentPage = 0;
	this.pageSize = pageSize;
	this.elements = elements;
	this.targetDisplay = targetDisplay;
	this.targetNav = targetNav;
	this.loadPage = loadPage;
	this.nextPage = nextPage;
	this.prevPage = prevPage;
	this.firstPage = firstPage;
	this.lastPage = lastPage;
	this.newElements = newElements;
	this.addElements = addElements;
	this.getMaxPage = getMaxPage;
	this.updateNav = updateNav;
	this.formatPage = formatProc;
	
	this.loadPage(this.currentPage);
}