It's going to return one position more than desired. I suggest to use:
for (var i = pageOffset, page; page = pages[i]; i++)