var mainarr = new Array();
var gamearr = new Array(64);

var obj1 = new Object();
var obj2 = new Object();
var clickEnabled = true;

var words = new Array();
var testwords = new Array();
var wordPtr = 0;

var OFFSETX = 113;
var OFFSETY = 10;
var selectedObj = null;




//=========================================================================================================
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

function resetWords() {
	words.length = 0;
	testwords.length = 0;

	words.push('car');
	words.push('computer');
	words.push('electric');
	words.push('curtain');
	words.push('feelings');
	words.push('light');
	words.push('machine');
	words.push('button');
	words.push('right');
	words.push('flight');
	words.push('bird');
	words.push('giraffe');
	words.push('unconscious');
	words.push('clouds');
	words.push('road');
	words.push('paint');
	words.push('bright');
	words.push('telephone');
	words.push('country');
	words.push('long');
	words.push('short');
	words.push('wing');
	words.push('animal');
	words.push('orange');
	words.push('instrument');
	words.push('music');
	words.push('carpet');
	words.push('forest');
	words.push('snow');
	words.push('woman');
	words.push('clock');
	words.push('peanut');
	words.push('blouse');
	words.push('hello');
	words.push('laughing');
	words.push('dust');
	words.push('fairy');
	words.push('excellent');
	words.push('walking');
	words.push('aura');
	words.push('flow');
	words.push('river');
	words.push('ocean');
	words.push('bananas');
	words.push('flee');
	words.push('euphoria');
	words.push('spike');
	words.push('jacket');
}
function createTestWords() {
	var randnum;

	document.getElementById('screen').innerHTML = "<font color=\"#ff0000\">Click Start!</font>";

	wordPtr = 0;

	for(var i = 0; i < 20; i++) {
		randnum = Math.floor(Math.random()*words.length);
		testwords.push(words[randnum]);
		words.splice(randnum,1);
	}
}
function checkAns() {
	var num = 0;

	for(var i = 1;i<21;i++){
		document.getElementById('a'+i).style.backgroundColor="#aaaafe";

		num = i*0.1*1000;

		if(trim(document.getElementById('a'+i).value.toLowerCase())==testwords[i-1].toLowerCase()) {
			setTimeout("document.getElementById('a'+"+i+").style.backgroundColor='#11ff11'",num);
		}
		else {
			setTimeout("document.getElementById('a'+"+i+").style.backgroundColor='#ff9999'",num);
		}
		setTimeout("document.getElementById('sa'+"+i+").innerHTML='"+testwords[i-1]+"'",num);
	}
}
function showPopup() {
	parentObj = document.getElementById('wg');
	obj = document.getElementById('wga');

	for(var i = 1; i < 21; i++) {
		document.getElementById('a'+i).value="";
		document.getElementById('a'+i).style.backgroundColor="#aaaafe";
		document.getElementById('sa'+i).innerHTML="";
	}
	
	obj.style.display = parentObj.style.display;
	obj.style.top = parseInt(parentObj.style.top.replace(/[px]/gi,''))+parseInt(-100)+'px';
	if((parseInt(parentObj.style.top.replace(/[px]/gi,''))+parseInt(-100)) < 0) {
		obj.style.top = '0px';
	}
	obj.style.left = parseInt(parentObj.style.left.replace(/[px]/gi,''))+parseInt(20)+'px';
}
function nextWord(obj) {
	if(wordPtr == 0) {
		document.getElementById('nb').value = "Next Word";
		document.getElementById('nb').style.width = 85;
	}
	if(wordPtr >= testwords.length) {
		obj.innerHTML = "<font color=\"#ff0000\">Finished.</font>";
		document.getElementById('nb').value = "Click to Test Your Memory";
		document.getElementById('nb').style.width = 175;
		document.getElementById('rb').style.visibility = 'visible';
		document.getElementById('nb').onclick = showPopup;
	}
	else {
		obj.innerHTML = parseInt(wordPtr)+1 + ": " + testwords[wordPtr];
	}
	wordPtr++;
}
function resetWordGame() {
	document.getElementById('rb').style.visibility = 'hidden';
	document.getElementById('nb').value = 'Start';
	resetWords();
	createTestWords();
	document.getElementById('nb').onclick = nwCont;
}
function nwCont() {
	nextWord(document.getElementById('screen'));
}

//=========================================================================================================





function initMainArr() {
	mainarr.length = 0;

	mainarr.push('A');
	mainarr.push('B');
	mainarr.push('C');
	mainarr.push('D');
	mainarr.push('E');
	mainarr.push('F');
	mainarr.push('G');
	mainarr.push('H');
	mainarr.push('I');
	mainarr.push('J');
	mainarr.push('K');
	mainarr.push('L');
	mainarr.push('M');
	mainarr.push('N');
	mainarr.push('O');
	mainarr.push('P');
	mainarr.push('Q');
	mainarr.push('R');
	mainarr.push('S');
	mainarr.push('T');
	mainarr.push('U');
	mainarr.push('V');
	mainarr.push('W');
	mainarr.push('X');
	mainarr.push('Y');
	mainarr.push('Z');
	mainarr.push('1');
	mainarr.push('2');
	mainarr.push('3');
	mainarr.push('4');
	mainarr.push('5');
	mainarr.push('6');
	mainarr.push('A');
	mainarr.push('B');
	mainarr.push('C');
	mainarr.push('D');
	mainarr.push('E');
	mainarr.push('F');
	mainarr.push('G');
	mainarr.push('H');
	mainarr.push('I');
	mainarr.push('J');
	mainarr.push('K');
	mainarr.push('L');
	mainarr.push('M');
	mainarr.push('N');
	mainarr.push('O');
	mainarr.push('P');
	mainarr.push('Q');
	mainarr.push('R');
	mainarr.push('S');
	mainarr.push('T');
	mainarr.push('U');
	mainarr.push('V');
	mainarr.push('W');
	mainarr.push('X');
	mainarr.push('Y');
	mainarr.push('Z');
	mainarr.push('1');
	mainarr.push('2');
	mainarr.push('3');
	mainarr.push('4');
	mainarr.push('5');
	mainarr.push('6');
}

function setVals() {
	var randnum = 99;

	document.getElementById("totalcount").value = 0;

	for(var i = 0; i < 64; i++) {
		randnum = Math.floor(Math.random()*mainarr.length);
		gamearr[i] = mainarr[randnum];
		mainarr.splice(randnum,1);
	}
}

function toggleShow(obj) {
	if(!clickEnabled) {
		clearTimeout(timer);
		cinc();
	}
	if(obj.innerHTML=="") {
		document.getElementById("totalcount").value++;
		obj.innerHTML = gamearr[obj.id - 1];
		if(obj1.className != "c") {
			obj1 = obj;
		}
		else if(obj2.className != "c") {
			clickEnabled = false;
			obj2 = obj;
			checkIfMatch();
		}
	}
}
function checkIfMatch() {
	if(obj1.innerHTML != obj2.innerHTML) {
		timer = setTimeout("cinc()", 1500);
	}
	else {
		obj1.style.backgroundColor = "#00ff00";
		obj2.style.backgroundColor = "#00ff00";
		obj1 = new Object();
		obj2 = new Object();

		clickEnabled = true;
	}
}
function cinc() {
	obj1.innerHTML = "";
	obj2.innerHTML = "";
	obj1 = new Object();
	obj2 = new Object();
	clickEnabled = true;
}
function h(obj) {
	if (obj.style.backgroundColor != "#00ff00" && obj.style.backgroundColor != "rgb(0, 255, 0)") {
		obj.style.backgroundColor="#ffff00";
	}
}
function u(obj) {
	if (obj.style.backgroundColor != "#00ff00" && obj.style.backgroundColor != "rgb(0, 255, 0)") {
		obj.style.backgroundColor="#aaaafe";
	}
}
function resetGame() {
	cinc();
	initMainArr();
	setVals();
	for(var i = 1; i < 65; i++){
		document.getElementById(i).innerHTML = "";
		document.getElementById(i).style.backgroundColor = "#aaaafe";
	}
}

function drag(e,dobj) {
	if(!e){
		e=window.event
	}
	if (dobj) {
	//	if(window.event) {
	//		dobj.style.pixelLeft = (e.clientX-OFFSETX+parseInt(document.body.scrollLeft))
	//		dobj.style.pixelTop = (e.clientY-OFFSETY+parseInt(document.body.scrollTop))
	//	}
	//	else {
			dobj.style.left = (e.clientX-OFFSETX+parseInt(document.body.scrollLeft))
		if(e.clientY-OFFSETY+parseInt(document.body.scrollTop)>0)
			dobj.style.top =  (e.clientY-OFFSETY+parseInt(document.body.scrollTop))
	//	}
	}
}

function checkdrag(event,obj) {
	if(obj){
		if(obj.className=="ctrl") {
			selectedObj = obj.parentNode;
		}
		if(obj.className=="main") {
			selectedObj = obj.parentNode.parentNode;
		}
		OFFSETX = event.clientX-selectedObj.style.left.replace(/[px]/gi,'')
	}
	else {
		selectedObj = null;
	}
}

function showGame(e,arg) {
	document.getElementById(arg).style.display = "block";
	drag(e,document.getElementById(arg));
}
function hideGame(arg) {
	document.getElementById(arg).style.display = "none";
}
