
/* Dependem das constantes em shared.php e nomes de classes em style.css
*/

var GAME_MODE_WAIT_JOIN	= 0;
var GAME_MODE_DEFINING	= 1;
var GAME_MODE_BOMBING	= 2;
var GAME_MODE_FINISH	= 3;

var CELL_WATER		= 'w';
var CELL_BOAT		= 'b';
var CELL_BOMB_WATER	= 'W';
var CELL_BOMB_BOAT	= 'B';

var turn = 0;		// whose turn is it
var my_turn = 0;	// which player am I


// ============================================================================
// #### Funcoes re recepcao de respostas AJAX #################################
// ============================================================================

var ajax_action = GAME_MODE_WAIT_JOIN;
// pode ser GAME_MODE_WAIT_JOIN, GAME_MODE_DEFINING ou GAME_MODE_BOMBING ('a espera do outro jogador)

function ajax_received( text )
{
	var json = eval( text );
	switch( ajax_action )
		{
		case 0:	if( json )
				document.location = "define.php";
			else
				setTimeout( "json_check_join()", 1000 );
			break;
		case 1:	if( json )
				document.location = "bomb.php";
			else
				setTimeout( "json_check_defined()", 2000 );
			break;
		case 2:	if( json[0] == "played" )
				{
				/* too ugly: document.location = "bomb.php"; */
				turn ^= 1;
				document.getElementById("bomb-other").style.display = "none";
				document.getElementById("bomb-me").style.display = "";
				var cell = board1[ json[1] ][ json[2] ];
				switch( cell )
					{
					case 'b':	cell = 'B';  break;
					case 'w':	cell = 'W';  break;
					// 'B' and 'W' don't change
					}
				board1[ json[1] ][ json[2] ] = cell;
				document.getElementById("board1_"+json[1]+"_"+json[2]).className = select_cell_class( cell, true );
				}
			else if( json[0] == "finish" )
				document.location = "finish.php";
			else
				setTimeout( "json_check_bomb()", 1000 );
			break;
		}
}


function json_check_join()
{
	ajax_action = GAME_MODE_WAIT_JOIN;
	loadXMLDoc( "json.join.php" );
}


function json_check_defined()
{
	ajax_action = GAME_MODE_DEFINING;
	loadXMLDoc( "json.defined.php" );
}


function json_check_bomb( x, y )
{
	ajax_action = GAME_MODE_BOMBING;
	if( x !== null  &&  y !== null )
		loadXMLDoc( "json.bomb.php?x="+escape(x)+"&y="+escape(y) );
	else
		loadXMLDoc( "json.bomb.php" );
}


// ============================================================================
// #### Funcoes padrao para tratar de pedidos AJAX ############################
// ============================================================================

var req = false;

function processReqChange()
{
	// only if req shows "loaded"
	if( req  &&  req.readyState == 4 )
		{
		// only if "OK"
		if( req.status == 200  &&  typeof(req.responseText) == "string" )
			{
			// ...processing statements go here...
			ajax_received( req.responseText );
			}
		else
			{
			alert(	"There was a problem retrieving the XML data:\n" +
				req.statusText );
			}
		}
}

function loadXMLDoc( url )
{
	req = false;

	// branch for native XMLHttpRequest object
	if( window.XMLHttpRequest  &&  !(window.ActiveXObject) )
		{
		try	{
			req = new XMLHttpRequest();
			}
		catch(e){
			req = false;
			}
		}
	// branch for IE/Windows ActiveX version
	else if( window.ActiveXObject )
		{
		try	{
			req = new ActiveXObject( "Msxml2.XMLHTTP" );
			}
		catch(e){
			try	{
				req = new ActiveXObject( "Microsoft.XMLHTTP" );
				}
			catch(e){
				req = false;
				}
			}
		}

	if( req )
		{
		req.onreadystatechange = processReqChange;
		req.open( "GET", url, true );
		req.send( "" );
		}
}


// ============================================================================
// #### Funcoes auxiliares ####################################################
// ============================================================================

function select_cell_class( cell, with_boats )
{
	switch( cell )
		{
		case 'b':	return with_boats ? "boat" : "water";
		case 'w':	return "water";
		case 'B':	return "bomb-boat";
		case 'W':	return "bomb-water";
		}
	return "water";  // reasonable default, but means error, actually...
}


function click_define( td, board, x, y )
{
	cell = board[x][y];

	switch( cell )
		{
		case 'b':	cell = 'w';  break;
		case 'w':	cell = 'b';  break;
		case 'B':	cell = 'w';  break;
		case 'W':	cell = 'b';  break;
		}
	board[x][y] = cell;
	td.className = select_cell_class( cell, true );
}


function click_bomb( td, board, x, y )
{
	var cell = board[x][y];

	if( turn != my_turn )
		return;  // not my turn
	switch( cell )
		{
		case 'b':	cell = 'B';  break;
		case 'w':	cell = 'W';  break;
		// 'B' and 'W' don't change
		}
	board[x][y] = cell;
	if( cell == 'b' )
		cell = 'w';  // hide boats!
	td.className = select_cell_class( cell, false );
	turn ^= 1;
	document.getElementById("bomb-me").style.display = "none";
	document.getElementById("bomb-other").style.display = "";
	json_check_bomb( x, y );
}


function submit_board( f, board )
{
	var boats, board_str = "";

	for( var x in board )
		{
		if( board_str.length > 0 )
			board_str += ";";
		board_str += board[x].join( "," );
		}
	boats = board_str.replace( /[bB]+/, "" );
/*
	if( boats.length < 25 )
		{
		alert( "Não existem barcos suficientes!" );
		return false;
		}
	if( boats.length > 25 )
		{
		alert( "Não existem barcos a mais!" );
		return false;
		}
*/
	f.board.value = board_str;
	return true;
}

