var p1Score;
var p2Score;
var p3Score;
var p1ScoreButton;
var p2ScoreButton;
var p3ScoreButton;
var pointsUp;
var p1Wager;
var p2Wager;
var p3Wager;

var boxUp;
var whoWonLast;

var doubleDiv;
var doubleInput;
var outerFinal;

var singleGrid;
var doubleGrid;
var switchGrid;

var nameTextBox1;
var nameTextBox2;
var nameTextBox3;

var finalName1;
var finalName2;
var finalName3;

var p1NameButton;
var p2NameButton;
var p3NameButton;

var finalRow1;
var finalRow2;
var finalRow3;

var finalTextBox1;
var finalTextBox2;
var finalTextBox3;

var winnerDiv;
var winnerButton;

var marker;
var endButton;

var helpDiv;
var helpButton;

var example1;

function init(){
	outerFinal = document.getElementById( 'outerFinal' );
	outerFinal.style.visibility = 'hidden';
	doubleDiv = document.getElementById( 'doubleDiv' );
	doubleDiv.style.visibility = 'hidden';
	doubleInput = document.getElementById( 'doubleInput' );
	pointsUp = 0;
	p1Score = 0;
	p2Score = 0;
	p3Score = 0;
	whoWonLast = 0;
	boxUp = null;

	example1 = document.getElementById( 'example1' );

	p1ScoreButton = document.getElementById( 'p1ScoreButton' );
	p2ScoreButton = document.getElementById( 'p2ScoreButton' );
	p3ScoreButton = document.getElementById( 'p3ScoreButton' );

	p1Wager = document.getElementById( 'p1Wager' );
	p2Wager = document.getElementById( 'p2Wager' );
	p3Wager = document.getElementById( 'p3Wager' );

	nameTextBox1 = document.getElementById( 'nameTextBox1' );
	nameTextBox2 = document.getElementById( 'nameTextBox2' );
	nameTextBox3 = document.getElementById( 'nameTextBox3' );

	p1NameButton = document.getElementById( 'p1NameButton' );
	p2NameButton = document.getElementById( 'p2NameButton' );
	p3NameButton = document.getElementById( 'p3NameButton' );

	finalName1 = document.getElementById( 'finalName1' );
	finalName2 = document.getElementById( 'finalName2' );
	finalName3 = document.getElementById( 'finalName3' );
	
	finalRow1 = document.getElementById( 'finalRow1' );
	finalRow2 = document.getElementById( 'finalRow2' );
	finalRow3 = document.getElementById( 'finalRow3' );
	finalRow1.style.display = 'none';
	finalRow2.style.display = 'none';
	finalRow3.style.display = 'none';

	finalTextBox1 = document.getElementById( 'finalTextBox1' );
	finalTextBox2 = document.getElementById( 'finalTextBox2' );
	finalTextBox3 = document.getElementById( 'finalTextBox3' );

	marker = document.getElementById( 'marker' );

	singleGrid = document.getElementById( 'singleGrid' );
	singleGrid.style.display = 'block';
	doubleGrid = document.getElementById( 'doubleGrid' );
	doubleGrid.style.display = 'none';
	switchGrid = document.getElementById( 'switchGrid' );

	winnerDiv = document.getElementById( 'winnerDiv' );
	winnerDiv.style.visibility = 'hidden';
	winnerButton = document.getElementById( 'winnerButton' );
	winnerButton.style.color = '#000000';

	endButton = document.getElementById( 'endButton' );
	endButton.style.display = 'none';

	helpDiv = document.getElementById( 'helpDiv' );
	helpDiv.style.display = 'none';
	helpButton = document.getElementById( 'helpButton' );
	
	document.getElementById('appDiv').style.visibility = 'visible';
	nameTextBox1.focus();
}

// checks to see which windows are open and determines which points to add to which player
function addPoints( id, isWrong ){
	if( doubleDiv.style.visibility == 'hidden' ){
		if( outerFinal.style.visibility == 'hidden' ){
			if( isWrong == true ){
				switch( id ){
					case 1:
						p1Score = p1Score + +pointsUp;
						p1ScoreButton.value = p1Score;
					break;
					case 2:
						p2Score = p2Score + +pointsUp;
						p2ScoreButton.value = p2Score;
					break;
					case 3:
						p3Score = p3Score + +pointsUp;
						p3ScoreButton.value = p3Score;
					break;
				}
				checkNeg( id );
				setBoxWin( id, true );
				setWinnerMarker( id );
				pointsUp = 0;

			}else{
				switch( id ){
					case 1:
						p1Score = p1Score - +pointsUp;
						p1ScoreButton.value = p1Score;
					break;
					case 2:
						p2Score = p2Score - +pointsUp;
						p2ScoreButton.value = p2Score;
					break;
					case 3:
						p3Score = p3Score - +pointsUp;
						p3ScoreButton.value = p3Score;
					break;
				}
				setBoxWin( id, false );
				checkNeg( id );
				pointsUp = 0;
			}
		}else{
			if( isWrong == true ){
				switch( id ){
					case 1:
						p1Score = p1Score + +p1Wager.value;
						p1ScoreButton.value = p1Score;
					break;
					case 2:
						p2Score = p2Score + +p2Wager.value;
						p2ScoreButton.value = p2Score;
					break;
					case 3:
						p3Score = p3Score + +p3Wager.value;
						p3ScoreButton.value = p3Score;
					break;
				}
				checkNeg( id );
			}else{
				switch( id ){
					case 1:
						p1Score = p1Score - +p1Wager.value;
						p1ScoreButton.value = p1Score;
					break;
					case 2:
						p2Score = p2Score - +p2Wager.value;
						p2ScoreButton.value = p2Score;
					break;
					case 3:
						p3Score = p3Score - +p3Wager.value;
						p3ScoreButton.value = p3Score;
					break;
				}
				checkNeg( id );
			}
		}
	}else{
		if( isWrong == true ){
			switch( id ){
				case 1:
					p1Score = p1Score + +doubleInput.value;
					p1ScoreButton.value = p1Score;
				break;
				case 2:
					p2Score = p2Score + +doubleInput.value;
					p2ScoreButton.value = p2Score;
				break;
				case 3:
					p3Score = p3Score + +doubleInput.value;
					p3ScoreButton.value = p3Score;
				break;
			}
			if( boxUp ){
				setBoxWin( id, true );
			}
			checkNeg( id );
			toggleDD();
		}else{
			switch( id ){
				case 1:
					p1Score = p1Score - +doubleInput.value;
					p1ScoreButton.value = p1Score;
				break;
				case 2:
					p2Score = p2Score - +doubleInput.value;
					p2ScoreButton.value = p2Score;
				break;
				case 3:
					p3Score = p3Score - +doubleInput.value;
					p3ScoreButton.value = p3Score;
				break;
			}
			if( boxUp ){
				setBoxWin( id, false );
			}
			checkNeg( id );
			toggleDD();
			checkNeg( id );
		}
	}
}

// shows/hides final j-pardy screen
function toggleFJ(){
	if( outerFinal.style.visibility == 'hidden' ){
		outerFinal.style.visibility = 'visible';
		if( doubleDiv.style.visibility == 'visible' ){ doubleDiv.style.visibility = 'hidden'; }
		if( p1Score > 0 ){ finalRow1.style.display = 'block'; }
		if( p2Score > 0 ){ finalRow2.style.display = 'block'; }
		if( p3Score > 0 ){ finalRow3.style.display = 'block'; }
		focusFinal();
	}else{
		outerFinal.style.visibility = 'hidden';
		finalRow1.style.display = 'none';
		finalRow2.style.display = 'none';
		finalRow3.style.display = 'none';
	}
}

// shows/hides the daily double input
function toggleDD(){
	if( doubleDiv.style.visibility == 'hidden' ){
		doubleDiv.style.visibility = 'visible';
	}else{
		doubleInput.value = '';
		doubleDiv.style.visibility = 'hidden';
	}
}

// toggles single and double j-pardy
function switchBoards(){
	if( singleGrid.style.display == 'block' ){
		singleGrid.style.display = 'none';
		doubleGrid.style.display = 'block';
		switchGrid.value = 'J-PARDY! ROUND';
	}else{
		singleGrid.style.display = 'block';
		doubleGrid.style.display = 'none';
		switchGrid.value = 'DOUBLE J-PARDY!';
	}
}

// turns boxUp to grey after no one answers
function muckBox(){
	if( boxUp ){
		boxUp.className = 'muckClass';
		pointsUp = 0;
		boxUp = null;
	}
}

// sets up which question is up and how many points for grabs
function setScore( points, obj ){
	if( boxUp ){
		if( obj == boxUp ){
			boxUp.className = 'boxUnselected';
			pointsUp = 0;
			boxUp = null;
		}else{
			boxUp.className = 'boxUnselected';
			boxUp = obj;
			boxUp.className = 'boxSelected';
			pointsUp = points;
		}
	}else{
		boxUp = obj;
		boxUp.className = 'boxSelected';
		pointsUp = points;
	}
}

// moves astrisk to indicate who got last question correct
function setWinnerMarker( id ){
	if( whoWonLast != id ){
		whoWonLast = id;
		marker.style.visibility = 'visible';
		marker.className = 'p' + id + '_markerLocation';
	}
}

// pops names from name entry screen into the player desks and final J-PARDY entries
function setNames(){
	blueDiv.style.visibility = 'hidden';
	finalName1.value = nameTextBox1.value + ': ';
	finalName2.value = nameTextBox2.value + ': ';
	finalName3.value = nameTextBox3.value + ': ';
	setTimeout( 'p1NameButton.value = nameTextBox1.value;',500 );
	setTimeout( 'p2NameButton.value = nameTextBox2.value;',1000 );
	setTimeout( 'p3NameButton.value = nameTextBox3.value;',1500 );
}

// colors grid box appropriately
function setBoxWin( x, isWrong ){
	switch( x ){
		case 1:
			if( isWrong == true ){	boxUp.className = 'p1_boxRight';
			}else{ boxUp.className = 'p1_boxWrong'; }
			boxUp = null;
		break;
		case 2:
			if( isWrong == true ){ boxUp.className = 'p2_boxRight';
			}else{ boxUp.className = 'p2_boxWrong'; }
			boxUp = null;
		break;
		case 3:
			if( isWrong == true ){ boxUp.className = 'p3_boxRight';
			}else{ boxUp.className = 'p3_boxWrong'; }
			boxUp = null;
		break;
	}
}

// checks to see if player's score is negative and colors the players desk red if so
function checkNeg( id ){
	switch( id ){
		case 1:
			if( p1Score < 0 ){
				p1Div.style.backgroundColor = '#FF0000';
				p1ScoreButton.style.backgroundColor = '#FF0000';
				p1NameButton.style.backgroundColor = '#FF0000';
			}else{
				p1Div.style.backgroundColor = '#0000CC';
				p1ScoreButton.style.backgroundColor = '#0000CC';
				p1NameButton.style.backgroundColor = '#0000CC';
			}
		break;
		case 2:
			if( p2Score < 0 ){
				p2Div.style.backgroundColor = '#FF0000';
				p2ScoreButton.style.backgroundColor = '#FF0000';
				p2NameButton.style.backgroundColor = '#FF0000';
			}else{
				p2Div.style.backgroundColor = '#0000CC';
				p2ScoreButton.style.backgroundColor = '#0000CC';
				p2NameButton.style.backgroundColor = '#0000CC';
			}
		break;
		case 3:
			if( p3Score < 0 ){
				p3Div.style.backgroundColor = '#FF0000';
				p3ScoreButton.style.backgroundColor = '#FF0000';
				p3NameButton.style.backgroundColor = '#FF0000';
			}else{
				p3Div.style.backgroundColor = '#0000CC';
				p3ScoreButton.style.backgroundColor = '#0000CC';
				p3NameButton.style.backgroundColor = '#0000CC';
			}
		break;
	}
}

// moves the who won last marker
function setWinnerMarker( id ){
	if( whoWonLast != id ){
		whoWonLast = id;
		marker.style.visibility = 'visible';
		marker.className = 'p' + id + 'Marker';
	}
}

//  shows final answers and calculates winner
function showFinal(){
	if( p1Score > 0 ){ 
		finalTextBox1.style.visibility = 'visible';
		p1Wager.style.visibility = 'visible';
	}
	if( p2Score > 0 ){ 
		finalTextBox2.style.visibility = 'visible';
		p2Wager.style.visibility = 'visible';
	}
	if( p3Score > 0 ){ 
		finalTextBox3.style.visibility = 'visible';
		p3Wager.style.visibility = 'visible';
	}
}

// shows/hides the daily double input
			function toggleHelp(){
				if( helpDiv.style.display == 'block' ){
					helpDiv.style.display = 'none';
				}else{
					helpDiv.style.display = 'block';
				}
			}

// focuses on appropriate final answer box
function focusFinal(){
	if( p1Score > 0 ){ p1Wager.focus();
	}else{
		if( p2Score > 0 ){ p2Wager.focus(); 
		}else{
			if( p3Score > 0 ){ p2Wager.focus(); }
		}
	}
}

// shows end game sequence
function endGame(){
	if( p1Score > p2Score && p1Score > p3Score ){
		winnerButton.value = nameTextBox1.value + " WINS!";
		winnerDiv.style.backgroundColor = '#FF0000';
		winnerButton.style.backgroundColor = '#FF0000';
	}else{
		if( p2Score > p1Score && p2Score > p3Score ){
			winnerButton.value = nameTextBox2.value + " WINS!";
			winnerDiv.style.backgroundColor = '#00EE00';
			winnerButton.style.backgroundColor = '#00EE00';
		}else{
			if( p3Score > p1Score && p3Score > p2Score ){
				winnerButton.value = nameTextBox3.value + " WINS!";
				winnerDiv.style.backgroundColor = '#FFFF00';
				winnerButton.style.backgroundColor = '#FFFF00';
			}
		}
	}
	winnerFlash();
	winnerDiv.style.visibility = 'visible';
	grow();
}

var vSpot = 190;
var hSpot = 200;
var vDiv = 70;
var hDiv = 250;

// grows winner div
function grow(){
	styleObj = winnerDiv.style;
	if ( hSpot > 0 ){
		hSpot -= 5;
		styleObj.left = hSpot + "px";

		setTimeout( 'grow()',50 );   
    }
	if( vSpot > 0 ){
		vSpot -= 5;
		styleObj.top = vSpot + "px";
	}
	if( hDiv < 645 ){
		hDiv += 10;
		styleObj.width = hDiv + "px";
	}
	if( vDiv < 550 ){
		vDiv += 11;
		styleObj.height = vDiv + "px";
	}
}

var flashTO;

function winnerFlash(){
	if( winnerButton.style.color == '#000000' ){
		winnerButton.style.color = '#FFFFFF';
	}else{
		winnerButton.style.color = '#000000';
	}
	flashTO = setTimeout( 'winnerFlash()',150 );
}
