dojo.require('dojo.fx');

Slideshow = {
	init: function(url) {
		dojo.xhrGet({
			url: url,
			handleAs: "json",
			load: function(response, ioArgs) {

				Slideshow.loadImages(response);
				return response;
        	}
		});
	},

	loadImages: function(data) {
		
		for	(i = 0; i < data.length; i++) {
			var image = new Image();
			dojo.byId('slide').appendChild(image);
			image.src = data[i].url;
			image.height = data[i].height;
			image.width = data[i].width;
			
		}

		/* Set width of the slideshow */
		var width = 0;
		dojo.query("#slide img").forEach(function(element) {
			width += element.width + 30;
		});

		dojo.byId('slide').style.width = width + 'px';

		function animate() {

			dojo.animateProperty({
				node: "slide",
			    duration: 3000,
			    properties: {
			        left: - dojo.query("#slide img")[0].width - 30
			    },
			    
			    onEnd: function() {
					dojo.byId('slide').appendChild(dojo.query("#slide img")[0]);
					dojo.byId('slide').style.left = 0;
					animate();
			    }
			}).play();
		}

		animate();
	}
}

