var inited = false;
var g_combination = "";
var g_ms = 500;
var g_pointer = 0;
var g_guess = "";
var g_clickout = 0;

function startCombination(combination)
{
    g_combination = combination;
    setTimeout("continueCombination()", g_ms * 2);
}

function continueCombination()
{
    if (++g_pointer <= g_combination.length)
    {
        var n = g_combination.charAt(g_pointer - 1);
        var elm = document.getElementById("square" + n);
        elm.style.backgroundColor = getBackgroundColor(n);
        setTimeout("breakContinue()", g_ms);
    }
    else
    {
        inited = true;
    }
}

function breakContinue()
{
    loliteSquares();
    setTimeout("continueCombination()", g_ms / 2);
}

function clickSquare(n)
{
    if (inited && g_clickout == 0)
    {
        var elm = document.getElementById("square" + n);
        elm.style.backgroundColor = getBackgroundColor(n);
        g_guess += "" + n + "";
        g_clickout = setTimeout("loliteSquares()", g_ms * 0.75);
    }
}

function overSquare(n)
{
}

function outSquare(n)
{
    if (inited)
    {
        var elm = document.getElementById("square" + n);
        elm.style.borderColor = "gray";
    }
}

function loliteSquares()
{
    var i;
    var elm;

    for (i = 1; i <= 4; i++)
    {
        elm = document.getElementById("square" + i);
        elm.style.backgroundColor = getBackgroundColorDark(i);
    }

    if (g_guess.length == g_combination.length)
    {
        elm = document.getElementById("guess");
        elm.value = g_guess;
        document.mainForm.submit();
    }

    g_clickout = 0;
}

function getBackgroundColor(n)
{
    var col = '';
    switch (n)
    {
        case "1": case 1: col = "rgb(255,81,81)"; break;
        case "2": case 2: col = "rgb(39,239,145)"; break;
        case "3": case 3: col = "rgb(91,133,230)"; break;
        case "4": case 4: col = "rgb(255,247,74)"; break;
    }

    return col;
}

function getBackgroundColorDark(n)
{
    var col = '';
    switch (n)
    {
        case "1": case 1: col = "rgb(115,0,0)"; break;
        case "2": case 2: col = "rgb(0,99,5)"; break;
        case "3": case 3: col = "rgb(0,0,90)"; break;
        case "4": case 4: col = "rgb(115,107,0)"; break;
    }

    return col;
}