var N = 5, S = 20, I = 2;

var stopSizes = [	[ 190, 60, 80, 0 ],
					[ 410, 60, 80, 1 ],
					[ 580, 100, 140, 2 ],
					[ 300, 150, 200, 1 ],
					[ 20, 100, 140, 0 ]	];

var objs = null;

function initMotion () {
	objs = [	document.getElementById('i1'),
				document.getElementById('i2'),
				document.getElementById('i3'),
				document.getElementById('i4'),
				document.getElementById('i5')	];
	for (var i = 0; i < N; i++) {
		var obj = objs[(i + I) % N];
		var stopSize = stopSizes[i];
		obj.style.left = stopSize[0] + 'px';
		obj.style.top = stopSize[1] + 'px';
		obj.style.height = stopSize[2] + 'px';
		obj.style.zIndex = stopSize[3];
		obj.className = '';
	}
	move(0);
}

function setupMove ( obj, i ) {
	i += I;
	var thisSize = stopSizes[i % N];
	var nextSize = stopSizes[(i + 1) % N];

	var ix = thisSize[0], iy = thisSize[1], is = thisSize[2];
	var fx = nextSize[0], fy = nextSize[1], fs = nextSize[2];
	var dx = (ix - fx) / S, dy = (iy - fy) / S, ds = (is - fs) / S;

	obj.step = function ( step ) {
		obj.style.left = fx + step * dx + 'px';
		obj.style.top = fy + step * dy + 'px';
		obj.style.height = fs + step * ds + 'px';
		obj.style.zIndex = thisSize[3];
	};
}

function move ( step ) {
	if (step > 0) {
		var s = step - 1;
		setTimeout(function () {
			move(s);
			for (var i = 0; i < N; i++) objs[i].step(s);
		}, 1);
	} else {
		I++;
		setTimeout(function () {
			move(S);
			for (var i = 0; i < N; i++) setupMove(objs[i], i);
		}, 3000);
	}
}
