var im;

var walks = new Array();

window.setTimeout("load_anims();", 3000);

function load_anims()
{
	walks["trab_ltr"] = new anim("ltr/trab_", 17, 15);
	walks["galopp_ltr"] = new anim("ltr/galopp_", 19, 42);
	
	walks["trab_rtl"] = new anim("rtl/trab_", 17, -15);
	walks["galopp_rtl"] = new anim("rtl/galopp_", 19, -42);
}

var curim = 0;

var curwalk;
var cur_dir = "rtl";

window.setTimeout('goponygo();', 10000);

function goponygo()
{
	im = document.getElementById("theim");
	
	if(cur_dir == "rtl")
		cur_dir = "ltr";
	else
		cur_dir = "rtl";
	var trabgalopp = Math.floor(Math.random() * 2);
	
	var tg = "";
	
	if(trabgalopp == 0)
		tg = "trab";
	if(trabgalopp == 1)
		tg = "galopp";
	
	curwalk = walks[tg + "_" + cur_dir];
	
	pony_init();

	var nextrun = Math.floor(Math.random() * 20000) + 20000;
	window.setTimeout('goponygo();', nextrun);
}


function anim(prefix, numimg, speed)
{
	var i;
	
	this.speed = speed;
	this.imgs = new Array();
	this.numimg = numimg;
	
	for(i = 0; i < numimg; i++)
	{
		this.imgs[i] = new Image();
		this.imgs[i].src = "img/pony/" + prefix + PadDigits(i, 5) + ".gif";
	}
}

function pony_init()
{
	document.getElementsByTagName("body")[0].style.overflow = 'hidden';
	im.style.display = "block";
	if(cur_dir == "rtl")
		im.style.left = (getWidth()) + "px"
	pony_render();
}

function pony_render()
{
	curim = (curim + 1) % curwalk.numimg;
	im.src = curwalk.imgs[curim].src;
	var newpos = ((parseInt(im.style.left) + curwalk.speed));
	
	im.style.left = newpos + "px";
	
	if(newpos < getWidth() && newpos > (-500 - parseInt(im.width)))
	{
		window.setTimeout("pony_render();", 40);
	}
	else
	{
		im.style.left = "-500px"
		im.style.display = "none";
		document.getElementsByTagName("body")[0].style.overflow = 'auto';
	}
}


function getWidth()
{
	var d=document,e=d.documentElement,b=d.body,w=window;
	return w.innerWidth ? w.innerWidth:e.clientWidth?e.clientWidth:b.clientWidth?b.clientWidth:0;
}

