var rychlost=10;

var tarja_x=0;
var tarja_y=0;

var tuom_x=0;
var tuom_y=0;

var movingTuom;

var startTime=0;

var istart=0;

var left=0;

	function move(e)
	{
		document.getElementById("tarja").style.left=e.clientX + 'px';
		document.getElementById("tarja").style.top=e.clientY + 'px';
		tarja_x=e.clientX;
		tarja_y=e.clientY;

		if(left==0 && tarja_x<tuom_x)
		{
			left=1;
			document.getElementById("tarja").src='tarja_left.gif';
			document.getElementById("tuomas").src='tuomas_left.gif';
		}
		if(left==1 && tarja_x>tuom_x)
		{
			left=0;
			document.getElementById("tarja").src='tarja_right.gif';
			document.getElementById("tuomas").src='tuomas_right.gif';
		}

		if(istart==1)
		{
			startTime=new Date();
			istart=2;
			movingTuom=window.setInterval("moveTuom()", 30);
			document.getElementById("tarja").style.display='block';
			document.getElementById("tuomas").style.display='block';
		}
	}
	function moveTuom()
	{
		var dif_x=tuom_x-tarja_x;
		var dif_y=tuom_y-tarja_y;

		if (dif_x>-80 && dif_x<80 && dif_y>-100 && dif_y<100) {
			clearInterval(movingTuom);
			catched();
			return false;
		}

		var smer=Math.atan(dif_x/dif_y);

		var move_x=Math.abs(Math.sin(smer));
		var move_y=Math.abs(Math.cos(smer));

		if(tarja_x<tuom_x){
			move_x*=-1;

		}
		if(tarja_y<tuom_y){
			move_y*=-1;
		}

		tuom_x=tuom_x+rychlost*move_x;
		tuom_y=tuom_y+rychlost*move_y;
		//alert(tuom_x);

		document.getElementById("tuomas").style.left=tuom_x + 'px';
		document.getElementById("tuomas").style.top=tuom_y + 'px';
	}
	function start(bStart)
	{
		if(bStart)
		{
			var arrayPageSize=getPageSize();
			document.getElementById('overlay').style.width=arrayPageSize[0] + 'px';
			document.getElementById('overlay').style.height=arrayPageSize[1] + 'px';
			istart=1;
		}
		//moveTuom();
	}

	function catched(){
		var endTime=new Date();
		var res=endTime-startTime;
		if (confirm("Vdrželi jste prchat "+(Math.round(res/1000))+" sekund. Chcete tento výsledek zaznamenat do žebříčku?\nYou managed to escape for "+(Math.round(res/1000))+" seconds. Do you want to add the time to ladder?")) {
			document.getElementById('ttime').value=res;
			document.getElementById('result').submit();
		}
		else
		{
			document.getElementById("tarja").style.display='none';
			document.getElementById("tuomas").style.display='none';
			document.getElementById('overlay').style.display='none';
		}
	}
	function winH() {
if (window.innerHeight) return window.innerHeight;
else if (document.documentElement && document.documentElement.clientHeight)
return document.documentElement.clientHeight;
else if (document.body && document.body.clientHeight)
return document.body.clientHeight;
else return null;
}
function getPageSize() {

var xScroll, yScroll;
if (window.innerHeight && window.scrollMaxY) {
xScroll = window.innerWidth + window.scrollMaxX;
 yScroll = window.innerHeight + window.scrollMaxY;
 } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
 xScroll = document.body.scrollWidth;
 yScroll = document.body.scrollHeight;
 } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
 xScroll = document.body.offsetWidth;
 yScroll = document.body.offsetHeight;
 }

 var windowWidth, windowHeight;

 if (self.innerHeight) { // all except Explorer
 if(document.documentElement.clientWidth){
 windowWidth = document.documentElement.clientWidth;
 } else {
 windowWidth = self.innerWidth;
 }
 windowHeight = self.innerHeight;
 } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
 windowWidth = document.documentElement.clientWidth;
 windowHeight = document.documentElement.clientHeight;
 } else if (document.body) { // other Explorers
 windowWidth = document.body.clientWidth;
 windowHeight = document.body.clientHeight;
 }

 // for small pages with total height less then height of the viewport
 if(yScroll < windowHeight){
 pageHeight = windowHeight;
 } else {
 pageHeight = yScroll;
}

 // for small pages with total width less then width of the viewport
 if(xScroll < windowWidth){
 pageWidth = xScroll;
 } else {
pageWidth = windowWidth;
 }

 return [pageWidth,pageHeight];
}
