includeCSS( "gfx/dsuss.WebClient.Watch.css" );

var g_activeGamesAutorefreshInterval = 15000;

function createActiveGamesTable( returnValue )
{
	isLoggedIn = getCurrentUserName() != null;
	var div = document.getElementById("ActiveGamesTableContainer");

	htmlForDiv =
		'<table id="ActiveGamesTable" width="100%">' +
			'<tr>' +
				'<th>' + dsuss_WebClient_Watch.WhiteHeader + ' / ' + dsuss_WebClient_Watch.BlackHeader + '</th>' +
				'<th style="text-align:left">' + dsuss_WebClient_Watch.TimeControlHeader + '</th>' +
			'</tr>';
			
	if (returnValue != null)
	{
		for ( var i=0; ((returnValue.GetActiveGames.length == null) && (i<1)) || (i < returnValue.GetActiveGames.length); i++)
		{	
			var currentActiveGame = (returnValue.GetActiveGames.length == null)
				? returnValue.GetActiveGames
				: returnValue.GetActiveGames[ i ];

			htmlForDiv += '<tr>';

			if (currentActiveGame.WhiteUserName == "WhiteUnavailablePlayer") {
				htmlForDiv += '<td>' + dsuss_WebClient_Watch.AnalysisGameBy + ' (' + currentActiveGame.AnalysisCaption + ')</td>';
			} else {
				if (isLoggedIn) {
					htmlForDiv +=
						'<td>' +
							'<a href = "javascript:getPlayerDetails(\'' + currentActiveGame.WhiteUserName + '\');" >' + currentActiveGame.WhiteDisplayName + '</a>' +
							'<br>' +
							'<a href = "javascript:getPlayerDetails(\'' + currentActiveGame.BlackUserName + '\');" >' + currentActiveGame.BlackDisplayName + '</a>' +
						'</td>';
				} else {
					htmlForDiv += '<td>' + currentActiveGame.WhiteDisplayName + '<br>' + currentActiveGame.BlackDisplayName + '</td>';
				}
			}

			htmlForDiv +=
					'<td style="text-align:left">';
					
			if (isLoggedIn)
			{
				htmlForDiv +=
						'<input type="button" onclick="javascript: watchGame(' + currentActiveGame.GameId + ')" value="' + dsuss_WebClient_Watch.WatchButton + '" id="WatchButton" style="float:right">';
			}
			
			if (currentActiveGame.WhiteUserName != "WhiteUnavailablePlayer")
			{
				htmlForDiv += currentActiveGame.TimeControl;
			}
			
			htmlForDiv +=
					'</td>' +
				'</tr>';
		}
	}
	
	htmlForDiv += '<tr><td colspan=2><input id="RefreshButton" type="button" onclick="getActiveGames()" value="' + dsuss_WebClient_Watch.RefreshButton + '"></td></tr>';
	htmlForDiv += '</table>';
	div.innerHTML = htmlForDiv;
}

// returns null if player is not playing none of the active games on server. GameId otherwise.
function isPlayingGameId(returnValue) {
	
	if ((returnValue == null) || (getSessionId() == null)) {
		return null;
	}

	var currentUserName = getCurrentUserName();
	if (currentUserName == null) {
		return null;
	}
	
	for ( var i=0; ((returnValue.GetActiveGames.length == null) && (i<1)) || (i < returnValue.GetActiveGames.length); i++) {
			
		var currentActiveGame = (returnValue.GetActiveGames.length == null)
			? returnValue.GetActiveGames
			: returnValue.GetActiveGames[i];
		
		if (compareUserNames( currentUserName, currentActiveGame.WhiteUserName ) || compareUserNames( currentUserName, currentActiveGame.BlackUserName )) {						
			return currentActiveGame.GameId;
		}
	}

	return null;
}

function getActiveGames()
{	
	var returnValue = call("Game/Game","GetActiveGames", "ff");
	
	// if user is playing game ATM, he came to the wrong page, so redirect him to game page.
	// First find out what game is he playing atm.
	var playingGameId = isPlayingGameId( returnValue );
	if (playingGameId != null)
	{
		alert( "you're playing game " + playingGameId );
		stopEventsProcessing();
		document.location = "Game.aspx?sid=" + getSessionId() + "&gid=" + playingGameId;
	}
	else
	{
		createActiveGamesTable( returnValue );
	}
}

function updateFeaturedGame()
{	
	document.getElementById("FeaturedGameImage").src = "gfx/FeaturedGame/snapshot.jpg?" + new Date().getTime();
}

var watchTabVisible = false;
var watchAutorefreshPostponed = false;
var featuredGameAutorefreshPostponed = false;

function onWatchTabShow()
{
	watchTabVisible = true;
	
	if (watchAutorefreshPostponed)
	{
		getActiveGames();
		watchAutorefreshPostponed = false;
	}
	
	if (featuredGameAutorefreshPostponed)
	{
		updateFeaturedGame();
		featuredGameAutorefreshPostponed = false;
	}
}

function onWatchTabHide()
{
	watchTabVisible = false;
}

addTabEventHandler( "WatchTab", onWatchTabShow, onWatchTabHide );

function watchAutorefresh()
{
	if (watchTabVisible)
	{
		getActiveGames();	
	}
	else
	{
		watchAutorefreshPostponed = true;
	}
	
	setTimeout( "watchAutorefresh()", g_activeGamesAutorefreshInterval );
}

var featuredGameRefreshInterval = 1;
function getFeaturedGameRefreshInterval()
{
	var returnValue = call("Game/Game", "GetFeaturedGameRefreshInterval", "ff");

	if (returnValue != null)
	{
		featuredGameRefreshInterval = returnValue["#text"];
	}

	setTimeout("getFeaturedGameRefreshInterval()", 1800000);
}

getFeaturedGameRefreshInterval();

function featuredGameAutorefresh()
{
	if (watchTabVisible)
	{
		updateFeaturedGame();
	}
	else
	{
		featuredGameAutorefreshPostponed = true;
	}
	
	setTimeout( "featuredGameAutorefresh()", featuredGameRefreshInterval * 1000 );
}

function watchGame(gameId)
{
	if (getCurrentUserName() == null)
	{
		alert(dsuss_WebClient_Watch.PleaseLogInMessage);
		return;
	}

	var parameters = new WebMethodParameters( true );
	parameters.add("gameId", gameId);

	var returnValue;
	returnValue = call("Game/Game", "WatchStart", parameters.all);

	stopEventsProcessing();
	document.location = "Game.aspx?sid=" + getSessionId() + "&gid=" + gameId;
}

function watchFeaturedGame ()
{
	var parameters = new WebMethodParameters( true );
	returnValue = call("Game/Game", "GetFeaturedGameId", parameters.all);

	featuredGameId = returnValue["#text"];
	if (featuredGameId != -1) {
		watchGame(featuredGameId);
	}
}

function dsuss_WebClient_Watch_StringsLoaded()
{
	watchAutorefresh();
	featuredGameAutorefresh();
}

includeStringTable( "dsuss.WebClient.Watch" );
