// javascript to simulate the game of "pong"

var xcount = 1;
var ycount = 1;
	
function animate() {
	var box = document.getElementById('pongbox');
	var xpos = parseFloat(box.style.left, 10);
	var ypos = parseFloat(box.style.top, 10);
	var loop1, loop2, loop3, loop4;

	if (xpos <= -1.3) {
		xcount = 1;
	}
	if (xpos >= 41.5) {
		xcount = 0;
	}
	if (ypos <= -1.3) {
		ycount = 1;
	}
	if (ypos >= 26.5) {
		ycount = 0;
	}
	if (xcount == 1) {
		if (ycount == 1) {
			xpos = xpos+2/10;
			ypos = ypos+1/10;
			box.style.left = xpos + "em";
			box.style.top = ypos + "em";
			loop1 = setTimeout(animate, 20);
		} else {
			xpos = xpos+2/10;
			ypos = ypos-1/10;
			box.style.left = xpos + "em";
			box.style.top = ypos + "em";
			loop2 = setTimeout(animate, 20);
		}
	} else {
		if (ycount == 1) {
			xpos = xpos-2/10;
			ypos = ypos+1/10;
			box.style.left = xpos + "em";
			box.style.top = ypos + "em";
			loop3 = setTimeout(animate, 20);
		} else {
			xpos = xpos-2/10;
			ypos = ypos-1/10;
			box.style.left = xpos + "em";
			box.style.top = ypos + "em";
			loop4 = setTimeout(animate, 20);
		}
	}
}

function init() {
	var box = document.getElementById('pongbox');
	box.style.top = '-1.3em';
	box.style.left = '-1.3em';
	animate();
}

window.onload = init;