var current = 0;
var targetif = 0;
var timer = 0;
var captionId = "i1";

function windowWidth()
{
	var width;
	if(document.layers) {
		width = window.innerWidth;
	} else {
		width = document.body.clientWidth;
	}
	return width;
}

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

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

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

function moveTo(x)
{
	current = x;
	var div = document.getElementById("images");
	var top = div.offsetTop;
	var width = windowWidth();
	var size = width * 0.5;
	var biggest = width * 0.6;
	var zIndex = div.childNodes.length;
	for (index = 0; index < div.childNodes.length; index++)
	{
		var image = div.childNodes.item(index);
		if (image.nodeType == 1)
		{
			var z = Math.sqrt(10000 + x * x) + 100;
			var xs = x / z * size + size;
			image.style.left = xs - 50 / z * biggest;
			image.style.top = 30 / z * size + top;
			image.style.width = 75 / z * biggest;
			image.style.height = 150 / 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("images");
	images.style.height = height;

	var captionTarget = document.getElementById("captionTarget");
	captionTarget.style.top = images.offsetTop + height-100;
	captionTarget.style.height = height * 0.4;
	captionTarget.style.zIndex = 100;

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

	moveTo(current);
}

