window.onDomReady(init);

// Hard-coded for images in multiples of 3

var imageNames = [
	'apples',
	'dahlia',
	'geranium',
	'golf',
	'greenhouse',
	'lawnshade',
	'spruce',
	'sweetcorn',
	'tulips',
	'waterlily',
	'bleedingheart',
	'sweetcorn',
	'snowtree',
	'flowers1',
	'flowers1'
];
var skip = imageNames.length / 3;

var imgCounter = 1;
var imgPosition = 0;
var images;

function init()
{
	var imageFiles = imageNames.map(imageArrayMap);

	images = new Asset.images(imageFiles, {
		onComplete: rotateImages
	});
}

function imageArrayMap(imageName) 
{
	return 'images/rotating-images/' + imageName + '.png';	
}

function rotateImages()
{
	var containerDiv = $('rotatingImages');
	var imageElements = containerDiv.getChildren();

	// Replace the image with the next image for the frame
	imageElements[imgPosition].replaceWith(images[(imgPosition * skip) + imgCounter]);

	// After rotation all three images increment imgCounter mod skip
	if (imgPosition == 2)
		imgCounter = (imgCounter + 1) % skip;

	// Increment imgPosition mod three
	imgPosition = (imgPosition + 1) % 3;

	// Do this again in 3 seconds
	setTimeout('rotateImages()', 3000);
}
