/*
* File: pagebyarray.js
* Include with pageindex_foldername.js to supply 'aPages' array.
*/

function getUrl(href) {
// Arguments:
// href: absolute url containing filename found in 'aPages' array.

	href = href.split('?')[0];												// Get url before ?
	var aPat = href.split('/');												// Split into bits sepated by /'s into an array
	
	var address = '';
	
	for (var i = 0; i < (aPat.length - 2); i++) {
				address = address + aPat[i] + '/';				// Get url up to last /
  		};
	
	var actUrl = {
								path: address,
								dir: aPat[aPat.length-2],						// Get directory containing file
								page: aPat[aPat.length-1]						// Get file
								};
		
	return actUrl;																		// Return object with properties representing separate parts of url.
}

function getPage(increment) {
// Arguments:
// increment: +/- integer specifying position of item returned from array
// relative to item matching url.

	var Url = getUrl(document.URL);						// Get object representing active url.
	
	var aPages = getPageIndex();							// Use array from pageindex_foldername.js.
	var dir = Url.dir;											// Search string for array.
	
	var actPos = -1;
	for (var i = 0; i < aPages.length; i++) {
		if (aPages[i] == dir) {
	    	actPos = i;
	    	break;
  		}
	}
	
	if (actPos == -1) {
		return 'javascript:;';									// If active page not found return null link.
	}
	
	var newPos = actPos + increment;					// Get position of item to return.
	var ubound = (aPages.length - 1);					// Get upper bound of array.
	
	if (newPos > ubound) {										// At end of array.
		dir = aPages[0];												// Loop to first page.
	}
	else if (newPos < 0) {										// At start of array.
		dir = aPages[ubound];									// Loop to last page.
	}
	else {
		dir = aPages[newPos];									// Use increment value.
	}

	return Url.path + dir + '/' + Url.page;
}

// Call either function below from onClick event to load next/previous page
// based on current page position in 'aPages' array.

function nextPage() {
	window.location.href = getPage(1);
}

function prevPage() {
	window.location.href = getPage(-1);
}

// Call function below from onClick event to go back to page
// specified in pageindex_foldername.js.

function goBack(){

	var Url = getUrl(document.URL);					// Get object representing active url.
	var page = backPage;										// Use global variable from pageindex_foldername.js.
	
	window.location.href = Url.path + page;
}