/* ==========================================================================
 * clNumberMindGame.js
 *
 * Written by Kostas Symeonidis
 * Copyright (c) 2009, CyLog Software
 * ==========================================================================
 * History:
 *
 * 19.Dec.2009  Created library for new round of CyLog games
 * ==========================================================================
 *
 * ==========================================================================
 */

// ==========================================================================

// ---- Constants -----------------------------------------------------------

var MAX_IMAGES = 25;
var IMG_CLOSED = 99;
var IMG_UNKNOWN = 98;

var imgclosed = new Image(48, 48);
imgclosed.src = "/static/games/numimg/closed.png";

// ---- Global Variables ----------------------------------------------------

var imgs = new Array();
var imgCount = 0;

// ---- Global initialisation -----------------------------------------------

var imgsFname = new Array();

var tiles = new Array();  // the actual tiles of the board
var sig = new Array();    // the signature of the solution gradually being built

var o1 = IMG_CLOSED;
var state = 0;
var score = 0;
var visible = 0;
var gameStarted = false;
var starttime;
var prevtime;
var endtime;

var lblScore;     // label for displaying current score
var lblTime;      // displays the time
var edtScore;     // hidden form element to submit the score
var edtDuration;  // hidden form element to submit the duration
var edtSig;       // hidden form element to submit the signature
var ajaxMsg;      // ajax keep alive message

var signature;   // being built up as we reveal cards

/*
 * Returns the current time in milliseconds
 */
function timeInMillis ()
{
    var now = new Date();
    return now.getTime();
}

/*
 * Allocates and sets up all images from 0..imgCount-1 
 */
function setupImageFilenames ()
{
    for (var i = 0; i < imgCount; i++)
    {
        imgs[i] = new Image(48, 48);
        imgsFname[i] = lPadZero(i+1, 2) + ".png";
        imgs[i].src = "/static/games/numimg/" + imgsFname[i];

        tiles[i] = i;
        sig[i] = IMG_UNKNOWN;
    }
}

/*
 * Gets the MAX_IMAGES filenames and mixes them up 100 times
 */
function randomMixUpTiles ()
{
    var h1,h2,h3;
    for (var i = 0; i < 300; i++)
    {
        h1 = Math.floor(Math.random() * imgCount);
        h2 = Math.floor(Math.random() * imgCount);
        if (h1 != h2)
        {
            h3 = tiles[h1];
            tiles[h1] = tiles[h2];
            tiles[h2] = h3;
        }
    }
}

/*
 * Sets up a NumberMind game for the given Rows and Columns
 */
function setupNumberMindGame ( a_iRows, a_iCols )
{
    imgCount = a_iRows * a_iCols;

    setupImageFilenames();
    randomMixUpTiles();

    // page placeholders that will be updating
    lblScore = document.getElementById("lblScore").firstChild;
    lblTime = document.getElementById("lblTime").firstChild;
    edtScore = document.getElementById("edtScore");
    edtDuration = document.getElementById("edtDuration");
    edtSig = document.getElementById("edtSig");

    ajaxMsg = document.getElementById("ajaxMsg");

    // start score
    score = 0;

    /*
    var s = "";
    for (var i = 0; i < imgCount; i++)
    {
        if (i != 0)
            s += ", ";
        s += tiles[i] + "-" + imgsFname[tiles[i]].substr(0, 2);
    }
    edtSig.value = "[" + s + "]";
    */
}

function buildSignature ()
{
    var s = "";
    for (var i = 0; i < imgCount; i++)
    {
        if (i != 0)
            s += ", ";
        s += sig[i] + "-" + imgsFname[sig[i]].substr(0, 2);
    }
    edtSig.value = s;
}

function hideimage ()
{
    document.images["bb" + o1].src = imgclosed.src;
    state = 0;
}

function imageClick ( btn )
{
    if (!gameStarted)
    {
        gameStarted = true;
        starttime = timeInMillis();
        setInterval("displayTime()", 1000);
    }

    if ((tiles[btn] != IMG_CLOSED) && (state == 0))
    {
        // do Ajax call
        ajaxMsg.value = btn + "-" + tiles[btn];
        invoke($('form')[1], 'ajax', '#replaceWithAjax');

        // increase state (how many images are open)
        state++;
        if (state == 1)
        {
            o1 = btn;
            document.images["bb" + btn].src = imgs[tiles[btn]].src;

            score++;
            lblScore.nodeValue = score;
            if (tiles[btn] == visible)
            {
                sig[btn] = tiles[btn];
                tiles[btn] = IMG_CLOSED;
                state = 0;
                visible++;
                if (visible == imgCount)
                {
                    endtime = timeInMillis();
                    gameStarted = false;
                    edtScore.value = score;
                    edtDuration.value = endtime - starttime;
                    buildSignature();

                    // Now submit the form
                    submit();
                }
            }
            else
            {
                setTimeout("hideimage()", 500);
            }
        }
    }
}

/*
 * Displays the running clock at the HTML node with id "lblTime"
 */
function displayTime ()
{
    if (gameStarted)
    {
        var timeElapsed = (timeInMillis() - starttime) / 1000;
        var minutes = Math.floor(timeElapsed / 60);
        var seconds = Math.floor(timeElapsed % 60);

        lblTime.nodeValue = lPadZero(minutes, 2) + ":" + lPadZero(seconds, 2);
    }
    else
    {
        timeElapsed = (endtime - starttime);
        minutes = Math.floor(timeElapsed / 60000);
        seconds = Math.floor(timeElapsed / 1000) % 60;
        var millis = Math.floor(timeElapsed % 1000);
        lblTime.nodeValue = lPadZero(minutes, 2) + ":" + lPadZero(seconds, 2) + "." + lPadZero(millis, 3);
    }
}

function submit ()
{
    document.forms[0].submit();
}

