added spin js code to load before gallery is created and re-organised the code a bit