//window.onload = lae;
window.onscroll = asukoht;
window.onresize = asukoht;

kaugus = 1;
timeout = 50;
samm = 2;

timertest = 0;
var kastId = 'flyThing'

function lae()
{
	if (!document.getElementById(kastId))
		return false;
		
	var kast = document.getElementById(kastId);
	
// 	kast.onmouseover = flyMouseOver;
// 	kast.onmouseout = flyMouseOut;
	
	var date = new Date();
	date.setTime(date.getTime()+(60*60*1000));
	var aegumine = date.toGMTString();
	
	var kypsised = document.cookie.split('; ');
	var lendNait = 0;
	
	for(i=0;i<kypsised.length;i++)
	{
		var kypsis = kypsised[i].split('=');
		if (kypsis[0] == 'lendNait')
		{
			lendNait = kypsis[1];
		}
	}
	
	if (lendNait < FLtimes)
	{
		lendNait++;
		document.cookie = 'lendNait='+lendNait+'; expires='+aegumine+'; path=/';
		
		kast.style.visibility = 'visible';
		kast.style.zIndex = 100;
		
		asukoht();
	}
}

function asukoht()
{
	if (!document.getElementById(kastId))
		return false;

	var kast = document.getElementById(kastId);
	
	wWidth = aknaLaius();
	wHeight = aknaKorgus();
	if (wHeight >= kast.offsetHeight)
	{
		kast.style.top = (wHeight/2)-(kast.offsetHeight/2)+topScroll()+'px';
	}
	if (timertest == 0)
	{
		timertest++;
		if (wWidth >= kast.offsetWidth)
		{
			liiguTimer = setTimeout('liiguta()', timeout);
		}
	}
}

function liiguta()
{
	var kast = document.getElementById(kastId);
	if(kaugus + kast.offsetWidth <= aknaLaius())
	{
		kast.style.left = kaugus+'px';
		kaugus = kaugus + samm;
		liiguTimer = setTimeout('liiguta()', timeout);
	}
	else
	{
		kast.style.visibility = 'hidden';
	}
}

function flyMouseOver()
{
	clearTimeout(liiguTimer);
}

function flyMouseOut()
{
	liiguTimer = setTimeout('liiguta("'+kastId+'")', timeout);
}

function topScroll()
{
	if (window.opera)
	{
		topOffset = document.body.scrollTop;
	}
	else
	{
		topOffset = document.documentElement.scrollTop;
	}
	return topOffset;
}

function aknaKorgus()
{
	if (window.opera)
	{
		wHeight = document.body.clientHeight;
	}
	else
	{
		wHeight = document.documentElement.clientHeight;
	}
	return wHeight;
}

function aknaLaius()
{
	if (window.opera)
	{
		wWidth = document.body.clientWidth;
	}
	else
	{
		wWidth = document.documentElement.clientWidth;
	}
	return wWidth;
}