/* Copyright 2004 Louis Jean­Richard (www.ljr.ch). All Rights Reserved. */
//	-----------------------------------
var ROWS = 4, COLUMNS = 4
var CELLS = ROWS * COLUMNS

var slide = new Preload("../aa/game/3/")
slide.load("slidep", CELLS + 1, ".jpg")

var blankedCell = randomInteger(1, CELLS)
var jumpMode = false
var matchedCells = 0
var moves = 0
var score = new Score()

//	-----------------------------------
function                                imageFile(imageNumber){
	return slide.image[imageNumber].src
}
//	-----------------------------------
function 								cellName(cellNumber){
	return 'SLIDE' + cellNumber
}
//	-----------------------------------
function                           areSameColumn(cell1, cell2) {
	return ((cell1 % COLUMNS) == (cell2 % COLUMNS))
}
//	-----------------------------------
function                              areSameRow(cell1, cell2) {
	return ((cell1 - ((cell1 - 1) % COLUMNS))
		== (cell2 - ((cell2 - 1) % COLUMNS)))
}
//	-----------------------------------
function                                clickedSlide(cell) {
	if ((cell == blankedCell) || (matchedCells == CELLS))
		return
	if (jumpMode)
		moveSlide(cell, blankedCell)
	else
		if (areSameColumn(cell, blankedCell)) {
			var i = blankedCell
			if (cell < blankedCell)
				while (i > cell) {
					var j = i
					i -= COLUMNS
					moveSlide(i, j)
				}
			else
				while (i < cell) {
					var j = i
					i += COLUMNS
					moveSlide(i, j)
				}
		}
		else
			if (areSameRow(cell, blankedCell)) {
				var i = blankedCell
				if (cell < blankedCell)
					while (i > cell) {
						var j = i
						i--
						moveSlide(i, j)
					}
				else
					var i = blankedCell
					while (i < cell) {
						var j = i
						i++
						moveSlide(i, j)
					}
			}
			else
				return
	moves++
	score.update(moves)
	blankedCell = cell
	if (matchedCells != CELLS)
		document.images[cellName(blankedCell)].src = imageFile(0)
	else {
		document.images[cellName(blankedCell)].src
			= imageFile(blankedCell)
		window.defaultStatus = msg3(moves)
	}
}
//	-----------------------------------
function                           moveSlide(fromCell, toCell) {
	var	mc = document.images[cellName(fromCell)].src
	if (imageFile(fromCell) == mc)
		matchedCells -= 1
	if (imageFile(toCell) == mc)
		matchedCells += 1
	document.images[cellName(toCell)].src = mc
}
//	-----------------------------------
function                                togglePlayMode() {
	if (!jumpMode) {
		jumpMode = true
		window.defaultStatus = msg2()
	}
}
//	-----------------------------------
function                                displayControl() {
	var	output = '<form name=GC>'
	output += '<input class="button" type="button" value='
	output += sQuoted(text1)
	output += " onClick='javascript:togglePlayMode()'>"
	output += "</form>"
	return output
}
//	-----------------------------------
function                                displayBoard() {
	var perm = new Permutation(CELLS)
	perm.random()
	var prime = 3

	matchedCells = 1
	moves = 0
	var	cell = 0
	var	output = ""
	for (var y=0; y < ROWS; y++) {
		for (var x=0; x < COLUMNS; x++) {
			cell++
			output += '<a href="javascript:clickedSlide('
			output += cell + ')">'
			output += '<img name=' + cellName(cell)
			output += ' src='
			if (cell == blankedCell)
				output += quoted(imageFile(0))
			else {
				var idx = (perm.index[cell] * prime % CELLS) + 1
				output += quoted(imageFile(idx))
				if (cell == idx)
					matchedCells += 1
			}
			output += ' /></a>'
		}
		output += '<br />'
	}
	return output
}

//	============================================================
//							S C O R E
//	============================================================

function 								Score(aGame, aBoard) {
	this.moves = 0
	this.chrono = new Chrono()
	this.displayed = false
	//
	this.board = aBoard
}
//	-----------------------------------
function                                htmlScore(){
	output = '<form name="SCORE">'
	output += '<table><tr><td>'
	output += text5d + '</td><td>'
	output += text5e
	output += "</td></tr><tr><td>"
	output += htmlDisplay('moves', '') + '</td><td>'
	output += htmlDisplay('time', '')
	output += "</td></tr></table>"
	output += "</form>"
	this.displayed = true
	return output
}
Score.prototype.html = htmlScore
//	-----------------------------------
function                                resetScore(){
	this.moves = 0
	document.SCORE.time.value = ''
	document.SCORE.moves.value = this.moves
	this.chrono.stop()
}
Score.prototype.reset = resetScore
//	-----------------------------------
function                                updateScore(moves){
	if (moves == 0)
		this.reset()
	else {
		this.moves = moves
		if (moves == 1)
			this.chrono.start()
		else
			document.SCORE.time.value = this.chrono.elapsedMinutes()
	}
	document.SCORE.moves.value = this.moves
}
Score.prototype.update = updateScore

//	============================================================
//						I N T E R F A C E
//	============================================================

function                                displayGame(language) {
	initLanguage(language)
	var output = '<table class="game"><tr><td class="score">'
	output += score.html()
	output += '</td><td class="board" rowspan="2">'
	output += displayBoard()
	output += '</td></tr><tr><td class="control">'
	output += displayControl()
	output += '</td></tr></table>'

	document.open()
	document.write(output)
	document.close()
	window.defaultStatus = msg1()
}

//	============================================================
var langCode = undefined
var text1 = 'Play simpler variant'
var text5d = 'moves'
var text5e = 'time'
//	-----------------------------------
function initLanguage(language) {
	if (langCode == undefined)
		langCode = language
	else
		return
	switch (langCode) {
	case 'de':
		text1 = 'Spiele einfachere Variante'
		text5d = 'Schritte'
		text5e = 'Zeit'
		break
	case 'es':
		text1 = 'Juega variaci&ograve;n mas simple'
		text5d = 'pasos'
		text5e = 'tiempo'
		break
	case 'fr':
		text1 = 'Joue la variante plus simple'
		text5d = 'pas'
		text5e = 'temps'
		break
	case 'it':
		text1 = 'Gioca la variante pi&ugrave; semplice'
		text5d = 'mosse'
		text5e = 'tempo'
		break
	}
}
//	-----------------------------------
function msg1(){
	switch (langCode) {
	case 'de':
		return 'Klicken Sie auf den vertikal- oder horizontalanliegenden Bildausschnitt um ihn in den leeren Ausschnitt zu verschieben.'
	case 'es':
		return 'Clicar la ficha sida verticalmente o horizontalmente a la ficha vacia para moverla en esta misma.'
	case 'fr':
		return 'Cliquez sur le pan &agrave; la verticale ou &agrave; la horizontale du pan vide pour le d&eacute;placer dans celui-ci.'
	case 'it':
		return "Cliccate sulla tessera alla verticale o all'orizontale della tessera vuota per spostarla dentro questa."
	default:
		return 'Click on the vertically or horizontally adjacent slide to shift it toward the empty one.'
	}
}
//	-----------------------------------
function msg2(){
	switch (langCode) {
	case 'de':
		return 'Klicken Sie auf den Bildausschnitt der mit dem leeren Ausschnitt zu tauschen ist.'
	case 'es':
		return 'Clicar la ficha que se desea cambiar por la vacia.'
	case 'fr':
		return 'Cliquez sur le pan que vous d&eacute;sirez &eacute;changer avec le pan vide.'
	case 'it':
		return 'Cliccate sulla tessera che volete scambiare con quella vuota.'
	default:
		return 'Click on the slide to swap with the empty one.'
	}
}
//	-----------------------------------
function msg3(N){
	switch (langCode) {
	case 'de':
		return 'Mit ' + N + ' Bewegungen haben Sie Erfolg gehabt!'
	case 'es':
		return 'Usted termin&ograve; en ' + N + ' pasos!'
	case 'fr':
		return 'Vou avez r&eacute;ussi en ' + N + ' mouvements!'
	case 'it':
		return 'Siete riusciti a finire con ' + N + ' mosse!'
	default:
		return 'You succeded within ' + N + ' moves!'
	}
}
//	-----------------------------------
