﻿/*
  Copyright (c) 2003, 2008 Nesterovsky bros.  All rights reserved.
  
  Board page scripts.

  You must not remove this notice, or any other, from this software.
*/    

function Board()
{
  this.tournament = new Object;
  this.tournament.title = null;
}

Board.prototype.tournament = null;
Board.prototype.annotation = null;

Board.prototype.load = function()
{
  var allGamesLink = document.getElementById("allGamesLink");
  var gameList = document.getElementById("gameList");

  if (gameList != null)
  {
    while(true)
    {
      var child = gameList.firstChild;
      
      if (child == null)
      {
        break;
      }
      
      gameList.removeChild(child);
    }
  }

  var tournament = this.tournament;
  var parameters = Util.getQueryParameters();
  var pgnParameter = parameters["pgn"];
  var roundParameter = parameters["round"];
  
  if (!pgnParameter)
  {
    return;
  }
  
  var url = "tournaments/" + pgnParameter;

  if (allGamesLink != null)
  {
    allGamesLink.href = url;
  }

  var self = this;
  var roundGroups = new Object;
  var selectedRoundFound = false;
  
  function onLoadGame(pgnDocument, game)
  {
    var gameIndex = pgnDocument.games.length - 1;
    var anEvent = game.tags["Event"];
    var round = game.tags["Round"];
    var white = game.tags["White"];
    var black = game.tags["Black"];
    
    if (tournament.title == null)
    {
      if (anEvent)
      {
        tournament.title = anEvent;
        document.title = "Chess games - " + anEvent;
      }
    }
    
    var optGroup = roundGroups[round];
    
    if (!optGroup)
    {
      optGroup = document.createElement("optgroup");
      roundGroups[round] = optGroup;
      
      optGroup.label = "Round " + round;
      
      if (gameList != null)
      {
        gameList.appendChild(optGroup);
      }
    }
    
    var checkRound = optGroup.firstChild == null;
    var option = document.createElement("option");

    option.innerText = white + " - " + black;
    option.value = gameIndex;
    optGroup.appendChild(option);
    
    if (!selectedRoundFound && checkRound && 
      (!roundParameter || (roundParameter == round)))
    {
      selectedRoundFound = true;
      option.selected = true;
      self.select();
    }  
    
    return true;
  }
  
  function onLoad(request, succeed)
  {
    if (succeed)
    {
      var pgnDocument = new PgnDocument;
      
      self.annotation.setDocument(pgnDocument);
      pgnDocument.load(request.responseText, onLoadGame);
    }
  }

  Util.sendRequest(url, onLoad);
}

Board.prototype.select = function()
{
  var gameList = document.getElementById("gameList");
  var board = document.getElementById("board");

  if ((gameList != null) && (board != null))
  {
    this.annotation.setGameIndex(gameList.value);
  }
}

Board.prototype.start = function()
{
  this.annotation.start();
}

Board.prototype.back = function()
{
  this.annotation.back();
}

Board.prototype.forward = function()
{
  this.annotation.forward();
}

Board.prototype.end = function()
{
  this.annotation.end();
}

Board.prototype.keyDown = function(eventObj)
{
  switch(eventObj.keyCode)
  {
    case 39: // Right
    {
      if (eventObj.ctrlKey || eventObj.altKey)
      {
        this.end();
      }
      else
      {
        this.forward();
      }
    
      break;
    }
    case 37: // Left
    {
      if (eventObj.ctrlKey || eventObj.altKey)
      {
        this.start();
      }
      else
      {
        this.back();
      }
    
      break;
    }
    case 36: // Home
    {
      this.start();
      
      break;
    }
    case 35: // End
    {
      this.end();
      
      break;
    }
    case 38: // Up
    {
      // TODO: open variation.
      break;
    }
    case 40: // Down
    {
      // TODO: close variation.
      break;
    }
  }
}

Board.prototype.init = function()
{
  var boardElement = document.getElementById("board");
  var annotationElement = document.getElementById("annotation");

  this.annotation = new Annotation(boardElement, annotationElement)
  this.load();
}

var board = new Board;
