// var color=null;
// var game_id=null;

function randOrd(){
return (Math.round(Math.random())-0.5); }

var CommonBoards = {
	bombs:"",
};

var Board ={
	clear:function(half)
	{
		if (!half)
			for(y=7; y<=10; y++)
			  for(x=1; x<=10; x++)
				   $(x+"_"+y).innerHTML="";		
	},
	randomize:function()
	{
		this.clear(false);
		bomb_code="rB";
		flag_code="rF";
		var pieces=["r1","r2","r3","r3","r4","r4","r4",
		"r5","r5","r5","r5","r6","r6","r6","r6",
		"r7","r7","r7","r7","r8","r8","r8","r8","r8",
		"r9","r9","r9","r9","r9","r9","r9","r9",
		"rB","rB","rB","rS"];
		if (with_nukes)
			pieces.push("rN");
		else
			pieces.push("rB");
		if (your_color=="blue")
		{
			bomb_code="bB";
			flag_code="bF";
			pieces=pieces.map(function(p) { return p.replace("r","b")} )
		}
		pieces=pieces.sort(randOrd);
//		y=Math.round(Math.random()*1)+1+7;
		y=10;
		x=Math.round(Math.random()*9)+1;
		id=x+'_'+y;
		piece=new Piece(flag_code);
		$(id).innerHTML=piece.html();
		if (x+1<11)
			$((x+1)+"_"+(y)).innerHTML=new Piece(bomb_code).html();
		if (x-1>0)
			$((x-1)+"_"+(y)).innerHTML=new Piece(bomb_code).html();
//		$((x)+"_"+(y+1)).innerHTML=new Piece("rB").html();
		$((x)+"_"+(y-1)).innerHTML=new Piece(bomb_code).html();
		offset=0;
		for(y=7; y<=10; y++)
		{
			for(x=1; x<=10; x++)
			{
				piece=this.piece_at(x,y);
				if (piece==false)
				{
					code=pieces[offset];
					$(x+"_"+y).innerHTML=new Piece(code).html();
					piece=Board.piece_at(x,y);
					offset+=1;
				}
				piece.setAttribute("x",x);
				piece.setAttribute("y",y);
			}
		}
		$('piece_selection').hide();
		Board.movable_pieces();
	},
	parse:function(str)
	{
		board=[];
		for(y=1; y<=10; y++)
		{
			board[y-1]=[];
			for(x=1; x<=10; x++)
			{
				index=((y-1)*10+x-1)*2;
				code=str.substring(index,index+2)
				board[y-1][x-1]=(code=='  ' ? null : code);
			}
		}
		return board;
	},
	select_color:function(color)
	{
		if (color=="Red")
		{
			board=
			"bUbUbUbUbUbUbUbUbUbU"+
			"bUbUbUbUbUbUbUbUbUbU"+
			"bUbUbUbUbUbUbUbUbUbU"+	
			"bUbUbUbUbUbUbUbUbUbU"+
			"                    "+
			"                    "+
			"rUrUrUrUrUrUrUrUrUrU"+
			"rUrUrUrUrUrUrUrUrUrU"+
			"rUrUrUrUrUrUrUrUrUrU"+
			"rUrUrUrUrUrUrUrUrUrU";
			game_data={last_move:null};
			game_data.board=Board.parse(board);
			this.render();
		}
		else // blue
		{
			board=
			"rUrUrUrUrUrUrUrUrUrU"+
			"rUrUrUrUrUrUrUrUrUrU"+
			"rUrUrUrUrUrUrUrUrUrU"+
			"rUrUrUrUrUrUrUrUrUrU"+
			"                    "+
			"                    "+
			"bUbUbUbUbUbUbUbUbUbU"+
			"bUbUbUbUbUbUbUbUbUbU"+
			"bUbUbUbUbUbUbUbUbUbU"+	
			"bUbUbUbUbUbUbUbUbUbU";
			game_data={last_move:null};
			game_data.board=Board.parse(board);
			this.render();
		}
		
	},
	display_error:function(msg)
	{
		$('error_message').innerHTML=msg;
		new Effect.Appear("board_status", {duration:0.25});
		window.setTimeout(this.fade_error, 3000);
	},
	fade_error:function(msg)
	{
		new Effect.Fade("board_status", {duration:0.5});
	},
	freeze_pieces:function()
	{
		Draggables.drags.each(function(d){
			Draggables.unregister(d);
		})
	},
	movable_pieces:function()
	{
		$$('#map .mypiece').each(function (p){
			new Draggable(p, {revert:true});
		});
	},
		render: function()
		{
			if (last_move=game_data["last_move"])
			{
				if (your_color=="blue")
				{
					last_move["from"][0]=11-last_move["from"][0];
					last_move["from"][1]=11-last_move["from"][1];
					last_move["to"][0]=11-last_move["to"][0];
					last_move["to"][1]=11-last_move["to"][1];
				}
			}

	//		alert(board_data);
			for(y=1; y<=10; y++)
			{
				for(x=1; x<=10; x++)
				{
					code=game_data["board"][y-1][x-1];
					square=$(x+'_'+y);
					square.innerHTML="";
					if (last_move=game_data["last_move"])
					{
						// need to reverse the coordinates as the server gives us TRUE coords
						
						if (x==last_move["from"][0] && y==last_move["from"][1])
						{
						diff_x=last_move["to"][0]-last_move["from"][0];
						diff_y=last_move["to"][1]-last_move["from"][1];
						if (diff_y>0)
							square.innerHTML="<image src='/images/icons/32x32/arrow_down_32.gif' id='arrow' />";
						else if (diff_y<0)
							square.innerHTML="<image src='/images/icons/32x32/arrow_up_32.gif' id='arrow' />";						
						else if (diff_x>0)
							square.innerHTML="<image src='/images/icons/32x32/arrow_r_32.gif' id='arrow' />";
						else if (diff_x<0)
							square.innerHTML="<image src='/images/icons/32x32/arrow_l_32.gif' id='arrow' />";
						}

					}
					if (code=="bu")
					{
						square.innerHTML="<image src='/images/pieces/bunknown.png' class='piece' />";
					}
					else if (code=="ru")
					{
						square.innerHTML="<image src='/images/pieces/runknown.png' class='piece' />";					
					}
					else if (code!=null)
					{
						piece=new Piece(code);
						square.innerHTML=piece.html({from:x+","+y});
					}
				}
			}

			if ($("arrow"))
				new Effect.Pulsate("arrow", {duration:3, pulses:3});
		},
	check: function()
	{
//		return this.submit();
		pp=this.pieces_placed();
		if (pp < 40)
		{
			alert("Please put all your pieces on the board. You have only placed " + pp + " pieces.");
			return false;
		}
		else
		{
			this.submit();
		}
	},
	submit: function()
	{
//		alert("submitting board");
		params="?placement=save";
		for( y=7; y<=10; y++)
			for( x=1; x<=10; x++)
			{
				piece=this.piece_at(x,y).getAttribute("piece");
				if (piece==false)
					params+="&board["+x+"_"+y+"]=";
				else
					params+="&board["+x+"_"+y+"]=" + piece;
			}
		new Ajax.Request('/games/' + game_id + '/setup_board', {asynchronous:true, evalScripts:true, method:'put', parameters:params});
//		alert(params);
	},
	pieces_placed: function()
	{
		i=0;
		for( y=7; y<=10; y++)
			for( x=1; x<=10; x++)
			{
				if (this.piece_at(x,y))
					i+=1;
			}
		return i;
	},
	piece_at: function(x,y)
	{
		pieces=$$("#" +x+"_"+y + " .piece");
		if (pieces.size()==0)
			return false;
		else
			return pieces.first();
	}
}