//  Slider puzzle copyright (c) 2002, Ferruggia Web Design
//  Ferruggia Web Design
//  Email: donf@warwick.net

var empty = new Image;
empty.src = "puz0.jpg";
var grid = new Array(9);
var pieces = new Array(9);
var unused = new Image;

function clickable_image (imgname,w,h) {
  var w1 = w + 20 ;
  var h1 = h + 100 ;
  var mywin = window.open ("","","width=" + w1 +",height=" + h1) ;
  mywin.document.write ("<html><head><title>Clickable Image</title></head><body><center>") ;
  mywin.document.write ("<b>To save this image, right-click on it and choose \"Save Image As ...\"</b><p>");
  mywin.document.write ("<img src='",imgname,"' width=",w," height=",h," border=1>") ;
  mywin.document.write ("</center></body></html>") ;
  mywin.focus () ;
}

function gothere (where) {
	location = where;
}

function move (me, neighbors) {
	for (var n = 0 ; n < neighbors.length ; n++) {
		if (neighbors[n] == empty.where) {
			grid[empty.where].src = grid[me].src;
			grid[me].src = empty.src;
			empty.where = me;
			solved();
			return;
		}
	}
}

function shuffle(prefix) {
	for (var n = 0 ; n < 9 ; n++) {
		grid[n] = document.getElementById(prefix + (n+1));
	}
	for (var n = 0 ; n < 9 ; n++) {
		pieces[n] = new Image ;
		pieces[n].src = "puz" + (n + 1) + ".jpg" ;
	}
	var all = "012345678" ;
	for (var n = 0 ; n < 9 ; n++) {
		var i = Math.floor (Math.random () * all.length);
		grid[n].src = pieces[all.charAt (i)].src;
		all = all.substring(0,i) + all.substring(i+1);
	}
	var i = Math.floor (Math.random () * 9) ;
	unused.src = grid[i].src ;
	grid[i].src = empty.src ;
	empty.where = i ;
}

function solved() {
	for (var n = 0 ; n < 9 ; n++) {
		if (n != empty.where && grid[n].src != pieces[n].src) {
			return ;
		}
	}
	grid[empty.where].src = unused.src ;
	empty.where = -1 ;
	setTimeout ("gothere('puzzlewon.html')",1000) ;
}

