$(document).ready(function () {

	// Place all images in position
	if (RANDOMIZE) {
		var o = IMAGES.slice(0);
		for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
		IMAGES = o;
	}
	var $images = $('#studio-images');
	for(var i=0, l=IMAGES.length; i<l; i++) {
		var $d = $('<div></div>').css({opacity:0}).appendTo($images);
		var $img = $('<img src="' + IMAGES[i].absoluteurl + '" />').appendTo($d);
	}

	// As soon as the first image is loaded, start the animation
	var img = new Image();
	img.onload = function() {
		tick();
	};
	img.src = IMAGES[0].absoluteurl;

	var current = null;
	var total = IMAGES.length;
	function tick() {
		if (current !== null) {
			$('#studio-images > div:nth-child(' + (current+1) + ')').css({
				'z-index': 99
			});
		}

		var next = current===null ? 0 : (current + 1) % total;
		$('#studio-images > div:nth-child(' + (next+1) + ')').css({
			'z-index': 100
		}).animate({
			opacity: 1
		}, PERIOD.mix, function () {
			if (current !== null) {
				$('#studio-images > div:nth-child(' + (current+1) + ')').css({
					opacity: 0
				});
			}
			current = next;
			setTimeout(tick, PERIOD.pause);
		});
	}
});
