// Script by Raphaël Mazoyer (raf@phase4.net).
// Please quote source if you want to use it.
// http://web.phase4.net/
// Version 1.01 - 15/09/99

// The square at (7,3) must be named "img-73", and must launch action "hop('73')".

// Number of lines in the game (must be 9 or under) :
nx = 6;

// Number of columns in the game (must be 9 or under) :
ny = 6;

// Path and name of the transparent file :
tr = '/assets/images/puzzle1/white.gif';

// Path and name of the missing image file :
bo = '/assets/images/puzzle1/img-16.png';

// End of configuration variables.


var canStart = false;
var hasStarted = false;
pos = new Array(); // Position array, initialized from original image position.
ind = new Array(); // Indexed array, used to shuffle the images.
rnd = new Array(); // Randomized array, used to display the images when beginning a game.

function hop(which) {
	if (hasStarted == true) {
		x = which.charAt(0);
		y = which.charAt(1);
		var tx = 0;
		var ty = 0;
		for (var i = 1; i <= nx; i++) {
			for (var j = 1; j <= ny; j++) {
				if (document.images['img-' + i.toString() + j.toString()].src.indexOf(tr) != -1) {
					if((i + 1 == x && j == y) || (i - 1 == x && j == y) || (j + 1 == y && i == x) || (j - 1 == y && i == x)) {
						tx = i;
						ty = j;
					}
				}
			}
		}
		if(tx != 0 && ty != 0) {
			document.images['img-' + tx.toString() + ty.toString()].src = document.images['img-' + which].src;
			document.images['img-' + which].src = tr;
			checkPositions(which);
		}
		//else alert('Tss...');
	}
	else {
		randomizePositions();
	}
}

function randomizePositions() {
	if (canStart == true) {
		hasStarted = true;
		for (var i = 1; i <= nx; i++) {
			for (var j = 1; j <= ny; j++) {
				var rm = i.toString() + j.toString();
				while (rnd[rm] == null) {
					var rn = Math.round(Math.random()*(ind.length - 1));
					if (ind[rn] != null) {
						rnd[rm] = ind[rn];
						ind[rn] = null;
					}
				}
			}
		}
		for (var i = 1; i <= nx; i++) {
			for (var j = 1; j <= ny; j++) {
				var rm = i.toString() + j.toString();
				document.images['img-' + rm].src = rnd[rm];
			}
		}
		ind = new Array();
		rnd = new Array();
		for (var i = 1; i <= nx; i++) {
			for (var j = 1; j <= ny; j++) {
				var rm = i.toString() + j.toString();
				ind[ind.length] = pos[rm];
			}
		}
		//document.controlpanel.startbutton.value = "Reshuffle";
		document.getElementById('start').innerHTML = 'RE-INICIAR'
		//document.getElementById('enter_site_pt').style.visibility = 'visible'
		//document.getElementById('enter_site_en').style.visibility = 'visible'
	}
	else {
		//alert('Please wait until\nthe entire page is loaded.');
	}
}

function checkPositions(which) {
	var wrong = false;
	for (var i = 1; i <= nx; i++) {
		for (var j = 1; j <= ny; j++) {
			var rm = i.toString() + j.toString();
			if(document.images['img-' + rm].src != pos[rm])
			wrong = true;
		}
	}
	if (wrong == false) {
		document.images['img-' + which].src = bo;
		//document.controlpanel.startbutton.value = 'You won!';
		document.getElementById('start').innerHTML = 'PARABÉNS!!'
		//if(confirm('You won!!\n\nWanna play again?')) {
		//	document.images['img-' + which].src = tr;
		//	randomizePositions();
		//}
		//document.controlpanel.startbutton.value = 'Restart';
	}
}

function startGame() {
	if (document.images) {
		bonusImage = new Image();
		bonusImage.src = bo;
		for (var i = 1; i <= nx; i++) {
			for (var j = 1; j <= ny; j++) {
				var rm = i.toString() + j.toString();
				pos[rm] = document.images['img-' + rm].src;
				ind[ind.length] = pos[rm];
			}
		}
		//document.controlpanel.startbutton.value = "Start";
		canStart = true;
	}
	else {
		alert('You can not view this page,\nwhich requires basic dynamic\nfeatures from your browser.\n\nPlease upgrade!');
		location.href="http://www.microsoft.com/windows/ie/download/all.htm";
	}
}