function count_covers() {
	var cover_count = 0;			
	var div = document.getElementById("covers");
	for (index = 0; index < div.childNodes.length; index++) {
		if (div.childNodes.item(index).className == "glider") {
			cover_count += 1;
		}
	}
	return cover_count;
}

function get_center_cover() {
	var start_cover_index = 0;
	var cover_count = count_covers();
	switch(cover_count) {
		case 1:
			return 0;
		case 2:
			return 0;
		case 3:
			return 1;
		default:
			return Math.round((cover_count / 2)) - 1;
	}
}

function step()
{
	if (target < current-1 || target > current+1)
	{
		moveTo(current + (target-current)/5);
		window.setTimeout(step, 50);
		timer = 1;
	}
	else
	{
		timer = 0;
	}
}

function glideTo(x, newCaptionId)
{
	target = x;
	if (timer == 0)
	{
		window.setTimeout(step, 50);
		timer = 1;
	}

	captionId = newCaptionId;
	var captionTarget = document.getElementById("captionTarget");
	var caption = document.getElementById(captionId);
	captionTarget.innerHTML = caption.innerHTML;
}

function moveTo(x)
{
	current = x;
	var div = document.getElementById("covers");
	var top = div.offsetTop;
	var width = 750;
	var size = width * 0.5;
	var biggest = width * 0.7;
	var zIndex = div.childNodes.length;
	var baseLeft = 68;
	for (index = 0; index < div.childNodes.length; index++)
	{
		var image = div.childNodes.item(index);
		if (image.className == "glider")
		{
			var z = Math.sqrt(10000 + x * x) + 200;
			var xs = x / z * size + size;
			image.style.left = baseLeft + (xs - 38 / z * biggest);
			image.style.top = (-75 / z * size + top) + 130;
			image.style.width = 100 / z * biggest;
			//image.style.height = 110.25 / z * biggest;
			image.style.height = 100 / z * biggest;
			image.style.zIndex = zIndex;
			if ( x < 0 )
				zIndex++;
			else
				zIndex--;
			x += 150;
		}
	}
}



function refresh()
{
	var width = windowWidth;
	var height = width * 0.3;
	var images = document.getElementById("covers");
	images.style.height = height;
	
	var captionTarget = document.getElementById("captionTarget");
	captionTarget.style.top = images.offsetTop + height;
	//captionTarget.style.height = height * 0.4;
	captionTarget.style.zIndex = 100;

	var caption = document.getElementById(captionId);
	captionTarget.innerHTML = caption.innerHTML;

	moveTo(current);
}