/**

	Javascript for controlling 'paginated' divs inside pages
	
	
	To make a set of divs paginatable, do the following:
	
		o Include this file and css/pagination.css
		o Create a containing div with class 'paginatable'
		o Give each div inside this a class of 'paginatablePage'
		o Add (inside the container) one or more unordered lists with class
		  'paginationNav', for controlling the navigation between divs
		o Ensure that all of the elements added above are also assigned a unique id

**/
	
//	Arrays to contain the elements to be paginated,
//	the current page for each of these, and the
//	navigation elements present on each of these

paginatedElements = new Array();
currentPage = new Array();
paginationNavElements = new Array();


/*
	Ensure the pagination is initialised when the page loads
*/

window.onload=function()
{
	initialise();
}


/*
	Initialise the pagination
*/

function initialise()
{
	var count;
	var pageCount;
	var pageElementCount;
	
	//	get all paginatable container elements
	paginatableElements = getElementsByClass('paginatable', 'div');
	
	//	initialise the pagination for each of these container elements
	for (count=0; count<paginatableElements.length; count++)
	{
		container = paginatableElements[count].id
		paginatedElements[container] = new Array();
		currentPage[container] = 0;
	
		// populate paginatedElements for the container
		paginatedElements[container] = getElementsByClass('paginatablePage', 'div', container);
		pageCount = paginatedElements[container].length;

		// display the navigation for this container
		initialiseNavigation(container);
		
		// display the contained elements as pages
		displayPaginated(container);
	}
}


/*
	get all elements of a given CSS class, optionally
	limiting the results by tag type and parent node
*/

function getElementsByClass(searchClass, tag, node)
{
	//	get parent node
	if ( node == null )
	{
		node = document;
	}
	else
	{
		node = document.getElementById(node);
	}
	
	//	select all elements if no tag specified
	if ( tag == null ) tag = '*';
	
	//	get all elements in parent node of type tag	
	var elements = node.getElementsByTagName(tag);
	
	//	initialise variables used for filtering elements by class
	var elementsLen = elements.length;
	var results = new Array();
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	
	//	filter elements by class into results
	for (elementsCount=0, resultsCount=0; elementsCount<elementsLen; elementsCount++) 
	{
		if (pattern.test(elements[elementsCount].className)) 
		{
			results[resultsCount] = elements[elementsCount];
			resultsCount++;
		}
	}
	return results;
}


/*
	show the navigation elements for a specified container
*/

function initialiseNavigation(container)
{
	// populate paginationNavElements for the container
	paginationNavElements[container] = getElementsByClass('paginationNav', 'ul', container);
	var navElementCount = paginationNavElements[container].length;
	
	// show all the navigation elements
	for (elementsCount=0; elementsCount<navElementCount; elementsCount++)
	{
		paginationNavElements[container][elementsCount].style.display = 'block';
	}
}


/*
	show requested page in specified container
*/

function showPage(container, pageNumber)
{
	//	hide currently displayed page
	paginatedElements[container][currentPage[container]].style.display = 'none';
	
	//	set specified page to be current and display it
	currentPage[container] = pageNumber;
	paginatedElements[container][currentPage[container]].style.display = 'block';
	
	//	update the navigation links
	writeNavigationLinks(container);
}


/*
	show all pages in specified container
*/

function displayAll(container)
{
	var pageCount = paginatedElements[container].length;
	
	for (elementsCount=0; elementsCount<pageCount; elementsCount++)
	{
		paginatedElements[container][elementsCount].style.display = 'block';
	}
	
//	update the navigation links
	writeNavigationLinks(container, 1);
}


/*
	show only the current page in specified container
*/

function displayPaginated(container)
{
	var pageCount = paginatedElements[container].length;
	
	for (elementsCount=0; elementsCount<pageCount; elementsCount++)
	{
		if(elementsCount != currentPage[container])
		{
			paginatedElements[container][elementsCount].style.display = 'none';
		}
	}
	
//	update the navigation links
	writeNavigationLinks(container);
}


/*
	write a list of links to the different pages inside a paginated container
*/

function writeNavigationLinks(container, showAll)
{
	var navHTML = '';
	var pageCount = paginatedElements[container].length;

//	output navigation if user hasn't opted to show all pages
	if(showAll == null)
	{
		if (pageCount > 1)
		{
			//	add the 'Previous' link
			if (currentPage[container] > 0)
			{
				navHTML += '<li class="previous"><a href="#' + container + '" onClick="showPage(\''+ container + '\', ' + (currentPage[container]-1) +'); return false;">Previous</a></li>';
			}
			else
			{
				navHTML += '<li class="previous">Previous</li>';
			}	
			
			//	add numbered links for each page
			for (elementsCount=0; elementsCount<pageCount; elementsCount++)
			{
				if (elementsCount != currentPage[container])
				{
					navHTML += '<li><a href="#' + container + '" onClick="showPage(\'' + container + '\', ' + elementsCount + '); return false;">'+ (elementsCount+1) +'</a></li>';
				}
				else
				{
					navHTML += '<li>' + (elementsCount+1) + '</li>';
				}
			}
			
			//	add the 'Next' link
			if (currentPage[container] < (pageCount-1))
			{
				navHTML += '<li class="next"><a href="#' + container + '" onClick="showPage(\''+ container + '\', ' + (currentPage[container]+1) +'); return false;">Next</a></li>';
			}
			else
			{
				navHTML += '<li class="next">Next</li>';
			}
			
			//	add the 'Show All' link
			navHTML += '<li class="next"><a href="#' + container + '" onClick="displayAll(\'' + container + '\'); return false">Show all</a>';
		}
	}
//	add the 'Show pagination' link
	else navHTML += '<li><a href="#' + container + '" onClick="displayPaginated(\'' + container + '\'); return false">Show paginated</a>';
	
	
	//	write the set of links into all navigation elements in the container
	for (count=0; count<paginationNavElements[container].length; count++)
	{
		document.getElementById(paginationNavElements[container][count].id).innerHTML = navHTML;
	}
}
