//var SpeelBord=new Array(7)
//var Shaduf=new Array(7)
//var Water=new Array(7)

//var nPlayers=3
playdemo = false;

//standaard hoogte en breedte is 50
CHeight = 90;
CWidth = 90;
sKey ="";
sActiveTile = 1;
iActiveTourist = 1;
iRound = 0;
iTouristState = 0;

//basis functies
function ShowSpeelBord(array){
  var str = "" 
  for (ii = 0; ii <= 6; ii++) { 
  for (jj = 0; jj <= 6; jj++) { 
    str = str + "<img src=\"L" +  array [ii][jj] + ".gif\" height="+CHeight+" width="+CWidth+" >" 
	}
	str = str + "<br>"
  } 
  return str;
}

function EindeBeurt(){
	iTouristState=0;
	RecalcRedLayer(SpeelBord);
	ShowLayer1();
	
	RefreshSpeelbordlayer();
	document.getElementById('RedLayer').innerHTML=ShowRedlayer2(SpeelBord);
}

function rand( n )
{
  return ( Math.floor ( Math.random ( ) * n + 1 ) );
}

function RemoveActiveTile(){
  for (ii = 0; ii <= 6; ii++) { 
  for (jj = 0; jj <= 6; jj++) { 
	if (SpeelBord[ii][jj] == sActiveTile) {
		SpeelBord[ii][jj]=0;
		Redlayer[ii][jj]=-1;
		Tile[sActiveTile][0]=0;
	}
  }
  }
}


function IsTileOnBoard(iTile){
  ret=false;
  for (ii = 0; ii <= 6; ii++) { 
  for (jj = 0; jj <= 6; jj++) { 
	if (SpeelBord[ii][jj] == iTile) {
		ret=true;
	}
  }
  }
  return ret;
}

function EvalType(value, type){
	var ret;
	ret = false;
	if (type==-1) {ret = (value == type)};
	if (type==0) {ret = (value == 0)};
	if (type==1) {ret = (value == 1)};
	if (type==2) {ret = (value == 2)};
	if (type==3) {ret = (value == 3)};
	if (type==4) {ret = (value == 4)};
	if (type==5) {ret = ((value == 3) || (value == 1))};
	if (type==6) {ret = ((value == 2) || (value == 0))};
	return ret;
}

function LookAround(array, x, y, type){
	var c = 0;	
	t2 =  LookInDirection(array, x, y, 2, type);
	t4 =  LookInDirection(array, x, y, 4, type);
	t6 =  LookInDirection(array, x, y, 6, type);
	t8 =  LookInDirection(array, x, y, 8, type);
	if (t2>0) {c = c + t2;}
	if (t4>0) {c = c + t4;}
	if (t6>0) {c = c + t6;}
	if (t8>0) {c = c + t8;}
	return c;
}

function EmptyArray(array, value){
  for (ii = 0; ii <= 6; ii++) { 
  for (jj = 0; jj <= 6; jj++) { 
	array[ii][jj] = value;
  }
  }
}

function CopyArray(from, to){
  for (ii = 0; ii <= 6; ii++) { 
  for (jj = 0; jj <= 6; jj++) { 
	to[ii][jj] = from[ii][jj];
  }
  }
}

function GetEindtelling(from, to){
  xstart = 999;
  ystart = 999;
  for (ii = 0; ii <= 6; ii++) { 
  for (jj = 0; jj <= 6; jj++) { 
	if ((from[ii][jj] >0) && ((xstart==999) || (ii<xstart))) xstart = ii;
	if ((from[ii][jj] >0) && ((ystart==999) || (jj<ystart))) ystart = jj;
  }
  }
  
  var iTourist = -1;
  for (ii = 0; ii <= 3; ii++) { 
	//ii gaat van boven naar beneden
  for (jj = 0; jj <= 3; jj++) { 
	if (Tile[from[xstart+ii][ystart+jj]][5]==1){iTourist=iTourist+1;Tourist[iTourist][0]=[ii*2];Tourist[iTourist][1]=[jj*2];}
	if (Tile[from[xstart+ii][ystart+jj]][5]==2){iTourist=iTourist+1;Tourist[iTourist][0]=[ii*2];Tourist[iTourist][1]=[jj*2+1];}
	if (Tile[from[xstart+ii][ystart+jj]][5]==3){iTourist=iTourist+1;Tourist[iTourist][0]=[ii*2+1];Tourist[iTourist][1]=[jj*2+1];}
	if (Tile[from[xstart+ii][ystart+jj]][5]==4){iTourist=iTourist+1;Tourist[iTourist][0]=[ii*2+1];Tourist[iTourist][1]=[jj*2];}
	
	//bovenste regel van links naar rechts
    to[ii*2][jj*2] = TilePos(from[xstart+ii][ystart+jj],1) 
	to[ii*2][jj*2+1] = TilePos(from[xstart+ii][ystart+jj],2) 
	}

  for (jj = 0; jj <= 3; jj++) { 
	//onderste regel van links naar rechts
    to[ii*2+1][jj*2] = TilePos(from[xstart+ii][ystart+jj],4) 
	to[ii*2+1][jj*2+1] = TilePos(from[xstart+ii][ystart+jj],3) 
	}
  } 
}

function LookInDirection(array, x, y, dir, type){
	var c = 0;
	v = GetRelativeEindtellingTile(array, x, y, dir, -1);

	if (((v==0 || v==2) && (type==1)) || ((v==0) && (type==0))) {
//	if ((v==0 || v==2) && (type==1)) {
		if (dir ==2) {c = c + 1 + parseInt(LookInDirection(array, parseInt(eval(x-1)), parseInt(y), dir, type));}
		if (dir ==4) {c = c + 1 + parseInt(LookInDirection(array, parseInt(x), parseInt(eval(y+1)), dir, type));}
		if (dir ==6) {c = c + 1 + parseInt(LookInDirection(array, parseInt(eval(x+1)), parseInt(y), dir, type));}
		if (dir ==8) {c = c + 1 + parseInt(LookInDirection(array, parseInt(x), parseInt(eval(y-1)), dir, type));}	
		//alert(c);
		}
	else
		{
		c = c;
		}
	//alert(x + ' ' + y + '  ' + c);
	return parseInt(c);
}

function CountSize(array, x, y, evaltyp){
	var c = 0;	
	if ((x>-1) && (x<8) && (y>-1) && (y<8)) {
		if ((Temp[x][y] ==0) && (EvalType(array[x][y],evaltyp))) {
			Temp[x][y] = 1;
			c = c + 1;
			c = c + CountSize(array, x-1, y, evaltyp);
			c = c + CountSize(array, x+1, y, evaltyp);
			c = c + CountSize(array, x, y-1, evaltyp);
			c = c + CountSize(array, x, y+1, evaltyp);
			}
		}
	return c;
}

function CountSize2Start(array, x, y, evaltyp){
	c = 0;	
	var toetstyp;
	toetstyp=evaltyp;
	if (evaltyp==5){ 
		toetstyp=3;
		}
	else if (evaltyp==6){ 
		toetstyp=2;
		}	
	if (x>-1 && x<8 && y>-1 && y<8 && Temp[x][y] ==0 && EvalType(array[x][y],toetstyp)) {
		c = c + CountSize2(array, x, y, evaltyp);
		}
	if (evaltyp>=7){ 
		c = c + CountSize2(array, x, y, evaltyp);
		}
	return c;
}


function CountSize2(array, x, y, evaltyp){
	c = 0;	
	//if (Temp[x][y] == 0 && array[x][y]==-1) {
	//alert('cz '+ x +' '+y + '  ' + eval(x-1) +' '+eval(y-1));
	var toetstyp;
	if (evaltyp==5){ 
		toetstyp=3;
		}
	if (evaltyp==6){ 
		toetstyp=2;
		}		
	
	if (evaltyp==7){ 
		c=0;
		if (EvalType(array[x][y],1)) {
			c = LookAround(array, x, y, 0);
			}
		return c;
		}
	else if (evaltyp==8){ 
		c=0;
		if (EvalType(array[x][y],1)) {
			c = LookAround(array, x, y, 1);
			}
		return c;
		}
	else{
		if (x>-1 && x<8 && y>-1 && y<8 && Temp[x][y] ==0 && EvalType(array[x][y],evaltyp)) {
			c = c + 1;
			Temp[x][y] = 2;
			if (EvalType(array[x][y],toetstyp)) {
				Temp[x][y] = 1;
				c = c + CountSize2(array, eval(x-1), y, evaltyp);
				c = c + CountSize2(array, eval(x+1), y, evaltyp);
				c = c + CountSize2(array, x, eval(y-1), evaltyp);
				c = c + CountSize2(array, x, eval(y+1), evaltyp);
				}
			}
		return c;
	}
}

function CountTouristen(){
	var TouristCount;
	TouristCount=0;
	for (i=0;i<=24; i++){
		if (Tile[i][5] != 0) {
			TouristCount = TouristCount+1;
			}
		}
	return TouristCount;
}


function PlaatsTourist(iTile, sLabel){
	//zet in touristplaats mode, zodat de tegels niet meer gedraaid mogen worden
	
	iTouristState = 1;
	if ((iTile!=0) && (iTile == sActiveTile) && (TilePos([iTile],sLabel)>0))  {
		Tile[iTile][5] = sLabel;
		iActiveTourist = (7-CountTouristen());
		ShowRound(iRound);
	}
}

function Act(x,y, sLabel){
	//16: shift, 17: CTRL
	if (sKey==16){
		// je mag nog niet of shift drukken, als er geen tegel is geplaatst
		if (Redlayer[x][y] == sActiveTile) {
			PlaatsTourist(SpeelBord[x][y], sLabel);
			/*if ((SpeelBord[x][y]!=0) && (SpeelBord[x][y] == sActiveTile) ) {
				//alert(SpeelBord[x][y] + '  ' + sLabel );
				Tile[SpeelBord[x][y]][5] = sLabel;
				iActiveTourist = (iActiveTourist + 1);
			}
			*/
		}
		}
	else if (sKey==17){
			//je kan alleen de actieve tegel draaien
		if (SpeelBord[x][y] == sActiveTile) {
			RotateTileR(SpeelBord[x][y]);
			//Tile[SpeelBord[x][y]][0] = Tile[SpeelBord[x][y]][0] + 1;
			//if (Tile[SpeelBord[x][y]][0]>4) {Tile[SpeelBord[x][y]][0]=1}
			}
			
			

	}
	else{
		if (iTouristState == 1) {
			PlaatsTourist(SpeelBord[x][y], sLabel);
		}
		else{
		if (Redlayer[x][y]==-1) {
			//als de positie leeg is mag je een tegel toevoegen (controleer wel of die niet eerder is uitgedeeld!)
			/*if (Tile[sActiveTile][0]>0)
				{
					alert('Deze tile is al toegevoegd');
				}
			else
			*/
				{
					RemoveActiveTile();
					SpeelBord[x][y]=sActiveTile;
					Redlayer[x][y]=sActiveTile;
					Tile[sActiveTile][0]=1;
					ShowRound(iRound);
				}	
			}
		}
	}

	RefreshSpeelbordlayer();
	document.getElementById('RedLayer').innerHTML=ShowRedlayer2(SpeelBord);
}

function RefreshSpeelbordlayer(){
	document.getElementById('SpeelBordLayer').innerHTML=ShowRedlayer(Redlayer);
}

function RotateTileL(iTile){
	//als je touristen gaat plaatsen, mag je niet meer de tegel draaien 
	//if (iTouristState == 0) {
	if (Tile[iTile][5] == 0) {
		Tile[iTile][0] = Tile[iTile][0] + 1;
		if (Tile[iTile][0]>4) {Tile[iTile][0]=1}
	}
}
function RotateTileR(iTile){
	//als je touristen gaat plaatsen, mag je niet meer de tegel draaien 
	//if (iTouristState == 0) {
	if (Tile[iTile][5] == 0) {
		Tile[iTile][0] = Tile[iTile][0] - 1;
		if (Tile[iTile][0]<1) {Tile[iTile][0]=4}
	}
}

function ActDbl(x,y){
	alert('dbl' + x);
	Tile[SpeelBord[x][y]][0] = Tile[SpeelBord[x][y]][0] + 1;
	
	document.getElementById('RedLayer').innerHTML=ShowRedlayer(Redlayer);
}

function TilePos(iTile, pos){
	p = pos + Tile[iTile][0]-1
	if  (Tile[iTile][0] == 0) 
		{
			p=p+1;
		}
	if (p>4){
		p=p-4;
		}
	return Tile[iTile][p]
}

function keydown(){
   sKey = window.event.keyCode;
}

function keyup(){
   sKey = "";
}


function ShowRedlayer(array){
  var str = "" 
  for (ii = 0; ii <= 6; ii++) { 
	//ii gaat van boven naar beneden
  for (jj = 0; jj <= 6; jj++) { 
	//bovenste regel van links naar rechts
    str = str + "<img src=\"L" + TilePos(array[ii][jj],1) + ".gif\" height="+CHeight/2+" width="+CWidth/2+" onmousedown=\"Act("+ii+","+jj+",'1');\" onDblClick=\"ActDbl("+ii+","+jj+");\">" 
    str = str + "<img src=\"L" + TilePos(array[ii][jj],2) + ".gif\" height="+CHeight/2+" width="+CWidth/2+" onmousedown=\"Act("+ii+","+jj+",'2');\" onDblClick=\"ActDbl("+ii+","+jj+");\">" 
	}
	str = str + "<br>"
  for (jj = 0; jj <= 6; jj++) { 
	//onderste regel van links naar rechts
    str = str + "<img src=\"L" + TilePos(array[ii][jj],4) + ".gif\" height="+CHeight/2+" width="+CWidth/2+" onmousedown=\"Act("+ii+","+jj+",'4');\" onDblClick=\"ActDbl("+ii+","+jj+");\">" 
    str = str + "<img src=\"L" +  TilePos(array[ii][jj],3) + ".gif\" height="+CHeight/2+" width="+CWidth/2+" onmousedown=\"Act("+ii+","+jj+",'3');\" onDblClick=\"ActDbl("+ii+","+jj+");\">" 
	}
	str = str + "<br>"
  } 
  return str;
}


function TouristPos(iTile, pos){
	p=0;
	if ( Tile[iTile][5] == pos) {
		/*
		p =1;
		var ii;
		for (ii = 1; ii < parseInt(iTile); ii++) {
			if (Tile[ii][5]>0){p=p+1;}
		}
		*/
		p =1;
	}
	return p;
}

function TouristPosGetAction(iTile, pos, ii, jj){
	s = "onmousedown=\"Act("+ii+","+jj+","+pos+");\" onDblClick=\"ActDbl("+ii+","+jj+");\""
	return s;
}

function ShowRedlayer2(array){
  var str = "" 
  for (ii = 0; ii <= 6; ii++) { 
	//ii gaat van boven naar beneden
  for (jj = 0; jj <= 6; jj++) { 
	//bovenste regel van links naar rechts
    str = str + "<img src=\"W" + TouristPos(array[ii][jj],1) + ".gif\" height="+CHeight/2+" width="+CWidth/2+" " + TouristPosGetAction(array[ii][jj],1,ii,jj) + ">" 
    str = str + "<img src=\"W" + TouristPos(array[ii][jj],2) + ".gif\" height="+CHeight/2+" width="+CWidth/2+" " + TouristPosGetAction(array[ii][jj],2,ii,jj) + ">" 
	}
	str = str + "<br>"
  for (jj = 0; jj <= 6; jj++) { 
	//onderste regel van links naar rechts
    str = str + "<img src=\"W" + TouristPos(array[ii][jj],4) + ".gif\" height="+CHeight/2+" width="+CWidth/2+" " + TouristPosGetAction(array[ii][jj],4,ii,jj) + ">" 
    str = str + "<img src=\"W" +  TouristPos(array[ii][jj],3) + ".gif\" height="+CHeight/2+" width="+CWidth/2+" " + TouristPosGetAction(array[ii][jj],3,ii,jj) + ">" 
	}
	str = str + "<br>"
  } 
  return str;
}



function GetValue(array, x, y, nullvalue){
   if ((x>0) && (x<=6) && (y>0) && (y<=6)){
	return array[x][y];
   }
   else {
        return nullvalue;
   }
}


function ShiftArray(array, direction){
  CopyArray(array, Shaduf)

  dx=0;
  dy=0;
  if (direction==1) {dy=-1}
  if (direction==3) {dy=+1}
  if (direction==2) {dx=-1}
  if (direction==4) {dx=-1}

  for (ii = 0; ii <= 6; ii++) { 
  for (jj = 0; jj <= 6; jj++) { 

	array[ii][jj] = GetValue(Shaduf,ii+dx,jj+dy,0)
	}
  } 
}

function GetRelativeTile(array, x, y, pos, negative){
	if (pos ==0) {xx = x; yy=y}
	if (pos ==1) {xx = x-1; yy=y-1}
	if (pos ==2) {xx = x-1; yy=y}
	if (pos ==3) {xx = x-1; yy=y+1}
	if (pos ==4) {xx = x; yy=y+1}
	if (pos ==5) {xx = x+1; yy=y+1}
	if (pos ==6) {xx = x+1; yy=y}
	if (pos ==7) {xx = x+1; yy=y-1}
	if (pos ==8) {xx = x; yy=y-1}
	fNegative = false;
	fNegative = fNegative || (xx<0) || (xx>6) || (yy<0) || (yy>6);
	if (fNegative) {
			ret = negative;
		}
	else
		{
			ret = array[xx][yy];
		}
	return ret;
}

function GetRelativeEindtellingTile(array, x, y, pos, negative){
	if (pos ==0) {xx = x; yy=y}
	if (pos ==1) {xx = x-1; yy=y-1}
	if (pos ==2) {xx = x-1; yy=y}
	if (pos ==3) {xx = x-1; yy=y+1}
	if (pos ==4) {xx = x; yy=y+1}
	if (pos ==5) {xx = x+1; yy=y+1}
	if (pos ==6) {xx = x+1; yy=y}
	if (pos ==7) {xx = x+1; yy=y-1}
	if (pos ==8) {xx = x; yy=y-1}
	fNegative = false;
	fNegative = fNegative || (xx<0) || (xx>7) || (yy<0) || (yy>7);
	if (fNegative) {
			ret = negative;
		}
	else
		{
			ret = array[xx][yy];
		}
	return ret;
}

function ChangeLayer(){

  for (ii = 0; ii <= 6; ii++) { 
	for (jj = 0; jj <= 6; jj++) { 

		if (SpeelBord[ii][jj] == 0) {
			t1 = GetRelativeTile(SpeelBord, ii, jj, 1, 0) != 0;
			t3 = GetRelativeTile(SpeelBord, ii, jj, 3, 0) != 0;
			t5 = GetRelativeTile(SpeelBord, ii, jj, 5, 0) != 0;
			t7 = GetRelativeTile(SpeelBord, ii, jj, 7, 0) != 0;
			
			t2 = GetRelativeTile(SpeelBord, ii, jj, 2, 0) != 0;
			t4 = GetRelativeTile(SpeelBord, ii, jj, 4, 0) != 0;
			t6 = GetRelativeTile(SpeelBord, ii, jj, 6, 0) != 0;
			t8 = GetRelativeTile(SpeelBord, ii, jj, 8, 0) != 0;
					
			if (t1 || t3 || t5 || t7) { SpeelBord[ii][jj] = -1;}
			//if (t2 || t4 || t6 || t8) { SpeelBord[ii][jj] = -1;}		  
		}
	
	}
  }
}
  

function RecalcRedLayer(SourceLayer){
//berekent de layout van de redlayer
  for (ii = 0; ii <= 6; ii++) { 
	for (jj = 0; jj <= 6; jj++) { 
		if (SourceLayer[ii][jj] == 0) {
		
			t1 = GetRelativeTile(SourceLayer, ii, jj, 1, 0) != 0;
			t3 = GetRelativeTile(SourceLayer, ii, jj, 3, 0) != 0;
			t5 = GetRelativeTile(SourceLayer, ii, jj, 5, 0) != 0;
			t7 = GetRelativeTile(SourceLayer, ii, jj, 7, 0) != 0;
			
			t2 = GetRelativeTile(SourceLayer, ii, jj, 2, 0) != 0;
			t4 = GetRelativeTile(SourceLayer, ii, jj, 4, 0) != 0;
			t6 = GetRelativeTile(SourceLayer, ii, jj, 6, 0) != 0;
			t8 = GetRelativeTile(SourceLayer, ii, jj, 8, 0) != 0;
					
			if ( (t1 || t3 || t5 || t7) == true) {
				Redlayer[ii][jj] = -1;
			}
			else{
				Redlayer[ii][jj] = 0;
			}
			if (iRound>3) {
				if ( (t2 || t4 || t6 || t8) == true) {
					Redlayer[ii][jj] = -1;
				}
			}


		}
	else
		{
			Redlayer[ii][jj] = SourceLayer[ii][jj];
		}
	
	}
  }


  var iC = 0;
  var imin = -1;
  var imax = -1;
  var jmin = -1;
  var jmax = -1;
  for (ii = 0; ii <= 6; ii++) { 
	for (jj = 0; jj <= 6; jj++) { 
	if (SourceLayer[ii][jj] != 0)
		 {
                     iC = 1;
		     if ((imin == -1) | (imin>ii)) {imin=ii;}	
		     if ((imax == -1) | (imax<ii)) {imax=ii;}	
		     if ((jmin == -1) | (jmin>jj)) {jmin=jj;}	
		     if ((jmax == -1) | (jmax<jj)) {jmax=jj;}	
		 }
	}
	}

  if (iC == 0) {
	Redlayer[3][3] = -1;
	}
  if ((imax-imin)==3) {
	  for (ii = 0; ii < imin; ii++) { 
		for (jj = 0; jj <= 6; jj++) { 
			Redlayer[ii][jj] = 0;
		}}
	  for (ii = imax+1; ii <= 6; ii++) { 
		for (jj = 0; jj <= 6; jj++) { 
			Redlayer[ii][jj] = 0;
		}}
  }
  if ((jmax-jmin)==3) {
	  for (ii = 0; ii <=6; ii++) { 
		for (jj = 0; jj < jmin; jj++) { 
			Redlayer[ii][jj] = 0;
		}}
	  for (ii = 0; ii <= 6; ii++) { 
		for (jj = jmax+1; jj <= 6; jj++) { 
			Redlayer[ii][jj] = 0;
		}}
  }

  //alert (imin+'  ' + imax +'  ' + imin+'  '+imax);
}

  
function redraw(millis){ 
	window.showModalDialog("javascript:document.writeln('<" + "script" + ">setTimeout(\"window.close();\"," + millis + ");</" + "script" + ">')");
	}	

function PauseAndRefresh(millis){
	// demo play mode true or false;
	if (playdemo){
		ShowLayer1();
		redraw(millis);
		}
}


 