Heres a simple class (javascript equivalent) I made that you could use.

//This class will manage cycling through a list of images
function ImageCollection(images) {
this.images = images;...