<!doctype html>
<html>
<head>
	<title>Picture This 2: Control</title>
	<link rel="stylesheet" type="text/css" href="control.css">
	<script src="ptlib.js"></script>
<script>
var domain = 'http://localhost';
var mypopup = window.open(domain+'/PT2/display.php', 'myWindow');

<?php 
//if ($_POST['key']=="soup") 
$banner2=trim($_POST['key']);
$cardfolder=$banner2;
//$cardfolder = "interplay";
//$banner2 = "boggle";
$team1 = $_POST['team1'];
$team2 = $_POST['team2'];
$clocktime = $_POST['clocktime'];
$cloudmode = $_POST['cloudmode'];
$directmode = $_POST['directmode'];
$words = array();
$wordfilename = "dictionaries/main.txt";
$wordfile = fopen($wordfilename,"r");
flock($wordfile,LOCK_SH);
$word = trim(fgets($wordfile));
while ($word!="999")
{
	$words[]=$word;
	$word = trim(fgets($wordfile));
}
flock($wordfile,LOCK_UN);
fclose($wordfile);
$wordfilename = "dictionaries/added.txt";
$wordfile = fopen($wordfilename,"r");
flock($wordfile,LOCK_SH);
$word = trim(fgets($wordfile));
while ($word!="")
{
	$words[]=$word;
	$word = trim(fgets($wordfile));
}
flock($wordfile,LOCK_UN);
fclose($wordfile);
$numwords = count($words);
print "var words = new Array();\r\n";
for ($i=0;$i<$numwords;$i++)
{
	print "words.push('"."$words[$i]"."');\r\n";
}
?>

var cardfolder = "<?=$cardfolder?>";
var ptCode = 0;
var theGame = "idle";
var team1r1 = 0;
var team1c1 = 0;
var team2r1 = 0;
var team2c1 = 0;
var clocktime = <?=$clocktime?>;
var clockmin="01";
var clocksec="30";
var team1score = 0;
var team2score = 0;
var bigkeys = 0;
var teamname1 = "<?=$team1?>";
var teamname2 = "<?=$team2?>";
var cloudmode = <?=$cloudmode?>;
var newname = "";
var newnamechars = 0;
var live = 0;
var livestate = 0;
var bumpX = -2903;
var bumpY = -407;
var bumpmode=0;
var solved1=0;
var solved2=0;
var clockloop=0;
var team1word = "";
var team2word = "";
var team1last = "";
var team2last = "";
var team1picks = new Array();
var team1rows = new Array();
var team1cols = new Array();
var team2picks = new Array();
var team2rows = new Array();
var team2cols = new Array();
var alphabet = new Array();
var donelist1 = new Array();
var donelist2 = new Array();
var GameOn=0;
var controls=0;
var directmode = <?=$directmode?>;
//preload score images
var scoreimg1=new Image();
scoreimg1.src="graphics/plus3.png";
var scoreimg2=new Image();
scoreimg2.src="graphics/plus4.png";
var scoreimg3=new Image();
scoreimg3.src="graphics/plus6.png";
var scoreimg4=new Image();
scoreimg4.src="graphics/plus9.png";
var scoreimg5=new Image();
scoreimg5.src="graphics/plus12.png";
var scoreimg6=new Image();
scoreimg6.src="graphics/plus16.png";
//removes the big keyboard
//do not move this function to ptlib
//it will be different depending on the game.
function offBigKeys()
{
	live=0;
	//CHANGE THIS TO RESTORE LETTERS
	if (cloudmode==1) goBigClouds(0,0,0); else cardbackshalf(cardfolder,1);
}

function initscores()
{
	var nametext1 = document.getElementById('team1name');
	var nametext2 = document.getElementById('team2name');
	var scorebox = document.getElementById('stats');
	nametext1.innerHTML=teamname1;
	nametext2.innerHTML=teamname2;
	nametext1.style.visibility="visible";
	nametext2.style.visibility="visible";
	scorebox.style.visibility="visible";
	var message = ["names","dash",1,teamname1,1];
	mypopup.postMessage(message,domain);
	var message = ["names","dash",2,teamname2,1];
	mypopup.postMessage(message,domain);
	var message = ["show","stats",0,"div"];
	mypopup.postMessage(message,domain);
}
function doGameClockStart()
{
	var message=["image",5,6,"graphics/black.png",1,0]; //what to put here?
	mypopup.postMessage(message,domain);
	var message=["image",5,7,"graphics/black.png",1,0];
	mypopup.postMessage(message,domain);
	GameOn=1;
	controls=0;
	//do any additional initialization required at first clock start
}
function putletters()
{
	index = 0;
	for (i=1;i<6;i++)
	{
		for (j=1;j<6;j++)
		{
			var letter = alphabet[index];
			var imagefile = "chars/"+letter+".png";
			var message=["image",j,i,imagefile, 1,1]; 
			mypopup.postMessage(message,domain);
			//also send it to the small board.
			var imageid="r"+j+"c"+i;
			var image = document.getElementById(imageid);
			data = "small_chars/"+letter+".png";
			image.src=data;
			image.style.visibility="visible";
			index++;
		}
	}
	index = 0;
	for (i=8;i<13;i++)
	{
		for (j=1;j<6;j++)
		{
			var letter = alphabet[index];
			var imagefile = "chars/"+letter+".png";
			var message=["image",j,i,imagefile, 1,1]; 
			mypopup.postMessage(message,domain);
			//also send it to the small board.
			var imageid="r"+j+"c"+i;
			var image = document.getElementById(imageid);
			data = "small_chars/"+letter+".png";
			image.src=data;
			image.style.visibility="visible";
			index++;
		}
	}
	for (i=6;i<8;i++)
	{
		for (j=1;j<6;j++)
		{
			if ((j==1)&&(i==6)) var imagefile = "graphics/team1.png";
			if ((j==1)&&(i==7)) var imagefile = "graphics/team2.png";
			if (j==2) var imagefile = "graphics/enter.png";
			if (j==3) var imagefile = "graphics/clear.png";
			if (j>3) var imagefile = "graphics/black.png";
			var message=["image",j,i,imagefile, 1,1];
			mypopup.postMessage(message,domain);
			//also send it to the small board.
			var imageid="r"+j+"c"+i;
			var image = document.getElementById(imageid);
			data = "small_"+imagefile;
			image.src=data;
			image.style.visibility="visible";
		}
	}	
}
function scoreIt(teamnum)
{
	var codeimg=new Image();
	if (teamnum==1) var numletters = team1last.length;
	if (teamnum==2) var numletters = team2last.length;
	if (numletters==3) var points = 3;
	if (numletters==4) var points = 4;
	if (numletters==5) var points = 6;
	if (numletters==6) var points = 9;
	if (numletters==7) var points = 12;
	if (numletters>7) var points = 16;
	if (teamnum==1)
	{
		donelist1.push(team1last);
		var scoreid="score1";
		team1score=team1score+points;
		var thescore = team1score;
		var score=document.getElementById(scoreid);
		score.innerHTML=thescore;
		message=["score",1,thescore,1];
		mypopup.postMessage(message,domain);
		var message=["sound","dingding_left.wav"];
		mypopup.postMessage(message,domain);
		codeimg.src="addword.php?word="+team1last;
		team1last = "";
	}
	if (teamnum==2)
	{
		donelist2.push(team2last);	
		var scoreid="score2";
		team2score=team2score+points;
		var thescore = team2score;
		var score=document.getElementById(scoreid);
		score.innerHTML=thescore;
		message=["score",2,thescore,1];
		mypopup.postMessage(message,domain);
		var message=["sound","dingding_right.wav"];
		mypopup.postMessage(message,domain);
		codeimg.src="addword.php?word="+team2last;
		team2last = "";
	}
}
//helper helper function for getAssets
function putstart()
{
	var message=["image",5,6,"graphics/gamestartblack.png",1,0];
	mypopup.postMessage(message,domain);
	var message=["image",5,7,"graphics/exitblack.png",1,0];
	mypopup.postMessage(message,domain);	
	controls=1;
}
function getAssets(allassets)
{
	initscores();
//	alphabet=	["a","a","a","a","a","a","a","a","a","b",
//				"b","c","c","d","d","d","d","e","e","e",
//				"e","e","e","e","e","e","e","e","e","f",
//				"f","g","g","g","h","h","i","i","i","i",
//				"i","i","i","i","i","j","k","l","l","l",
//				"l","m","m","n","n","n","n","n","n","o",
//				"o","o","o","o","o","o","o","p","p","qu",
//				"r","r","r","r","r","r","s","s","s","s",
//				"t","t","t","t","t","t","u","u","u","u","v",
//				"v","w","w","x","y","z"];
				
				alphabet=["A","A","A","A","A","A","A","A","A","B",
	            "B","C","C","D","D","D","D","E","E","E",
				"E","E","E","E","E","E","E","E","E","F",
				"F","G","G","G","H","H","I","I","I","I",
				"I","I","I","I","I","J","K","L","L","L",
				"L","M","M","N","N","N","N","N","N","O",
				"O","O","O","O","O","O","O","P","P","QU",
				"R","R","R","R","R","R","S","S","S","S",
				"T","T","T","T","T","T","U","U","U","U","V",
				"V","W","W","X","Y","Z"];
	
				  
	var numletters = 97;
	alphabet = shuffle(alphabet);		
	setclock(clocktime);	
	bumpy(0);
	putletters();
	//var assettimer = setTimeout(function(){ putstart(); },2500 );
}
function init() 
{
	clearalltext();
	if (directmode)
	{
		//slide3different("<?=$banner2?>3.png","<?=$banner2?>2.png","<?=$banner2?>1.png",0)
		setbanner("<?=$banner2?>1",1);
		setbanner("<?=$banner2?>2",2);
		setbanner("<?=$banner2?>3",3);
		getAssets(1);
		//var assettimer = setTimeout(function(){ getAssets(1); },7500 );	
	}
	else
	{
		if (cardfolder=="soup") //a game has been selected
		{
			setbanner("<?=$banner2?>1",1);
			setbanner("<?=$banner2?>2",2);
			setbanner("<?=$banner2?>3",3);
			//getnewset();

	//		alphabet=["A","A","A","A","A","A","A","A","A","B",
	//	              "B","C","C","D","D","D","D","E","E","E",
	//				  "E","E","E","E","E","E","E","E","E","F",
	//				  "F","G","G","G","H","H","I","I","I","I",
	//				  "I","I","I","I","I","J","K","L","L","L",
	//				  "L","M","M","N","N","N","N","N","N","O",
	//				  "O","O","O","O","O","O","O","P","P","QU",
	//				  "R","R","R","R","R","R","S","S","S","S",
	//				  "T","T","T","T","T","T","U","U","U","U","V",
	//				  "V","W","W","X","Y","Z"];
					  
					  
		// this is for full board play. //need to revise for two players
		// wouldn't it be simpler to randomize the alphabet and take the first 25?
			getAssets(1);
		}
		else //a game has not been selected?
		{
			slide3("<?=$banner2?>.png",0);
			//setallbanners("<?=$banner2?>.png");
			setclock(clocktime);
		}
	}	
	theGame = "boggle";	
}

//deals with keystrokes coming from the arduino
function goBoggle(ptCode,row,col)
{
	var index = ptCode; 
	if (col==6) //team 1 control
	{
		var numletters = team1word.length;
		if (row==2) //enter
		{
			if ((words.indexOf(team1word)>=0)&&(donelist1.indexOf(team1word)<0)) //good word
			{
			//figure out lenth of word and score it
				//var numletters = team1word.length;
				if (numletters==3) 
				{
					var points = 3;
					var pointgraphic = "plus3.png";
				}
				if (numletters==4)
				{
					var points = 4;
					var pointgraphic = "plus4.png";
				}
				if (numletters==5)
				{
					var points = 6;
					var pointgraphic = "plus6.png";
				}
				if (numletters==6)
				{
					var points = 9;
					var pointgraphic = "plus9.png";
				}
				if (numletters==7)
				{
					var points = 12;
					var pointgraphic = "plus12.png";
				}
				if (numletters>7)
				{
					var points = 16;
					var pointgraphic = "plus16.png";
				}	
				donelist1.push(team1word); //need something to keep track of words, eventually
				//also add points to team score...
				var scoreid="score1";
				team1score=team1score+points;
				var thescore = team1score;
				var score=document.getElementById(scoreid);
				score.innerHTML=thescore;
				message=["score",1,thescore,1];
				mypopup.postMessage(message,domain);
			// put the graphics up in the word spaces
				for (i=0;i<numletters;i++)
				{
					var oldimageindex = team1picks[i];
					var oldimageletter = alphabet[oldimageindex].toLowerCase();
					message=["image",team1rows[i],team1cols[i],"graphics/"+pointgraphic, 1, 2, "chars/"+oldimageletter+".png"];
					mypopup.postMessage(message,domain);
				}
				//sound bell
				var message=["sound","dingding_left.wav"];
				mypopup.postMessage(message,domain);
				team1last = "";
				row=3;
			}
			else //bad word
			{
				for (i=0;i<numletters;i++)
				{
					var oldimageindex = team1picks[i];
					var oldimageletter = alphabet[oldimageindex].toLowerCase();
					message=["image",team1rows[i],team1cols[i],"graphics/redcross.png", 1, 2, "chars/"+oldimageletter+".png"];
					mypopup.postMessage(message,domain);
				}
				var message=["sound","dong_left.wav"];
				mypopup.postMessage(message,domain);
				team1last = team1word;
				row=3;
			}
		}
		if (row==3) //clear
		{
			hideteam1borders();
			var nametext1 = document.getElementById('team1name');
			nametext1.innerHTML=teamname1;
			nametext1.style.visibility="visible";
			var message = ["names","dash",1,teamname1,1];
			mypopup.postMessage(message,domain);
			team1word = "";
			team1picks = [];
			team1rows = [];
			team1cols = [];
			team1r1 = 0;
			team1c1 = 0;
		}
	}
	if (col==7) //team 2 control
	{
		var numletters = team2word.length;
		if (row==2) //enter
		{	
			if ((words.indexOf(team2word)>=0)&&(donelist2.indexOf(team2word)<0))//good word
			{
			//figure out lenth of word and score it
				//var numletters = team2word.length;
				if (numletters==3) 
				{
					var points = 3;
					var pointgraphic = "plus3.png";
				}
				if (numletters==4)
				{
					var points = 4;
					var pointgraphic = "plus4.png";
				}
				if (numletters==5)
				{
					var points = 6;
					var pointgraphic = "plus6.png";
				}
				if (numletters==6)
				{
					var points = 9;
					var pointgraphic = "plus9.png";
				}
				if (numletters==7)
				{
					var points = 12;
					var pointgraphic = "plus12.png";
				}
				if (numletters>7)
				{
					var points = 16;
					var pointgraphic = "plus16.png";
				}
;
				donelist2.push(team2word);
				//also add points to team score...
				var scoreid="score2";
				team2score=team2score+points;
				var thescore = team2score;
				var score=document.getElementById(scoreid);
				score.innerHTML=thescore;
				message=["score",2,thescore,1];
				mypopup.postMessage(message,domain);
				for (i=0;i<numletters;i++)
				{
					var oldimageindex = team2picks[i]-35;
					var oldimageletter = alphabet[oldimageindex].toLowerCase();
					message=["image",team2rows[i],team2cols[i],"graphics/"+pointgraphic, 1, 2, "chars/"+oldimageletter+".png"];
					mypopup.postMessage(message,domain);
				}
				//sound bell
				var message=["sound","dingding_right.wav"];
				mypopup.postMessage(message,domain)
				team2last = "";
				row=3;
			}
			else //bad word
			{
				for (i=0;i<numletters;i++)
				{
					var oldimageindex = team2picks[i]-35;
					var oldimageletter = alphabet[oldimageindex].toLowerCase();
					message=["image",team2rows[i],team2cols[i],"graphics/redcross.png", 1, 2, "chars/"+oldimageletter+".png"];
					mypopup.postMessage(message,domain);
				}
				var message=["sound","dong_right.wav"];
				mypopup.postMessage(message,domain);
				team2last = team2word;				
				row=3;
			}	
		}
		if (row==3) //clear
		{
			hideteam2borders();
			var nametext2 = document.getElementById('team2name');
			nametext2.innerHTML=teamname2;
			nametext2.style.visibility="visible";
			var message = ["names","dash",2,teamname2,1];
			mypopup.postMessage(message,domain);
			team2word = "";
			team2picks = [];
			team2rows = [];
			team2cols = [];
			team2r1 = 0;
			team2c1 = 0;
		}
	}
	if (col<6) //team 1 letter
	{
		if (team1picks.indexOf(index)<0)
		{
			//further clarification to ensure a legal pick
			if (((row>=team1r1-1)&&(row<=team1r1+1))||(team1r1==0))
			{	
				if (((col>=team1c1-1)&&(col<=team1c1+1))||(team1c1==0))
				{
					team1picks.push(index);
					team1rows.push(row);
					team1cols.push(col);
					var letterindex = index;
					var theletter = alphabet[letterindex];
					team1word = team1word+theletter; 
					id="br"+row+"c"+col;	
					border = document.getElementById(id);
					var borderfile = "border_green.png";
					var data = "small_graphics/"+borderfile;
					border.src=data;
					border.style.visibility="visible";
					message = ["border",row,col,borderfile,1];
					mypopup.postMessage(message,domain);
					var teamname=document.getElementById('team1name');
					teamname.innerHTML=team1word;
					message = ["names",0,1,team1word,1];
					mypopup.postMessage(message,domain);
					team1r1=row;
					team1c1=col;
				}
			}
		}
	}
	if (col>7) //team 2 letter
	{
		if (team2picks.indexOf(index)<0)
		{
			if (((row>=team2r1-1)&&(row<=team2r1+1))||(team2r1==0))
			{
				if (((col>=team2c1-1)&&(col<=team2c1+1))||(team2c1==0))
				{
					team2picks.push(index);
					team2rows.push(row);
					team2cols.push(col);
					var letterindex = index-35;
					var theletter = alphabet[letterindex];
					team2word = team2word+theletter; 
					id="br"+row+"c"+col;	
					border = document.getElementById(id);
					var borderfile = "border_green.png";
					var data = "small_graphics/"+borderfile;
					border.src=data;
					border.style.visibility="visible";
					message = ["border",row,col,borderfile,1];
					mypopup.postMessage(message,domain);
					var teamname=document.getElementById('team2name');
					teamname.innerHTML=team2word;
					message = ["names",0,2,team2word,1];
					mypopup.postMessage(message,domain);
					team2r1=row;
					team2c1=col;
				}
			}
		}		
	}

}

</script>
</head>
<body>
<div id="container">
<div id="games">
<?php include("panel.php"); ?>
</div>
<div id="keyboard">
<input type="text" class="arduino" id="keyin" value="" onkeypress="return dokey(event)">
<BR><div id="ptindex">0</div><div id="ptgrid">r0 c0</div>
</div>

<div id="smallboard">
	<img id="smallscreen" src="small_graphics/smallscreen.png">
<!-- banners -->
	<img id="banner1" class="row0" class="col1" src="small_banners/match.png">
	<img id="banner2" class="row0" class="col5" src="small_banners/match.png">
	<img id="banner3" class="row0" class="col9" src="small_banners/match.png">
<?php include "preview.php"; ?>
</div>
</div>
</body>
<script>
var inittimer = setTimeout(function(){ init(); },1000 );
var loop = setInterval("setFocus();", 100);
</script>
</html>