AUI-1334 Image viewer thumbnails link to wrong image

When clicking on a thumbnail which was not on the first set of a gallery viewer, the user was being taken back to respective index on the first set. This is because the code wasn't calculating the 'offset' correctly. Also, the code wasn't respecting the limits for page/offset values, sending invalid values to the pagination widget temporarily, and fixing them later.

This fixes the bug by correctly calculating the offset. It's also calculating the new page/offset before setting them instead of fixing them afterwards.