var blankX=1, blankY=1, diffX, diffY, from, to, current_location=new Array(0, 3, 8, 7, 2, 6, 4, 1, 5), target_location=new Array(0, 1, 2, 3, 4, 5, 6, 7, 8), save_location, attempts=0

function move(tileX, tileY) {  // 
  
  diffX=tileX-blankX;
  diffY=tileY-blankY;
  to=((tileY-1)*3)+tileX-1;
  from=((blankY-1)*3)+blankX-1;
  
  if ((diffX==0) && (diffY==0)) alert("That's not a valid move!")
  else
    {
	if (((diffX==0) && ((diffY==1) || (diffY==-1))) || ((diffY==0) && ((diffX==1) || (diffX==-1)))) 
	{
	blankX=tileX;
	blankY=tileY;
	document.box[from].src=document.box[to].src;
    document.box[to].src="images/tile1.jpg";
	save_location=current_location[from];
	current_location[from]=current_location[to];
	current_location[to]=save_location;
    attempts++; 
	set_scoreboard();	
	}
	else alert("That's not a valid move!")
    }
	
  }

function set_scoreboard() {  // 
  
  var score=0;
  
  for (var i=0;i<9;i++){
    if (target_location[i]==current_location[i]) score++
  }
  if (score==9) MM_setTextOfLayer('intro','','Congratulations! You finished the game in ' + attempts + ' moves.')
  if (attempts==1) MM_setTextOfLayer('scoreboard','','You have had 1 move.')
  else MM_setTextOfLayer('scoreboard','','You have had ' + attempts + ' moves.')
  }

function MM_setTextOfLayer(objName,x,newText) { //v3.0
  if ((obj=MM_findObj(objName))!=null) with (obj)
    if (navigator.appName=='Netscape') {document.write(unescape(newText)); document.close();}
    else innerHTML = unescape(newText);
}



