function chessics()
{
	this.cur_move_node = false;
	this.animation = false;

	this.repositionChessBoard = function()
	{
		var cur_pos_x = Math.round(dd.getWndW() - dd.elements.ci_board.defw - 5);
		if (cur_pos_x > 670) cur_pos_x = 670;

		//var cur_pos_x = 7;
		//var cur_pos_y = Math.round(dd.getScrollY() + dd.getWndH() - dd.elements.ci_board.defh - 7);
		var cur_pos_y = dd.getScrollY() + 190;
		if (cur_pos_x != this.board_pos_x || cur_pos_y != this.board_pos_y) {
			this.board_pos_x = cur_pos_x;
			this.board_pos_y = cur_pos_y;
			dd.elements.ci_board.moveTo(cur_pos_x, cur_pos_y);
		}

		window.setTimeout('ci.repositionChessBoard()', 200);
	}

	this.db = function(node)
	{
		if (this.cur_move_node)
		{
			this.cur_move_node.setStyle({
				backgroundColor: 'transparent'
			});
		}

		this.cur_move_node = $(node);
		this.cur_move_node.setStyle({
			backgroundColor: 'yellow'
		});

		if (!dd.elements.ci_board.visible)
		{
			this.repositionChessBoard();
			dd.elements.ci_board.show();
		}

		ac.displayFEN(node.readAttribute('title'));

		var comment_node;
		if ((comment_node = $(node).next()) && (comment_node.readAttribute('class') == 'txt'))
		{
			this.showComment(comment_node.innerHTML);
		}
		else
		{
			this.showComment('');
		}
	}

	this.showComment = function(txt)
	{
		$('ci_move_comment').innerHTML = txt != '' ? '<i>'+ txt +'</i>' : '';
	}

	this.getMoveNode = function(direction)
	{
		var node;
		if (node = this.cur_move_node)
		{
			var move_node;
			if (move_node = (direction == -1 ? $(node).previous('a.mv') : $(node).next('a.mv')))
			{
				return move_node;
			}
		}

		return false;
	}

	this.displayNextMove = function(animate)
	{
		var node;
		if (node = this.getMoveNode(1))
		{
			this.db(node);
			if (animate) this.animation = this.animation = window.setTimeout('ci.displayNextMove(true)', 1000);
		}
	}

	this.displayPreviousMove = function()
	{
		var node;
		if (node = this.getMoveNode(-1))
		{
			this.db(node);
		}
	}

	this.displayFirstMove = function()
	{
		var node, previous_node;
		if (node = this.cur_move_node)
		{
			while(previous_node = $(node).previous('a.mv'))
			{
				node = previous_node;
			}

			this.db(node);
		}
	}

	this.displayLastMove = function()
	{
		var node, next_node;
		if (node = this.cur_move_node)
		{
			while(next_node = $(node).next('a.mv'))
			{
				node = next_node;
			}

			this.db(node);
		}
	}

	this.animationToggle = function()
	{
		if (this.animation)
		{
			window.clearTimeout(this.animation);
			this.animation = false;
		}
		else
		{
			this.displayNextMove(true);
		}
	}

	this.close = function()
	{
		if (this.animation)
		{
			window.clearTimeout(this.animation);
			this.animation = false;
		}		
		
		dd.elements.ci_board.hide();
	}

	this.showPGN = function(node)
	{
		var pgn = $(node).next('div.ci_pgn_text');

		if (node.innerHTML.search(/anzeigen/) != -1)
		{
			pgn.show();
			node.innerHTML = '&laquo; PGN verstecken';
		}
		else
		{
			pgn.hide();
			node.innerHTML = '&raquo; PGN anzeigen';
		}
	}
}

ci = new chessics();