Here's an example of using the onEnd event to close the viewer after the last image has been display: