var move = null; // object
var aktivna=0;
var visina=0;
var sirina=0;
var top=1;
var left=1;
var div_visina=0;
var div_sirina=0;
var tr_left=0;
var tr_top=0;
var opt_left=1;
var opt_top=1;

function doMove()
{
	if(opt_left==1)
	{
		tr_left-=left;
		move.style.right='auto';
		move.style.left = Math.floor(tr_left)+'px';
		if(parseInt(move.style.left)<-(sirina-div_sirina))
		{
			init();
			return 0;
		}
	}
	else if(opt_left==-1)
	{
		tr_left-=left;
		move.style.left='auto';
		move.style.right = Math.floor(tr_left)+'px';
		if(parseInt(move.style.right)<-(sirina-div_sirina))
		{
			init();
			return 0;
		}
	}
	else if(opt_left==0)
	{
		move.style.right='auto';
		move.style.left=tr_left+'px';
	}
	if(opt_top==1)
	{
		tr_top-=top;
		move.style.bottom='auto';
		move.style.top = Math.floor(tr_top)+'px';
		if(parseInt(move.style.top)<-(visina-div_visina))
		{
			init();
			return 0;
		}
		else setTimeout(doMove,50);
	}
	else if(opt_top==-1)
	{
		tr_top-=top;
		move.style.top='auto';
		move.style.bottom = Math.floor(tr_top)+'px';
		if(parseInt(move.style.bottom)<-(visina-div_visina))
		{
			init();
			return 0;
		}
		else setTimeout(doMove,50);
	}
	else if(opt_top==0)
	{
		move.style.bottom='auto';
		move.style.top=tr_top+'px';
		setTimeout(doMove,50);
	}
}

function fadeout(op,id)
{
	document.getElementById('frontpic_'+id).style.opacity=op/100;
	document.getElementById('frontpic_'+id).style.filter="alpha(opacity="+op+")";
	//move.style.-moz-opacity=op/100;
	if(op==0)return 0;
	setTimeout("fadeout("+(op-10)+","+id+");",90);
}

function fadein(op,id)
{
	document.getElementById('frontpic_'+id).style.opacity=op/100;
	document.getElementById('frontpic_'+id).style.filter="alpha(opacity="+op+")";
	//move.style.-moz-opacity=op/100;
	if(op==100)return 0;
	setTimeout("fadein("+(op+10)+","+id+");",90);
}

function init() {
	
	div_visina=parseInt(document.getElementById('move_holder').height);
	div_sirina=parseInt(document.getElementById('move_holder').width);
	if(aktivna>0)
	{
		move = document.getElementById('frontpic_'+aktivna);
		fadeout(100,aktivna);
		//move.style.display = 'none';
	}
	aktivna++;
	if (aktivna>stevilo)aktivna=1;
	move = document.getElementById('frontpic_'+aktivna);
	fadein(0,aktivna);
	sirina=move.width;
	visina=move.height;
	move.style.top='0px';
	move.style.left='0px';
	left=sirina-div_sirina;
	top=visina-div_visina;
	tr_left=0;
	tr_top=0;
	if(visina<=div_visina)
	{
		left=1;
		top=0;
	}
	else if (sirina<=div_sirina)
	{
		left=0;
		top=1;
	}
	else if(left<top)
	{
		left/=top;
		top=1;
	}
	else
	{
		top/=left;
		left=1;
	}
	left=1;
	top=1;
	move.style.display = 'block';
	do
	{
		opt_left=Math.ceil(Math.random()*3)-2;
		opt_top=Math.ceil(Math.random()*3)-2;
	}
	while(opt_left==0 && opt_top==0)
	if(opt_left==0)tr_left=-Math.round(Math.random()*(sirina-div_sirina-2));
	if(opt_top==0)tr_top=-Math.round(Math.random()*(visina-div_visina-2));
	doMove();
}


//window.onload = init;