// This Document contains all the Pagination Javascript Code used on my website robhowland.net
// Feel free to take this code and do whatever with it, I really don't care if you credit me
// I had a lot of fun tweaking this to perfection so just try to have fun with it too!

// Enter Total Page Value Here
var totalPages = 10;
// Since we will start on the last page
var curPage = totalPages;
// Declare temp page storage variable
var newPage;
// Storage for current news page # from URL
var justIn;

// This function is called only when the previous or next button is hit
// It will be passed a value of either 0 or 1
// The value depends on whether the previous (0) or next (1) button was pressed
function prevNext(preNex)
{
	// If we hit prev button and are not on page 1
	if(preNex == 0 && curPage != 1)
	{
		// Our new page will be one less than the current page
		newPage = curPage - 1;
	}
	// If we hit next button and are not on the last page
	else if (preNex == 1 && curPage != totalPages)
	{
		// Our new page will be one more than the current page
		newPage = curPage + 1;
	}
	else // If we happen to be on the first or last page
	{
		// Stop Here
		return;
	}
	// Go and change the page if we made it this far
	changePage(newPage);
}

// This function actually changes the page and is called in two ways
// The first way is the above function calls it when one of the prev or next buttons are hit
// The second way is by hitting one of the page numbers directly, which skips the above function
function changePage(pageid)
{
	// Check value of curPage against the variable passed to the above function
	if (curPage != pageid)
	{
		// Make loading preparations if the numbers are different
		showLoad();
		// Load the new page based on pageid value
		top.frames['newsframe'].location.href="news"+(pageid).toString()+".html";
		// Update the current page value to the value passed to the above function
		curPage = pageid;
	}
	else // If we clicked on the current page number
	{
		// Stop Here
		return;
	}
	// Update the Active Page Highlighter to the current page if we made it this far
	changeClass(curPage);
}


// This function Highlights the Current Page by changing the class for each page element
// If we made it to the end of the above function, this function gets called
// We MUST use "top." befor each document declaration in order to allow the iframe to make changes to the parent window
function changeClass(litPage)
{
	// The css highlights pages with class = 'active'
	// Run loop and remove the active class for all pages
	for(var i=1;i<=totalPages;i++)
	{
		// Check for page elements with class name 'active'
		if (top.document.getElementById(i).className == 'active')
		{
			// Set class name to a blank string
			top.document.getElementById(i).className = "";
		}
	}
	
	// Then we assign the active class to only the current page
	top.document.getElementById(litPage).className = "active";
	
	// Finally, we need to update the destination values of the prev and next buttons
	moveIt(litPage);
}

// This function updates the destination values of the previous and next buttons
function moveIt(whereTo)
{
	// If new current page value is not the first or last page
	if (whereTo != 1 && whereTo != totalPages)
	{
		// Set the previous button's new destination value
		top.document.getElementById("prev").onclick="changeClass("+(curPage-1).toString()+")";
		// Set the next button's new destination value
		top.document.getElementById("next").onclick="changeClass("+(curPage+1).toString()+")";
	}
}

// The following functions are opposites and are used to show/hide the loader and the iframe

// This function is called when we are loading a new page
function showLoad()
{
	document.all.newsframe.style.visibility="hidden"; // Hide the iFrame while loading (disables white flash)
	document.all.loader.style.visibility="visible"; // Show first loader gif while we wait
	document.all.loader2.style.visibility="visible"; // Show second loader gif while we wait
}

// This function is called once the content is loaded
function showOff()
{
	document.all.loader.style.visibility="hidden"; // Hide the first loader gif
	document.all.loader2.style.visibility="hidden"; // Hide the second loader gif
	document.all.newsframe.style.visibility="visible"; // Show the iFrame when finished
}

// This last bit verifies the curent page value from the iframe itself in case user hits back within iframe
// It takes advantage of the fact that we used "top." before each document reference to make changes to the parent window
function backCheck()
{
	// This finds the number at the end of the url and makes it an Int
	// NOTE: If you change "news" to something else, you need to count how many characters in your number (from 0!)
	
	// Check for Japanese and add 3 characters to substring check if so
	if (location.pathname.substr(1,2) == "jp")
	{
		// We need 1 digit when we are under 10 pages so: (6th character (from 0), length 1)
		if (top.curPage < 10)
		{
			justIn = parseInt((location.pathname).substr(8,1));
		}
		// We need 2 digits once we hit 10 pages so: (6th character (from 0), length 2)
		else
		{
			justIn = parseInt((location.pathname).substr(8,2));
		}
	}
	else
	{
		// We need 1 digit when we are under 10 pages so: (6th character (from 0), length 1)
		if (top.curPage < 10)
		{
			justIn = parseInt((location.pathname).substr(5,1));
		}
		// We need 2 digits once we hit 10 pages so: (6th character (from 0), length 2)
		else
		{
			justIn = parseInt((location.pathname).substr(5,2));
		}
	}
	

	// Check to see if our parent's current page value does not equal the news page value (ie: user hit back within iframe)
	if (top.curPage != justIn)
	{
		// We must update the current page value
		top.curPage = justIn;
		
		// We must change the class to highlight the new current page and in turn, update the prev/next links
		changeClass(top.curPage);
	}
}


// That's it! Be sure to check the css and html code to understand how to implement this. Hope you learned something :)
