function fs_TicTacToe(){
    this.colors = ["white","white"];
    this.images = ["/images/0.gif","/images/x.gif"];
    this.spacing = 50;
    this.cellsize = 40;
}

fs_TicTacToe.prototype.init = function(x, y, w , which, id, watching, started, can_move, player_id){
    this.taken=0;
    this.width = x;
    this.height = y;
    this.length = x*y;
    this.win = w;
    this.v = which;
    this.can_move = can_move;	
    this.id = id;
    this.watching = watching;
    this.started = started;
    this.player_id = player_id;
    this.create();
    this.get_move();
}

fs_TicTacToe.prototype.create_cell = function(x, y, bg, border, delta){
    var sq = document.createElement("DIV");
    sq.style.position = "absolute";
    sq.className = "horizontal_table_categories";
    sq.style.left = x*this.spacing+delta + "px";
    sq.style.top = y*this.spacing+delta + "px";
    sq.style.width = this.cellsize + "px";
    sq.style.height = this.cellsize + "px";    
    sq.style.border = border;

    return sq;
}

fs_TicTacToe.prototype.draw_cell = function(x,y){
    var sq = document.getElementById("tic"+x+y);
    if(!sq){
        sq = this.create_cell(x,y,"blue", "solid 1px black",0);
        sq.id = "tic" + x + y;
        sq.x=x;
        sq.y=y;
        var sqh = this.create_cell(x,y,"#666666", "none", 4);
        this.holder.appendChild(sqh);
        this.holder.appendChild(sq);
        if(this.watching==false)//add the listener if playing
            fs_addListener(sq, "onclick", fs_tictactoe.clicked, true);
    }

}
fs_TicTacToe.prototype.cell = function(x,y){
    return document.getElementById("tic" + x + y);
}




fs_TicTacToe.prototype.send_move = function(x, y){
    new Ajax.Request('/games/ttt_send_move?x=' + escape(x) + '&y=' + escape(y) + '&id='+escape(fs_tictactoe.id), {asynchronous:true, evalScripts:true,
        onComplete:function(request){	
            eval(request.responseText);
        }}); 
}

fs_TicTacToe.prototype.get_move = function(){
    
    if(jugg_server && jugg_server.is_connected == true)
        return;
    
    if(fs_tictactoe.ended==true)
        return;
    setTimeout(fs_tictactoe.get_move,10000);

    if(!fs_tictactoe.started){
        new Ajax.Request( '/games/ttt_has_started?id=' + fs_tictactoe.id, {asynchronous:true, evalScripts:true,
            onComplete:function(request){	
                eval(request.responseText);
            }}); 
        return false;	
    }

    if(fs_tictactoe.can_move){
        return	false;
    }
    new Ajax.Request('/games/ttt_get_move?id=' + fs_tictactoe.id, {asynchronous:true, evalScripts:true,
        onComplete:function(request){	
            eval(request.responseText);
        }}); 
	
}

fs_TicTacToe.prototype.clicked = function(ev){
    if(!fs_tictactoe.started){
        alert('The game has not begun yet');
        return false;
    }
    var sq = fs_getTargetElement(fs_getEvent(ev))	
    if(fs_tictactoe.can_take_cell(sq, fs_tictactoe.v)){
        fs_tictactoe.send_move(sq.x,sq.y);		
    }
}


fs_TicTacToe.prototype.can_take_cell = function(cell,v){
    if(!this.can_move){
        alert("It is not your turn yet!");
        return false;
    }
    if(this.is_taken(cell.x,cell.y)){
        alert("That cell is already taken!");
        return false;
    }
    return true;
}
fs_TicTacToe.prototype.take_cell = function(cell, v){
    this.taken++;
    this.values[this.width*cell.y + cell.x] = v;
    cell.style.background = this.colors[v?1:0];
    cell.innerHTML="<img src='" + this.images[v?1:0] + "' align='middle' style='margin-left:3px;margin-top:3px;'/>";
    return true;
}

fs_TicTacToe.prototype.is_taken = function(x,y){
    if(this.values[this.width*y + x] != -1)
        return true;
    return false;
}

fs_TicTacToe.prototype.check_game_status = function(x,y,v){
    var cnt = 1;
    v = v ? 1 : 0;


    for(var i=x-1;i>=0&&this.values[this.width*y + i]==v && cnt<this.win;i--)
        cnt++;
    for(var i=x+1;i<this.width&&this.values[this.width*y + i]==v && cnt<this.win;i++)
        cnt++;
    if(cnt==this.win)
        this.end_game(this.v == v)
    cnt=1;
    for(var i=y-1;i>=0&&this.values[this.width*i + x]==v && cnt<this.win;i--)
        cnt++;
    for(var i=y+1;i<this.height&&this.values[this.width*i + x]==v && cnt<this.win;i++)
        cnt++;
    if(cnt==this.win)
        this.end_game(this.v == v);

    cnt=1;

    for(var i=y-1,j=x-1;j>=0 && i>=0 && this.values[this.width*i + j]==v && cnt<this.win;i--,j--)
        cnt++;
    for(var i=y+1,j=x+1;j<this.width && i<this.height && this.values[this.width*i + j]==v && cnt<this.win;i++,j++)
        cnt++;
    if(cnt==this.win)
        this.end_game(this.v == v);
    cnt=1;

    for(var i=y-1,j=x+1;j<this.width && i>=0 && this.values[this.width*i + j]==v && cnt<this.win;i--,j++)
        cnt++;

    for(var i=y+1,j=x-1;j>=0 && i<this.height && this.values[this.width*i + j]==v && cnt<this.win;i++,j--){
        cnt++;
    }
	
    if(cnt==this.win)
        this.end_game(this.v == v);

    if(this.taken==this.width*this.height)
        this.end_game();
}

fs_TicTacToe.prototype.end_game = function(status){
    this.ended = true;
    for(var y=0;y<this.height;y++)
        for(var x=0;x<this.width;x++){
            fs_removeListener(document.getElementById("tic" + x + y), "onclick", fs_tictactoe.clicked);
        }			
    if(status==true)
        alert('You have won the game!');
    else
        if(status==false)
            alert('You have lost the game!');				
    else
        if(this.taken==this.width*this.height)
            alert('The game ended in a draw!');
		
    new Ajax.Request('/games/ttt_end_game?id=' + escape(this.id)+'&result=' + status, {asynchronous:true, evalScripts:true,
        onComplete:function(request){	
        }}); 
		
    return false;
			
}

fs_TicTacToe.prototype.create = function(){
    this.holder = document.createElement("DIV");
    this.holder.id = "tic_tag_g" + this.id;

    this.holder.style.position="relative";
    this.holder.style.left="10px";
    if(fs_browserType==fs_InternetExplorer && navigator.userAgent.indexOf("7.")==-1)
        this.holder.style.left="-120px";
		
	
    this.holder.style.top="10px";	

    this.squares = new Array();
    this.values = new Array(this.width*this.height);
    for(var y=0;y<this.height;y++)
        for(var x=0;x<this.width;x++){
            this.values[this.width*y + x] = -1;
            this.draw_cell(x,y);
        }			
    document.getElementById("ttt_game_area").appendChild(this.holder);
}

fs_TicTacToe.prototype.create_game = function(){
    new Ajax.Request( '/games/ttt_create_game?name=' + escape(document.getElementById('ttt_new_game').value)+'&size=' + document.getElementById('ttt_table_size').value, {asynchronous:true, evalScripts:true}); 
}


fs_TicTacToe.prototype.reconnect = function(game_id){
    if(!jugg_server || !jugg_server.is_connected)
        return;
    var game = "tic_tac_g" + game_id;
    if(jugg_server.options.channels.indexOf(game) == -1){
        jugg_server.options.channels.push(game) 
        jugg_server.disconnect();
        jugg_server.connect();
    }

}

fs_TicTacToe.prototype.move = function(x, y, current_move, player_id){    
    fs_tictactoe.take_cell(document.getElementById('tic' + x + y),current_move);
    fs_tictactoe.can_move = fs_tictactoe.player_id != player_id;
    fs_tictactoe.check_game_status(x,y,current_move);
}


var fs_tictactoe = new fs_TicTacToe();