/*
    ReactionJS - research tool to measure reaction times of people using a webbrowser
    Written by Hay Kranen <hay at bykr dot org> <http://www.haykranen.nl/projects/reaction>
    version 1.0
    Released under the GPLv3 License

    This package includes jQuery (released under MIT / GPL licenses) and
    SoundManager 2 (released under the BSD license)
*/

// This is simple wrapper for Firebug
if ((typeof console == "undefined") || (typeof console.log == "undefined")) {
    var log = function(){}
} else {
    var log = console.log;
}

// Check if PATH and data have been set already
if (typeof PATH == "undefined" || typeof data == "undefined") {
    var msg = "It seems you haven't made a config.js file in the js/ " +
                "directory. Please copy config-sample.js to config.js " +
                "and change the appropiate values";
    alert(msg);
}

var results = [];
var current = 0;
var itemShown = false;
var startTime, endTime;
// how many miliseconds is a second
// (1000 of course, but you can lower this for debug purposes)
var timeUnit = 1000;
var WIDTH, HEIGHT;
var beep; // Used to globally play the beep sound

// Initialize soundManager
soundManager.url = PATH + 'js/swf/';
log("soundManager url", soundManager.url);
soundManager.onload = function() {
    log("Soundmanager loaded");
    beep = soundManager.createSound({
        id  : "beep",
        url : PATH + 'sound/beep.mp3'
    });
}

function rand(max) {
    return Math.floor(Math.random() * max);
}

function start() {
    // Check if SoundManager is loaded
    if (typeof soundManager == "undefined") {
        alert("Sound is not available!");
    }

    if (current < data.length) {
        setTimeout(show, (data[current][1] * timeUnit));
    } else {
        // test over
        alert("The test is over!");
        showResults();
    }
}

function showResults() {
    for (var i = 0; i < results.length; i++) {
        var cell = i + 1;
        $("table .t" + cell).html(results[i]);
    }
    $("#results").fadeIn();
}

function show() {
    if (data[current][0] == 0) {
        $("#box").css('left', rand(WIDTH - 30) + 'px')
                 .css('top', rand(HEIGHT - 30) + 'px')
                 .show();
    } else {
        log('beep');
        // Play a sound
        beep.play();
    }

    itemShown = true;

    var time = new Date();
    startTime = time.getTime();
}

function hide() {
    $("#box").hide();
    itemShown = false;

    var time = new Date();
    endTime = time.getTime();
    var result = endTime - startTime;
    log('Result:' + result);
    results.push(result);

    // Next test
    current++;
    start();
}

$(document).ready(function() {
    // Set WIDTH and HEIGHT on the basis of the CSS values
    WIDTH  = $("#grid").css('width').slice(0, 3);
    HEIGHT = $("#grid").css('height').slice(0, 3);

    $("#btnGo").click(function() {
        $("#about").fadeOut();
        $(this).fadeOut(function() {
            start();
        });
    });

    $(window).keydown(function(event) {
        if (itemShown && event.keyCode == '32') {
            hide();
            return false;
        }
        return true;
    });
});