File: D:/HostingSpaces/SBogers10/opendag.komma.pro/wwwroot/js/matching.js
var tiles = new Array(),
flips = new Array('rl'),
iFlippedTile = null,
iTileBeingFlippedId = null,
tileImages = new Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14),
//tileImages = new Array(1, 2),
tileAllocation = null,
iTimer = 0,
iInterval = 50,
iPeekTime = 1000;
totalFound = 0;
timesTried = 0;
startTime = null;
function getRandomImageForTile() {
var iRandomImage = Math.floor((Math.random() * tileAllocation.length)),
iMaxImageUse = 2;
while (tileAllocation[iRandomImage] >= iMaxImageUse) {
iRandomImage = iRandomImage + 1;
if (iRandomImage >= tileAllocation.length) {
iRandomImage = 0;
}
}
return iRandomImage;
}
function createTile(iCounter) {
var curTile = new tile("tile" + iCounter),
iRandomImage = getRandomImageForTile();
tileAllocation[iRandomImage] = tileAllocation[iRandomImage] + 1;
curTile.setFrontColor("tileColor");
curTile.setStartAt(500 * Math.floor((Math.random() * 5) + 1));
curTile.setFlipMethod(flips[Math.floor((Math.random() * 3) + 1)]);
curTile.setBackContentImage("images/" + (iRandomImage + 1) + ".jpg");
return curTile;
}
function initState() {
/* Reset the tile allocation count array. This
is used to ensure each image is only
allocated twice.
*/
tileAllocation = new Array();
for (i = 0; i < tileImages.length; ++i) {
tileAllocation.push(0);
}
while (tiles.length > 0) {
tiles.pop();
}
$('#board').empty();
iTimer = 0;
}
function initTiles() {
var iCounter = 0,
curTile = null;
initState();
// Randomly create twenty tiles and render to board
for (iCounter = 0; iCounter < (tileImages.length * 2); iCounter++) {
curTile = createTile(iCounter);
$('#board').append(curTile.getHTML());
tiles.push(curTile);
}
}
function hideTiles(callback) {
var iCounter = 0;
for (iCounter = 0; iCounter < tiles.length; iCounter++) {
tiles[iCounter].revertFlip();
}
callback();
}
function revealTiles(callback) {
var iCounter = 0,
bTileNotFlipped = false;
for (iCounter = 0; iCounter < tiles.length; iCounter++) {
if (tiles[iCounter].getFlipped() === false) {
if (iTimer > tiles[iCounter].getStartAt()) {
tiles[iCounter].flip();
}
else {
bTileNotFlipped = true;
}
}
}
iTimer = iTimer + iInterval;
if (bTileNotFlipped === true) {
setTimeout("revealTiles(" + callback + ")", iInterval);
} else {
callback();
}
}
function playAudio(sAudio) {
var audioElement = document.getElementById('audioEngine');
if (audioElement !== null) {
audioElement.src = sAudio;
audioElement.play();
}
}
function checkMatch() {
if (iFlippedTile === null) {
iFlippedTile = iTileBeingFlippedId;
} else {
timesTried++;
if (tiles[iFlippedTile].getBackContentImage() !== tiles[iTileBeingFlippedId].getBackContentImage()) {
var staytime = 1001
setTimeout("tiles[" + iFlippedTile + "].revertFlip()", staytime);
setTimeout("tiles[" + iTileBeingFlippedId + "].revertFlip()", staytime);
} else {
totalFound++;
}
score = 20000-Math.ceil((timesTried + ($.now() - startTime ))/100);
if(score > 0) $score = 0
$('.score').html(score)
if (totalFound == tileImages.length) {
startTime = null;
}
iFlippedTile = null;
iTileBeingFlippedId = null;
}
}
function onPeekComplete() {
$('div.tile').click(function () {
if(startTime == null){
startTime = $.now();
}
if (totalFound != tileImages.length) {
$('.poging').html(timesTried + 1);
}
iTileBeingFlippedId = this.id.substring("tile".length);
if (tiles[iTileBeingFlippedId].getFlipped() === false) {
tiles[iTileBeingFlippedId].addFlipCompleteCallback(function () {
checkMatch();
});
tiles[iTileBeingFlippedId].flip();
}
});
}
function onPeekStart() {
setTimeout("hideTiles( function() { onPeekComplete(); })", iPeekTime);
}
$(document).ready(function () {
$('#startGameButton').click(function () {
timesTried = 0;
totalFound = 0;
$('.poging').html(0);
$('.score').html(20000)
initTiles();
onPeekComplete();
//setTimeout("revealTiles(function() { onPeekStart(); })",iInterval);
});
});