function fs_MemoryGame(){
}

fs_MemoryGame.prototype.restart = function(){
    this.found = 0;
    this.started = true;
    
    this.first_number = -1;
    this.second_number = -1;
    this.numbers = new Array(this.width * this.height);
    for(var i = 0 ; i < this.width * this.height ; i++)
        this.numbers[i] = 0;
    var complete = 0;
    var tmpn = new Array((this.width * this.height) / 2);
    for(var i = 0 ; i < (this.width * this.height)/2;){
        var n = 100 + parseInt(Math.random()*899) ;
        var j = 0;
        for(;j < i && tmpn[j] != n;j++);
        if(j == i){
            tmpn[i++] = n;
        }
    }


    for(var i = 0; i < (this.width * this.height)/2;i++){
        var n1,n2;
        do
        n1 = parseInt(1000*Math.random())%(this.width * this.height);
        while(this.numbers[n1]!= 0);
        do
        n2 = parseInt(1000*Math.random())%(this.width * this.height);
        while(this.numbers[n2]!= 0 || n2 == n1);

        this.numbers[n1] = tmpn[i];
        this.numbers[n2] = tmpn[i];

    }
    ret = "<table border='1' style='padding:2px' class='horizontal_table round shadow'>";
    for(var y = 0;y < this.height;y++){
        ret+= "<tr>";
        for(var x = 0;x < this.width;x++)
            ret+= ("<td class='ht_even round '><span style = ';cursor:hand;margin:2px;display:block' onclick='fs_memory_game.show(this)' id=" + (y * this.width + x) + ">???</span></td>");
        ret+= "</tr>";
    }
    ret+= "</table>";
    this.handler.innerHTML = ret;
}

fs_MemoryGame.prototype.init = function(width,height,parent){
    this.width = width;
    this.height = height;
    this.handler = document.createElement("DIV");
    document.getElementById(parent).appendChild(this.handler);
    this.restart();
}
fs_MemoryGame.prototype.show = function(obj){
    
    if(this.started != true)
        return alert(this.game_ended);

    
    if(obj.id == this.first_number || obj.id == this.second_number)
        return;

    if(this.second_number!= -1){
        document.getElementById(this.second_number).innerHTML = "???";
    }
    if(this.first_number!= -1)
        this.second_number = this.first_number;
    this.first_number = obj.id;

    if(this.numbers[this.first_number] == this.numbers[this.second_number]){
        this.found++;
        var fn = document.getElementById(this.first_number);
        var sn = document.getElementById(this.second_number);
        fn.style.backgroundColor = 'red';
        sn.style.backgroundColor = 'red';
        fn.onClick = function(){}
        sn.onClick = function(){}
        this.first_number = -1;
        this.second_number = -1;
    }
    obj.innerHTML = this.numbers[obj.id];
    if(this.found>= (this.width*this.height)/2){
        new Ajax.Request('/games/memory_end_game/' + this.width*this.height, {
            asynchronous:true,
            evalScripts:true
        });
        this.started = false;
        if(confirm(this.confirm_restart))
            new Ajax.Request('/games/restart_memory', {
                asynchronous:true,
                evalScripts:true
            });
    }
}

fs_memory_game = new fs_MemoryGame();
fs_memory_game.confirm_restart = window.memory_messages ? window.memory_messages.confirm_restart : "Congratulations, do you want to play again?";
fs_memory_game.game_ended = window.memory_messages ? window.memory_messages.game_ended : 'The game has ended, you must start a new one!';