
BackgammonRoom.kRefresh = 'rfsh';
BackgammonRoom.kbRefresh = 'brfsh';

BackgammonRoom.prototype = new GameRoom();
BackgammonRoom.prototype.constructor = BackgammonRoom;
function BackgammonRoom( v )
{
	GameRoom.call( this, v );
}

BackgammonRoom.prototype.AutoRefreshOnClick = function(el)
{
	if ( this.mMyTurns ) 
	{ 
		if ( el ) { el.checked = false; }
		var html = '<TABLE style=\"margin:8px;\"><TR><TD>You already have at least one game where it is your turn.</TD></TR><TR><TD style="vertical-align:middle; text-align:center; padding:8px;"><INPUT type="button" onclick="UI_ShowOverlay(false);" value="OK"></TD></TR></TABLE>';
		UI_ShowOverlay( true, html ); 
		return; 
	}
	
	this.mbRefreshMode = this.mbRefreshMode ? false : true;
	if ( this.mbRefreshMode ) 
	{ 
		var now = new Date();
		this.mTimeLoaded = now.getTime();
	}
	
	var img = GetElement( 'bg_refresh_img' );
	if ( img ) { img.style.visibility = this.mbRefreshMode ? 'visible' : 'hidden'; }
	this.SetRefreshTimeout( this.mbRefreshMode );
};

BackgammonRoom.prototype.IsAutoRefreshMode = function()
{
	return this.mbRefreshMode ? 1 : 0;
};

BackgammonRoom.prototype.CheckForTurn = function()
{
	var last = CookieGet( 'ckturn' );
	var now = new Date();
	now = now.getTime();
	if ( now-last < 20*1000 ) { ErrorOutput( 'Too soon to request check for turn: delta=' + (now-last) ); return; }

	DebugOutput( 'checking for turn' );
	CookieSet( 'ckturn', now );
	var sendTxt = 'ckturn=1';
	var url = '/backgammon-room.pl';
	if ( !this.StartRemoteRequest( RemoteRequest.NewRequest( url, sendTxt, { mCompletion:{obj:this, funcName:'CheckForTurn_Complete' }, mTimeout:{ obj:this, funcName:'RemoteRequest_Timeout', timeout:30 } } ) ) )
	{
		this.FatalError();
	}
};

var set_title = 'ZooEscape - Backgammon';
BackgammonRoom.prototype.UpdateTitle = function()
{
    var t = set_title;
    if (this.mBlinkCount%2) 
    { 
	    var txt = this.mMyTurns + (this.mMyTurns > 1 ? ' games' : ' game');
    	t = 'It is your turn! ('+txt+') -- ' + t; 
	}
//	DebugOutput( t );
    try { document.title = t; } catch(e) {}
};

BackgammonRoom.prototype.BlinkTitle = function()
{
    if (++this.mBlinkCount>10) 
    {
    	this.mBlinkCount = 1;
		this.UpdateTitle();
		return;
    }
    this.UpdateTitle();
    var g = this;
	setTimeout( function() { var bg=g; bg.BlinkTitle(); }, 1000 );
};

BackgammonRoom.prototype.CheckForTurn_Complete = function( response, rr )
{
	var bHasTurn = ConvertToInt( response ) ? true : false;
	if ( bHasTurn )
	{
		alert( 'It is your turn!' ); // REVISIT -- enhance
		RedirectImmediate();
	}
	else
	{
		this.SetRefreshTimeout( true );
	}
	this.DeleteRemoteRequest( rr );
};

var maxWait = 10*60*1000;
BackgammonRoom.prototype.SetRefreshTimeout = function( bOn )
{
	if ( !(this.mbLoggedIn&&this.mNumGames>0) ) { return; }
	
	var now = new Date();
	now = now.getTime();
	if ( bOn )
	{
		var bg = this;
		var timeout = maxWait;
		if ( this.IsAutoRefreshMode() && !this.mMyTurns )
		{
			var diff = now - this.mTimeLoaded;
			DebugOutput( 'now=' + now + ', loaded=' + this.mTimeLoaded );
			if ( diff < maxWait )
			{
				if ( this.mWaitTime === undefined ) { this.mWaitTime = 60*1000; }
				else 								{ this.mWaitTime = Math.floor( this.mWaitTime * 1.1 ); }
				
				if (this.mWaitTime>600000) this.mWaitTime=600000;
				this.mRefreshTimeoutID = window.setTimeout( function(){ if(bg){ bg.CheckForTurn(); }}, this.mWaitTime );
			}
			else
			{
				timeout = 2;
			}
		}
		if ( !this.mRefreshTimeoutID )
		{
			this.mRefreshTimeoutID = window.setTimeout( function(){ if(bg){bg.OnExit();} RedirectImmediate('/backgammon-room.pl?nla=1'); }, timeout ); // reload every 10 minutes, but don't update the user's last_access
		}
	}
	else
	{
		if ( this.mRefreshTimeoutID )
		{
			window.clearTimeout( this.mRefreshTimeoutID );
			this.mRefreshTimeoutID = undefined;
		}
	}
};

BackgammonRoom.prototype.OnExit = function()
{
	this.SetRefreshTimeout( false );
	this.DeleteRemoteRequests();
};

BackgammonRoom.prototype.RefreshGames = function( el )
{
	if ( !el ) { return; }

	this.OnExit();
		
    var butt = el;
    var counter = 0;
    var total = 0;
    butt.disabled = 1;
    var field = '........';
    var id = setInterval(
	function()
	{
	    counter++;

	    var n = Math.abs( counter - field.length ) % field.length;
	    butt.value = field.substr( 0, n ) + 'Please wait' + field.substr( n );
	    if (counter>=(2*field.length-2)) counter = 0;
	    
	    if (++total>20)
	    {
			window.clearInterval( id );

			RedirectImmediate('',true);	// 5 seconds
	    }
	},
    50 );
};

function RoomObjInit(sortOrder,variation,numid)
{
	GameVariation.SetupCurrentVariation( sortOrder,variation );
	var g = new BackgammonRoom( GameVariation.mCurrentVariation );
	g.mUserID	= numid;
}

function RoomSetup(bReload)
{
	var g=Game.mSingleton;
	if(g)
	{
		g.mbAutoReload = bReload;
		g.mbLoggedIn= g.mUserID ? true : false;

		if ( !g.mMyTurns ) {
			var now = new Date();
			g.mTimeLoaded = now.getTime();
			g.SetRefreshTimeout( true );
		}
		else
		if ( g.mbAutoReload ) {
			g.mBlinkCount = 0;
			setTimeout( function() { var bg=g; bg.BlinkTitle(); }, 1000 );
		}
		
		if ( g.mStartGameDestDivName && g.mStartGameSrcDivName ){
			var dst = GetElement( g.mStartGameDestDivName );
			var src = GetElement( g.mStartGameSrcDivName );
			if ( dst && src ) { dst.appendChild( src ); }
		}
	}
}
