pageNavigation() still uses a global variable $document which is replaced by a parameter passed to the function. All calls of pageNavigation() needs a third parameter with the document.