var bgcolor="ffffff";   

var gridwidth=4;       
var gridheight=4;       

var piecewidth=85;      
var pieceheight=85;     

var blankx=3;                    
var blanky=3;           
                        
var gfxtype="jpg";      
//var quiturl="http://mid-day.com/news/travel/";

var hintpic="jigsaw/hint.jpg"; 
var hintwidth=170;      
var hintheight=170;     

var hintbutton="yes";   

var browserName=navigator.appName;
var browserVer=parseInt(navigator.appVersion);

if ((browserName == "Netscape" && browserVer>=3) || (browserName == "Microsoft Internet Explorer" && browserVer>=4))
{ var version="yes"; }
else { var version="no"; }

if (version=="yes")
{
  var prevx=-1; var prevy=-1;     
  var moves=0;                    
  var playing=false;              

  var blank=new Image(piecewidth,pieceheight);    
  blank.src="jigsaw/blank"+blankx+blanky+".gif"; 
}


 
function slide(clickx,clicky,forceslide)
{
       
  if (!playing && !forceslide)
  {
    alert("\n\n\tYou must shuffle the pieces before you begin playing!\t\t\n\n");
    return;
  }

  if ((clickx==blankx) && (clicky==blanky)) return;

  if ((playing || forceslide) && (clickx==blankx) || (clicky==blanky))
  {
    if (clicky>blanky) slideUp(clickx,clicky);
    if (clicky<blanky) slideDown(clickx,clicky);
    if (clickx>blankx) slideLeft(clickx,clicky);
    if (clickx<blankx) slideRight(clickx,clicky);
               
    blankx=clickx; blanky=clicky;

    if (playing) { moves++; checkSolved(); }
  }
}



function slideUp(clickx,clicky)
{
  for(movey=blanky;movey<clicky;movey++)
  {
    movefrom="at"+clickx+(movey+1); 
    moveto="at"+clickx+movey;       
	document.images[moveto].src=document.images[movefrom].src;
  }
  document.images[movefrom].src=blank.src;
}



function slideDown(clickx,clicky)
{
  for(movey=blanky;movey>clicky;movey--)
  {
    movefrom="at"+clickx+(movey-1); 
    moveto="at"+clickx+movey;       
    document.images[moveto].src=document.images[movefrom].src;
  }
  document.images[movefrom].src=blank.src;
}



function slideLeft(clickx,clicky)
{
  for(movex=blankx;movex<clickx;movex++)
  {
    movefrom="at"+(movex+1)+clicky; 
    moveto="at"+movex+clicky;       
    document.images[moveto].src=document.images[movefrom].src;
  }
  document.images[movefrom].src=blank.src;
}



function slideRight(clickx,clicky)
{
  for(movex=blankx;movex>clickx;movex--)
  {
    movefrom="at"+(movex-1)+clicky; 
    moveto="at"+movex+clicky;       
    document.images[moveto].src=document.images[movefrom].src;
  }
  document.images[movefrom].src=blank.src;
}



function hint()
{
  hintparams="toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width="+(hintwidth+50)+",height="+(hintheight+50)

  hintwindow=open('','hintwindow',hintparams);
  hintwindow.document.writeln('<html>');
  hintwindow.document.writeln('<head>');
  hintwindow.document.writeln('<title>MAKE A BRAIN - HINT</title>');
  hintwindow.document.writeln('</head>');
  hintwindow.document.writeln('<body bgcolor="#'+bgcolor+'" onLoad="window.focus();timeout=setTimeout(\'window.close()\',10000);">');
  hintwindow.document.writeln('<table cellspacing=0 cellpadding=0 border=0 width="100%" height="100%"><tr><td align=center><img alt="completed puzzle" src="'+hintpic+'" width='+hintwidth+' height='+hintheight+'></td></tr></table>');
  hintwindow.document.writeln('</body>');
  hintwindow.document.writeln('</html>');
  hintwindow.document.close();
}



function randomSlide(iterations)
{
  if (version=="no")
  {
    alert("\n\n\tYou are not using a browser that can deal with the latest JavaScript 1.1 language.\t\n\tSorry, you can't play this version of the game.\t\t\n\n");
  }
  else
  {
    playing=false;

    while (iterations--)
    {
      clickx=blankx;
      clicky=blanky;

      if (Math.random()>.5)   
      {
        while ((clickx==blankx) || (clickx==prevx))
		{ 
		  clickx=Math.floor(Math.random()*gridwidth);
		}
        clicky=blanky;
      }
      else                    
      {
        while ((clicky==blanky) || (clicky==prevy)) 
		{ 
		  clicky=Math.floor(Math.random()*gridheight);
		}
        clickx=blankx;
      }

      prevx=blankx;
      prevy=blanky;

      slide(clickx,clicky,true);
    }

    checkSolved();
    playing=true;
	
	startTimer();
  }
}



function checkSolved()
{
  solved=true;
  for(y=0;y<gridheight;y++) 
  {
    for(x=0;x<gridwidth;x++)
    {
      name1="at"+x+y;
      block=document.images[name1].src;
      start=block.lastIndexOf(".")-2;
      name2="at"+block.substring(start,start+2);
      if (name1!=name2) solved=false;
    }
  }

  if (solved)
  {
    stopTimer();
    replace="at"+blankx+blanky;
    document.images[replace].src="jigsaw/map"+blankx+blanky+"."+gfxtype;
	alert("\n\n\tCONGRATULATIONS!\n\tYou solved the puzzle in " + moves + " moves.\n\n");
    document.f1.score.value=moves;
    document.f1.submit();
  }
}


// that's all folks -->

var t,tt,ttt
tt = 0
ttt=0

function timer()
{
  tt++
  document.f1.time.value = tt
  ttt = setTimeout("timer()",1000) 
}

function startTimer()
{
  clearTimeout(ttt)
  timer() 
}

function stopTimer()
{
   clearTimeout(ttt)
   //ttt = 0
}

