The only one thing I am not certain is this line. Now, setMaxHeightAllPages() runs only once. It should be OK as soon as none of they elements with classes .overlay .page are added.