// global variables
var g_activeRowIndex = 0;
var g_activeRow = null;

function showGame(row)
{
    if (row.rowIndex == g_activeRowIndex)
        return;
        
    // reset the colors for the old active row    
    if (g_activeRowIndex != 0)
    {
        g_activeRow.style.backgroundColor='#B0C4DE';    
        g_activeRow.style.color='#483D8B';    
    }
     
    // save the new row to our global variables 
    g_activeRowIndex = row.rowIndex;
    g_activeRow = row;
    
    // load the selected game into the viewer
    ChessBoard.loadGame(row.rowIndex - 1);
    
    // reverse the colors to indicate that this game is being displayed
    row.style.backgroundColor = '#483D8B';
    row.style.color = '#B0C4DE';
    
    // set the cursor style to an arrow
    row.style.cursor = "default";
}

function showCrossTableGame(index)
{
    // load the selected game into the viewer
    ChessBoard.loadCrossTableGame(index);
}

function StatSearch()
{
    // serach for win/loss/draw info on game positions for the current game
    ChessBoard.searchForStats();    
}

function rollOver(row)
{
    // don't change the colors of the row representing the active game
    if (row.rowIndex == g_activeRowIndex)
        return;
        
    // hightlite this row    
    row.style.backgroundColor = '#E5E5E5';
    row.style.cursor = "hand";    
}

function rollOut(row)
{
    // don't change the colors of the row representing the active game
    if (row.rowIndex == g_activeRowIndex)
        return;
        
    // turn off the highlite for this row    
    row.style.backgroundColor = '#B0C4DE';
    row.style.cursor = "default";    
}
  
function search()
{
    // put together a search criteria string
    var strSearch = "";
    strSearch += AddSearchField("search",       GameSearch.MaxGames.value);
    strSearch += AddSearchField("PlayerOne",    GameSearch.PlayerOne.value);
    strSearch += AddSearchField("PlayerTwo",    GameSearch.PlayerTwo.value);
    strSearch += AddSearchField("Event",        GameSearch.Event.value);
    strSearch += AddSearchField("Eco",          GameSearch.Eco.value);
    strSearch += AddSearchField("MinRating",    GameSearch.MinRating.value);
    strSearch += AddSearchField("MaxRating",    GameSearch.MaxRating.value);
    strSearch += AddSearchField("EarliestYear", GameSearch.EarliestYear.value);
    strSearch += AddSearchField("LatestYear",   GameSearch.LatestYear.value);
    strSearch += AddSearchField("Result",       GameSearch.Result.value);    
    if (GameSearch.OrderBy.value == "MostRecent")
        strSearch += AddSearchField("OrderBy", GameSearch.OrderBy.value);    
    
    // remove the extra field separator '|' - at the end of the string
    if (strSearch.length > 0)
        strSearch = strSearch.substr(0, strSearch.length - 1);
  
    // call the search routine
    ShowSearchStatus("Searching for games...");
    var strResult = ChessBoard.searchForGames(strSearch);    
       
    // display our results
    //document.getElementById('Games').innerHTML = strResult;
    //document.getElementById('SearchGames').tabber.tabShow(1);
}

function positionOnlySearch(positionCode)
{
    var strSearch = "";
 
    // get the MaxGames and the OrderBy values currently in the search tab
    strSearch += AddSearchField("search", GameSearch.MaxGames.value);
    if (GameSearch.OrderBy.value == "MostRecent")
        strSearch += AddSearchField("OrderBy", GameSearch.OrderBy.value);
        
    // the positionCode we are searching for    
    strSearch += "position|" + positionCode;       

    // do the search
    ShowSearchStatus("Searching for games...");
    var strResult = ChessBoard.searchForGames(strSearch);
}

function checkUrlSearch()
{
    var strSearch = getURLParam('search');
    if (strSearch != "")
    {
      ChessBoard.searchForGames(strSearch);
      document.getElementById('Games').innerHTML = "<h2>Games</h2><p>Searching...</p>";
      document.getElementById('SearchGames').tabber.tabShow(1);
    }
}

function searchCrossTable()
{
    var strEvent = "ch-USA GpB";
    var strSite  = "San Diego";
    var strDate  = "2006-03-07";
    
    ChessBoard.searchForCrossTable(strEvent + "|" + strSite + "|" + strDate);
}

function AddSearchField(strName, strValue)
{
    if (strValue == "any" || strValue == "")
        return "";
        
    // convert PlayerOne and PlayerTwo to WhitePlayer and/or BlackPlayer here
    // handle PlayerOne first
    if (strName == "PlayerOne")
    {
        if (GameSearch.PlayerOneColor.value == "white")
            strName = "WhitePlayer";
        else if (GameSearch.PlayerOneColor.value == "black")
            strName = "BlackPlayer";
        else 
            strName = "(WhitePlayer";
    }     
    else if (strName == "PlayerTwo")
    {
        // we must have a PlayerOne before we have a PlayerTwo
        if (GameSearch.PlayerOne == "any" || GameSearch.PlayerOne == "")
            return;
            
        if (GameSearch.PlayerOneColor.value == "white")
            strName = "BlackPlayer";
        else if (GameSearch.PlayerOneColor.value == "black")
            strName = "WhitePlayer";
        else 
            strName = "(WhitePlayer";
    }
        
    return strName + "|" + strValue + "|";           
}

function clearAll()
{
    // clear the game in the applet
    ChessBoard.ClearGame();
    
    // clear our search fields
    GameSearch.PlayerOne.value = "any";
    GameSearch.PlayerTwo.value = "any";
    GameSearch.Event.value = "any";
    GameSearch.Eco.value = "any";
    GameSearch.MinRating.value = "any";
    GameSearch.MaxRating.value = "any";
    GameSearch.EarliestYear.value = "any";
    GameSearch.LatestYear.value = "any";
    GameSearch.Result.value = "any";
    GameSearch.PlayerOneColor.value = "any";
}

function ShowSearchStatus(msg)
{
    document.getElementById('Games').innerHTML = "<p>" + msg + "</p>";
    document.getElementById('SearchGames').tabber.tabShow(1);
    //alert(msg);
}

function ShowGames()
{
    var strPage = ChessBoard.buildGamePage();
    document.getElementById('Games').innerHTML = strPage;    
}

function SetPgnPage()
{
    var strPage = ChessBoard.buildPgnPage();
    document.getElementById('PGN').innerHTML = strPage;
}

function ShowEventTab()
{
    var strPage = ChessBoard.buildEventPage();
    document.getElementById('Games2').innerHTML = strPage;
    document.getElementById('SearchGames').tabber.tabShow(2);
}

function ShowStats()
{ 
    var strPage = "<h2>Stats</h2>\n" + ChessBoard.buildStatsTable();
    document.getElementById('Stats').innerHTML = strPage;
}

function ShowURLs()
{ 
    var strPage = ChessBoard.buildURLsPage();
    document.getElementById('URLs').innerHTML = strPage;
}


function getURLParam(strParamName)
{
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 )
  {
    var strQueryString = strHref.substr(strHref.indexOf("?"));
    var aQueryString = strQueryString.split("&");
    for (var iParam = 0; iParam < aQueryString.length; iParam++ )
    {
      if (aQueryString[iParam].indexOf(strParamName + "=") > -1 )
      {
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  
  return strReturn;
}
