var minX=0, minY=0,  maxX=740, maxY=500;
var boxSize=1, boxLeft=1, boxTop=1;
var score=0;
var bestScore=0;
var startTime=0;
var endTime=0;
var instructionsShown=false;

function setTheBox() {
	if (boxSize<5) boxSize = 5;
	if (boxLeft<minX) boxLeft = minX;
	if (boxLeft>maxX-boxSize) boxLeft = maxX-boxSize;
	if (boxTop<minY) boxTop = minY;
	if (boxTop>maxY-boxSize) boxTop = maxY-boxSize;
	setEProp("thebox","width",boxSize+"px");
	setEProp("thebox","height",boxSize+"px");
	setEProp("thebox","left",boxLeft+"px");
	setEProp("thebox","top",boxTop+"px");
}

function init() {
	setEProp("all","height",(maxY)+"px");
	setEProp("playarea","height",(maxY)+"px");
}

function jump() {
	if (boxSize>20) {
		boxSize *= 0.75;
		boxLeft = (boxLeft + maxX*Math.random())%maxX;
		boxTop = (boxTop + maxY*Math.random())%maxY;
		setTheBox();
	} else {
		endTime=new Date().getTime();
		score = (endTime-startTime)/1000;
		if (score<bestScore || bestScore==0)
			bestScore = score;
		setEContent("status","<p align='right'>This Game:"+score+"&nbsp;&nbsp;&nbsp;  Best Score This Session:"+bestScore+"</p>");
		getERef("startbutton").disabled=false;
		getERef("startbutton").value="START";
	}
}

function ranInit() {
	var t = new Date().getTime()/1000;
	t = t%100;
	for (var i=0; i<t; i++) Math.random();
}

function start() {
	ranInit();
	if (instructionsShown) instructions();
	boxSize=400;
	boxLeft=0;
	boxTop=0;
	startTime=new Date().getTime();
	setEContent("status","<p></p>");
	var b = getERef("startbutton");
	b.disabled=true;
	b.value="----";
	jump();
}

function showInstructions() {
	setEProp("instructions","visibility","visible");
	getERef("startbutton").disabled=true;
}

function hideInstructions() {
	setEProp("instructions","visibility","hidden");
	getERef("startbutton").disabled=false;
}

function instructions() {
	if (instructionsShown) {
		getERef("instructionsbutton").value='SHOW INSTRUCTIONS';
		setEProp("instructions","visibility","hidden");
		instructionsShown=false;
	} else {
		getERef("instructionsbutton").value='HIDE INSTRUCTIONS';
		setEProp("instructions","visibility","visible");
		instructionsShown=true;
	}
}

