var arena; 
// execute on page load
onload=function(){
	// get arena object
	arena = document.getElementById('arena');
	score_keep = document.getElementById('score_keep');
	score_keep.scores = new Array(); 
	serial_num();
}


// random number function, with range
function new_rand(min, max){
	nrand = 0;
	while(nrand < min) nrand = Math.floor(Math.random()*max);
	return nrand;
}

// function tat sets everything up
function serial_num(){
	// set arena blank
	arena.innerHTML = "";
	// create new elements
	divSer_stat = document.createElement('DIV');
	divSer_input = document.createElement('INPUT');
	// set id for div and type for input
	divSer_stat.id = "ser_stat";
	divSer_input.id = "ser_input";
	divSer_input.type = "text";
	// put them in the arena
	arena.appendChild(divSer_stat);
	arena.appendChild(divSer_input);
	
	// set events for input box
	divSer_input.onkeyup = function(event){serial_check(this,event)};
	divSer_input.onkeypress = function(event){serial_check(this,event)};
	
	// set new timer and finish
	divSer_input.timer = false;
	divSer_input.fin = false;
	
	// make new random numbers
	// num to subtract
	divSer_input.rand = new_rand(6, 10);
	//beginning number
	divSer_input.beg_num = new_rand(87, 110);
	// set our everchanging current number to the beginning number;
	divSer_input.cur_num = divSer_input.beg_num;

	// set div to state what the beginning num and subtracting factor is
	divSer_stat.innerHTML = divSer_input.cur_num+" minus "+divSer_input.rand;
}


// function that checks inputed numbers
function serial_check(numid, event){
	if(!event) event = window.event;

	// get the current value of input box
	num = numid.value;
	eq = document.getElementById("ser_stat");
	event = event ? event : document.event;

	// check if that value is correct answer to equation
	if(num == (numid.cur_num-numid.rand) && !numid.fin){
		// set beginning time on first correct answer
		if(!numid.timer){
			date = new Date;
			numid.timer = date.getTime();
		}
		// set next number
		numid.cur_num = (numid.cur_num-numid.rand)
		// check to see if next iteration of subtraction falls below 1
		if((numid.cur_num-numid.rand) <= 0){
			// get current time
			date = new Date;
			// figure out how long it took them to reach 0 (in milliseconds, divide by 1000)
			total_time = Math.round((date.getTime() - numid.timer)/1000);
			// get score
			total_score = score(numid.beg_num, numid.rand, total_time);
			
			score_keep.scores.push(total_score);
			sorted = score_keep.scores.sort(sortNumbers);
			score_keep.innerHTML = "";
			length = sorted.length >= 5 ? 5 : sorted.length;
			for(i=0; i < length; i++){	
				score_keep.innerHTML += "<ol>"+sorted[i]+"</ol>";
			}
					
			// set the div box to reflect results
			eq.innerHTML = 'Completed in '+total_time+' seconds!<br>';
			eq.innerHTML += 'Your score is '+total_score+'<br>'; 
			eq.innerHTML += 'Press enter to go <a onclick="serial_num()" style="cursor: pointer; color: #00f;">again!';
			// set numid.finish variable to true
			numid.fin = true;
			// make input box blank
			numid.value = "";
		}
		// if next iterationd oesn't fall below one, continue!
		else{
			// set div to next equation
			eq.innerHTML = numid.cur_num+" minus "+numid.rand;
			// make input box blank
			numid.value = "";
		}
	}
	// if not correct answer in input box, check for enter key
	else if(event.keyCode == 13){
		// enter key pressed and sequence is numid.finished, start a new one!
		if(numid.fin){
			numid.value = "";
			serial_num();
		}
		// not numid.finished then make input box blank
		else{
			numid.value = "";
		}
	}
}

function sortNumbers(a, b) { return b - a} 

// calculate score using arbitrary system
function score(start, num, time){
	var dif;
	
	// find difficulty of number
	switch(num){
		case 6 :
			dif = 0.58;
			break;
		case 7 :
			dif = 0.68;
			break;
		case 8 : 
			dif = 0.58;
			break;
		case 9 :
			dif = 0.52;
			break;
		default:
			dif = 0;
	}
	
	// I played with the equation until i got numbers 
	// that seemed to represent the actual skill one
	// would need to get a higher score
	total_score = Math.round(((start/num)*dif)/time*100);
	
	return total_score;
}