function PokerClock()
{
	this.initialise = function()
	{
		this.startTime = new Date();
		this.blindLevels = new Array( 
		  new BlindLevel( 10, 20, '#0f0' ), 
		  new BlindLevel( 20, 40, '#ff9999' ),
		  new BlindLevel( 30, 60, '#99ff99' ),
		  new BlindLevel( 50, 100, '#ff9999' ),
		  new BlindLevel( 100, 200, '#99ff99' ),
		  new BlindLevel( 150, 300, '#ff9999' ),
		  new BlindLevel( 200, 400, '#99ff99' ),
		  new BlindLevel( 300, 600, '#ff9999' ),
		  new BlindLevel( 400, 800, '#99ff99' ),
		  new BlindLevel( 500, 1000, '#ff9999' ),
		  new BlindLevel( 600, 1200, '#99ff99' ),
		  new BlindLevel( 800, 1600, '#ff9999' ),
		  new BlindLevel( 1000, 2000, '#99ff99' ),
		  new BlindLevel( 1500, 3000, '#ff9999' ),
		  new BlindLevel( 2000, 4000, '#99ff99' ),
		  new BlindLevel( 3000, 6000, '#ff9999' ),
		  new BlindLevel( 4000, 8000, '#99ff99' ),
		  new BlindLevel( 5000, 10000, '#ff9999' )
		 );
		this.lastBlindLevel = -1;
		this.pauseStartTime = 0;	
		this.pauseTimeTotal = 0;	
		this.skipTime = 0;
		this.gameTime = 0;
		this.objPaused = document.getElementById( 'pausedText' );
		this.objRebuyPlaceholder = document.getElementById( 'rebuyPlaceholder' );
		this.objGameTime = document.getElementById( 'gameTime' );
		this.objProgress = document.getElementById( 'blindProgressBar' );
		this.objBlindChangeTime = document.getElementById( 'blindChangeTime' );
		this.objBlindProgressContainer = document.getElementById( 'blindProgressContainer' );
		this.objNextBlindsContainer = document.getElementById( 'nextBlindsContainer' );
		this.objNextBlinds = document.getElementById( 'nextBlinds' );
		this.objProgressContainer = document.getElementById( 'blindProgressContainer' );
		this.objBlinds = document.getElementById( 'currentBlinds' );
		this.objBlindsShadow = document.getElementById( 'currentBlindsShadow' );
		this.objRebuyText = document.createTextNode( '' );
		this.objRebuyPlaceholder.appendChild( this.objRebuyText );	
		// Start paused
		this.updateTime();
		document.body.className = 'paused';
		this.pauseStartTime = this.startTime.getTime();
		this.updateTime();
	}
	
	this.initFromXml = function( xml )
	{
		this.blindTime = parseInt( xml.getAttribute( 'blindtime' ) );
		this.rebuyTime = parseInt( xml.getAttribute( 'rebuytime' ) );
		this.initialise();
	}
	
	this.initWithValues = function( blindMins, rebuyMins )
	{
		this.blindTime = blindMins * 60 * 1000;
		this.rebuyTime = rebuyMins * 60 * 1000;
		this.initialise();
	}
	
	function BlindLevel( little, big, colour )
	{
		this.little = little;
		this.big = big;
		this.colour = colour;	
	}
	
	this.toXml = function( documentContext )
	{
		if ( !documentContext )
		{
			alert( "No documentContext passed to PokerClock.toXML()" );
			return null;
		}
		var clockElement = documentContext.createElement( 'clock' );
		clockElement.setAttribute( 'gametime', this.gameTime );
		clockElement.setAttribute( 'blindtime', this.blindTime );
		clockElement.setAttribute( 'rebuytime', this.rebuyTime );
		return clockElement;
	}
	
	this.incrementTime = function(direction)
	{
		if ( direction > 0 )
		{
			/* Skip forward a minute */
			this.skipTime += 60 * 1000;
		}
		else
		{
			/* Skip back a minute */
			this.skipTime -= 60 * 1000;
			/* this.gameTime = this.getGameTime();
			if ( this.gameTime < 0 ) 
			{
				this.skipTime = this.gameTime;
			} */
		}
		this.updateTime();
	}
	
	this.isPaused = function()
	{
		return ( document.body.className == 'paused' );
	}
	
	this.togglePaused = function()
	{
		//var objProgress = document.getElementById( 'blindProgressContainer' );
		var objPausedButton = document.getElementById( 'pauseButton' );
		var now = new Date();
		if ( this.isPaused() )
		{
			/* UN-PAUSE THE CLOCK */
			document.body.className = '';
			this.objPaused.style.display = 'none';
			//objProgress.style.display = 'block';
			objPausedButton.value = '||';
			this.pauseTimeTotal += ( now.getTime() - this.pauseStartTime );
			this.updateTime();
		}
		else
		{
			/* PAUSE THE CLOCK */
			document.body.className = 'paused';
			this.objPaused.style.display = 'block';
			//objProgress.style.display = 'none';
			objPausedButton.value = '>';
			this.pauseStartTime = now.getTime();
		}
	}
	
	this.calculateGameTime = function()
	{
		var now = new Date();
		this.gameTime = now.getTime() - this.startTime.getTime() - this.pauseTimeTotal + this.skipTime;	
	}
	
	/**
	 * Called at a given interval to update game progress
	 */
	this.updateTime = function()
	{
		if ( this.isPaused() ) return;
		this.calculateGameTime();
		var blindLevel = Math.floor( this.gameTime / this.blindTime );
		this.displayTime();
		if ( this.blindLevels.length > blindLevel )
		{
			if ( blindLevel != this.lastBlindLevel )
			{
				this.playBlindChangeSound();
				this.displayBlinds( blindLevel );
				this.lastBlindLevel = blindLevel;
			}
		}
		this.moreBlindLevels( this.blindLevels.length - 1 > blindLevel )
		if ( this.timerHandle )
		{
			clearTimeout( this.timerHandle );
		}
		this.timerHandle = setTimeout( 'game.clock.updateTime()', 500 ); 
	}
	
	/**
	 * Display time-related information
	 */
	this.displayTime = function()
	{
		var remainingRebuyTime = this.rebuyTime - this.gameTime;
		var blindChangeTime = this.blindTime - ( this.gameTime % this.blindTime );
		var blindIndex = Math.floor( this.gameTime / this.blindTime );
		var blindProgress = ( this.gameTime % this.blindTime ) / this.blindTime;
		this.objProgress.style.width = Math.floor( blindProgress * 100 ) + '%';
		this.objGameTime.innerHTML = fmtTime( this.gameTime );
		this.objBlindChangeTime.innerHTML = fmtTime( blindChangeTime );
		// Rebuys
		if ( remainingRebuyTime > 0 )
		{
			this.objRebuyText.nodeValue = fmtTime( remainingRebuyTime ) + ' left';
			this.objRebuyPlaceholder.className = '';
		}
		else
		{
			this.objRebuyText.nodeValue = 'expired';
			this.objRebuyPlaceholder.className = 'expired';
		}
	}
	
	/**
	 * Display blind information 
	 */
	this.displayBlinds = function( blindLevel )
	{
		var blinds = this.blindLevels[ blindLevel ]; 
		if ( this.blindLevels.length > blindLevel + 1 )
		{
			var nextBlinds = this.blindLevels[ blindLevel + 1 ];
			this.objNextBlinds.innerHTML = nextBlinds.little + ' - ' + nextBlinds.big;
		}
		else
		{
			this.objNextBlinds.innerHTML = '';
		}
		this.objBlinds.innerHTML = blinds.little + ' - ' + blinds.big;
		this.objBlindsShadow.innerHTML = this.objBlinds.innerHTML;
		this.objProgress.style.backgroundColor = blinds.colour;
	}
	
	/**
	 * Hide the blind information because there are no more blind levels
	 */
	this.moreBlindLevels = function( isMoreBlinds )
	{
		if ( this.objProgress.style.visibility == 'hidden' && isMoreBlinds )
		{
			this.objProgress.style.visibility = 'visible';
		}
		else if ( this.objProgress.style.visibility != 'hidden' && !isMoreBlinds )
		{
			this.objProgress.style.visibility = 'hidden';		
		}
		this.objNextBlindsContainer.style.visibility = isMoreBlinds ? 'visible' : 'hidden';
	}
	
	this.playBlindChangeSound = function()
	{
		try
		{
			if ( !document.blindChangeSoundPlayerApplet )
			{
				return false;
			}
			document.blindChangeSoundPlayerApplet.playAudio();
			return true;
		} catch(err) 
		{
			//alert("Error loading blind change sound " + err.description);		
			return false;
		}
	}
}