var rotationDelay = 3; // Seconds
var rotationSpeed = 500; // Milliseconds
var rotationEasing = 'swing'; // 'linear' or 'swing'
var currentImage = 0;
var imageIds = new Array();
var imageWidths = new Array();
var imageHeights = new Array();
var zindex = 999;
var lastImage = false;

$(document).ready(function() {
	initImageRotation();
});

function initImageRotation() {
	// Initialize image rotation
	if($('#imageRotation').length > 0) {
		// Collect image IDs and reorganize z-indexes
		i = 0;
		$.each($('#imageRotation').children(),function() {
			// Store image ID
			imageIds[i] = $(this).attr('id');
			
			// Store image dimensions
			imageWidths[i] = $(this).find('img').width();
			imageHeights[i] = $(this).find('img').height();
			
			// Move image
			$(this).css('zIndex',zindex-i);
			i++;
		});
	
		rotationInterval = setInterval('rotateImage()',rotationDelay*1000);
	}
}

function rotateImage() {
	// Move last image into the bottom of the image stack
	if(lastImage !== false) {
		$('#'+imageIds[lastImage]).css('zIndex',zindex - imageIds.length);
		$('#'+imageIds[lastImage]).animate({ marginLeft: 0 },0,0);
	}
	
	// Animate current element
	$('#'+imageIds[currentImage]).animate({marginLeft:(imageWidths[currentImage])+'px'},rotationSpeed,rotationEasing);
	
	zindex--;
	lastImage = currentImage;
	currentImage = (currentImage == imageIds.length - 1) ? 0 : currentImage + 1;
}
