
jQuery(document).ready(function()
{

	if ($("#chess").length != 0)
	{
		var pgn_input = '';
		if ( jQuery("#pgn").html() != '' )
		{
		pgn_input = jQuery("#chess-pgn").chess({pgn : jQuery("#pgn").html()});
		}

// Register event handler DEFAULT
		jQuery('#controls-home').click(function()
		{
			var i = pgn_input.game.transitions.length;
			while (i >= 0)
			{
				i--;
				pgn_input.transitionBackward();
			}
			jQuery("#chess-annotation").text( pgn_input.annotation() );
			return false;
		});

		jQuery('#controls-rewind').click(function()
		{
			for (var i = 0; i < 10; i++)
			{
				pgn_input.transitionBackward();
			}
			jQuery("#chess-annotation").text( pgn_input.annotation() );
			return false;
		});

		jQuery('#controls-back').click(function()
		{
			pgn_input.transitionBackward();
			jQuery("#chess-annotation").text( pgn_input.annotation() );
			return false;
		});

		jQuery('#controls-next').click(function()
		{
			pgn_input.transitionForward();
			jQuery("#chess-annotation").text( pgn_input.annotation() );
			return false;
		});

		jQuery('#controls-forward').click(function()
		{
			for (var i = 10; i > 0; i--)
			{
				pgn_input.transitionForward();
			}
			jQuery("#chess-annotation").text( pgn_input.annotation() );
			return false;
		});

		jQuery('#controls-end').click(function()
		{
			var i = 0;
			while (i <= pgn_input.game.transitions.length)
			{
				pgn_input.transitionForward();
				i++;
			}
			jQuery("#chess-annotation").text( pgn_input.annotation() );
			return false;
		});

		jQuery('#controls-play').click(function()
		{
			var i = 0;
			while (i <= pgn_input.game.transitions.length)
			{
				pgn_input.transitionForward();
				i++;
			}
			jQuery("#chess-annotation").text( pgn_input.annotation() );
			return false;
		});

		jQuery('#controls-flip').click(function()
		{
			pgn_input.flipBoard();
			return false;
		});

		jQuery(".move").click(function()
		{
			var idMove = $(this).attr("id");
			pgn_input.transitionMoves(idMove);
			jQuery("#chess-annotation").text( pgn_input.annotation() );
			return false;
		});


// Register event handler SELECT
		jQuery(".selectPGN").change(function()
		{
			jQuery("#chess-pgn").html('');
			var gameNumber = 1;
			jQuery(".selectPGN option:selected").each(function()
			{
				gameNumber = $(this).val();
				jQuery("#pgn").html(pgngames.games[gameNumber - 1]);
				pgn_input = jQuery("#chess-pgn").chess({pgn : jQuery("#pgn").html()});

				jQuery("#controls-home").click(function()
				{
					var i = pgn_input.game.transitions.length;
					while (i >= 0)
					{
						i--;
						pgn_input.transitionBackward();
					}
					jQuery("#chess-annotation").text( pgn_input.annotation() );
					return false;
				});

				jQuery("#controls-rewind").click(function()
				{
					for (var i = 0; i < 10; i++)
					{
						pgn_input.transitionBackward();
					}
					jQuery("#chess-annotation").text( pgn_input.annotation() );
					return false;
				});

				jQuery("#controls-back").click(function()
				{
					pgn_input.transitionBackward();
					jQuery("#chess-annotation").text( pgn_input.annotation() );
					return false;
				});

				jQuery("#controls-next").click(function()
				{
					pgn_input.transitionForward();
					jQuery("#chess-annotation").text( pgn_input.annotation() );
					return false;
				});

				jQuery("#controls-forward").click(function()
				{
					for (var i = 10; i > 0; i--)
					{
						pgn_input.transitionForward();
					}
					jQuery("#chess-annotation").text( pgn_input.annotation() );
					return false;
				});

				jQuery("#controls-end").click(function()
				{
					var i = 0;
					while (i <= pgn_input.game.transitions.length)
					{
						pgn_input.transitionForward();
						i++;
					}
					jQuery("#chess-annotation").text( pgn_input.annotation() );
					return false;
				});

				jQuery("#controls-play").click(function()
				{
					var i = 0;
					while (i <= pgn_input.game.transitions.length)
					{
						pgn_input.transitionForward();
						i++;
					}
					jQuery("#chess-annotation").text( pgn_input.annotation() );
					return false;
				});

				jQuery("#controls-flip").click(function()
				{
					pgn_input.flipBoard();
					return false;
				});

				jQuery(".move").click(function()
				{
					var idMove = $(this).attr("id");
					pgn_input.transitionMoves(idMove);
					jQuery("#chess-annotation").text( pgn_input.annotation() );
					return false;
				});
			})
		});


// Register event handler STANDINGS
		jQuery(".standingsGame").click(function()
		{
			jQuery("#chess-pgn").html('');
			var reGameNumber = /[0-9]+/gmi;
			var gameNumber = 1;
			gameNumber = parseInt(reGameNumber.exec($(this).attr("id")));
			jQuery("#pgn").html(pgngames.games[gameNumber - 1]);
			pgn_input = jQuery("#chess-pgn").chess({pgn : jQuery("#pgn").html()});

			jQuery("#controls-home").click(function()
			{
				var i = pgn_input.game.transitions.length;
				while (i >= 0)
				{
					i--;
					pgn_input.transitionBackward();
				}
				jQuery("#chess-annotation").text( pgn_input.annotation() );
				return false;
			});

			jQuery("#controls-rewind").click(function()
			{
				for (var i = 0; i < 10; i++)
				{
					pgn_input.transitionBackward();
				}
				jQuery("#chess-annotation").text( pgn_input.annotation() );
				return false;
			});

			jQuery("#controls-back").click(function()
			{
				pgn_input.transitionBackward();
				jQuery("#chess-annotation").text( pgn_input.annotation() );
				return false;
			});

			jQuery("#controls-next").click(function()
			{
				pgn_input.transitionForward();
				jQuery("#chess-annotation").text( pgn_input.annotation() );
				return false;
			});

			jQuery("#controls-forward").click(function()
			{
				for (var i = 10; i > 0; i--)
				{
					pgn_input.transitionForward();
				}
				jQuery("#chess-annotation").text( pgn_input.annotation() );
				return false;
			});

			jQuery("#controls-end").click(function()
			{
				var i = 0;
				while (i <= pgn_input.game.transitions.length)
				{
					pgn_input.transitionForward();
					i++;
				}
				jQuery("#chess-annotation").text( pgn_input.annotation() );
				return false;
			});

			jQuery("#controls-play").click(function()
			{
				var i = 0;
				while (i <= pgn_input.game.transitions.length)
				{
					pgn_input.transitionForward();
					i++;
				}
				jQuery("#chess-annotation").text( pgn_input.annotation() );
				return false;
			});

			jQuery("#controls-flip").click(function()
			{
				pgn_input.flipBoard();
				return false;
			});

			jQuery(".move").click(function()
			{
				var idMove = $(this).attr("id");
				pgn_input.transitionMoves(idMove);
				jQuery("#chess-annotation").text( pgn_input.annotation() );
				return false;
			});
		});

	}

});
