function Defaults(){
	number_of_cases = 26;
	turn = new Array(6, 5, 4, 3, 2, 1, 1, 1, 1, 1, 1);

	// main divs
	casesDiv = document.getElementById('cases');
	leftDiv = document.getElementById('leftValues');
	rightDiv = document.getElementById('rightValues');
	message = document.getElementById('gameMessage');
	overlay = document.getElementById('gameOverlay');
	prefs = document.getElementById('gamePrefs');
	rules = document.getElementById('gameRules');
	// Off by default
	overlay.style.display = 'none';
	prefs.style.display = 'none';
	rules.style.display = 'none';

	// Buttons for forms.
	buttons = "<input type='button' id='deal' value='Deal' onClick='deal()'> <input type='button' id='nodeal' value='No Deal' onClick='nodeal()'>";
	buttons2 = "<input type='button' class='button' value='Keep' onClick='keep()'> <input type='button' class='button' value='Take' onClick='take()'>";
	resetButton = "<br><br><input type='button' value='Another Game?' onClick=\"newgame('reset');\">";
	beginButton = "<br><input type='button' value='Begin Game' onClick=\"overlay.style.display = 'none';\">";

	// messages
	message_win = "";
	quickRules = "<div id='quickRules'>Quick Rules<br><br>Start the game by picking a case to keep. Then you open cases to eliminate from the board... trying to keep the highest values in play.<br><br>Each round, you open one less case than you did the round before... the first round you open 6. <br><br>When you are done, the banker will call with an offer to buy your case. He'll make the offer based on the amounts left in play. You can then choose to make a deal with the banker for the offer amount, or risk that and go on opening another bunch of cases.<br><br>Keep going for as long as it takes but be careful or you could lose it all!</div>";
	welcome_normal = "<p>Welcome to Deal or No Deal!</p>" + quickRules +  beginButton;
	welcome_xmas = "<p>Welcome to Deal or No Deal:<br>Christmas Special Edition!!!</p>" + quickRules + beginButton;
	welcome_quest = "Welcome to Deal or No Deal:<br>Quest for $1,000,000!!!";
	if( questGame > 1 ){ welcome_quest = welcome_quest + "<BR>Another $1,000,00 has been added to the board!"; }
	welcome_quest += quickRules + beginButton;

	// Game settings, determine the values of the cases
	if( setting['gameType'] == 'xmas' ){	values = Array(.01, 1, 5, 10, 25, 50, 75, 100, 200, 300, 400, 500, 750, 1000, 5000, 10000, 25000, 50000, 75000, 100000, 200000, 400000, 500000, 1000000, 2500000, 5000000); }
	else if ( setting['gameType'] == 'quest' ){
		values = Array(.01, 1, 5, 10, 25, 50, 75, 100, 200, 300, 400, 500, 750, 1000, 5000, 10000, 25000, 50000, 75000, 100000, 200000, 300000, 400000, 500000, 750000, 1000000);

		// Each round of a quest game, another value is replaced with a million.
		for( i=1; i <= questGame; i++ ){
			values[values.length-i] = 1000000;
		}
	}
	else { values = Array(.01, 1, 5, 10, 25, 50, 75, 100, 200, 300, 400, 500, 750, 1000, 5000, 10000, 25000, 50000, 75000, 100000, 200000, 300000, 400000, 500000, 750000, 1000000); }
}




/**
 * Once a turn is completed, settings are adjusted and offers are made
 *
 */
function completeTurn(){
	round++; // first, go to the next round... for programming sake, rounds start at 0 and work up from there.
	turncount = 0; // reset turns to 0.
	tmpTotal = 0; // place to add up the remaining values.
	tmpCases = 0; // number of cases still unopened.
	offer = 0; // banker's offer.

	for(i=1; i <= number_of_cases; i++){
		objCase = eval('case' + i); // the case object

		// Only count cases that are still open
		if( objCase.open == 0 ){
			tmpTotal = tmpTotal + objCase.value.val;
			tmpCases++;
		}
	}

	// Calculate the offer and add to our settings
	offer = parseInt(tmpTotal / tmpCases);
	setting['offer_value'] = offer;
	setting['offer_display'] = '$' + addCommas(offer);

	// Set up our overlay
	overlay.className = 'offer';
	overlay.style.display = '';
	overlay.innerHTML = "<p>The banker is calling...</p>";

	// If there are previous offers, we need to display things a little differently
	if( prevOffers.length > 0 ){
		// Mystery offer settings... if the mystery box is checked and this is the mystery round...
		if( setting['mysteryoffer'] && round == setting['mysteryround'] ){

			// Because there is a mystery offer, we need to record what the original offer would have been
			setting['mystery_original_offer'] = offer;

			// As a bonus, because this is a mystery offer, we're going to adjust the offer by + $100k or minus half the offer
			offer = rand(setting['offer_value'] + 100000, parseInt(setting['offer_value']/2));
			setting['offer_value'] = offer;
			setting['offer_display'] = '$' + addCommas(offer);

			// Now set the message
			message_offer = "<table><tr><td width=75% align=center valign=center>This is the mystery round!!! Which means that the banker isn't telling what the offer is. But I can say that it could be MORE or LESS than what it would normally be. So the question still remains...<BR><BR> Deal Or No Deal?<BR><BR>" + buttons + "</td><td width=25% align=center valign=top>" + previousOffers() + "</td></tr></table>";
		}
		else {
			message_offer = "<table><tr><td width=75% align=center valign=center>The banker is making you an offer of " + setting['offer_display'] + "<BR><BR> Deal Or No Deal?<BR><BR>" + buttons + "</td><td width=25% align=center valign=top>" + previousOffers() + "</td></tr></table>";
		}
	}
	else {
		message_offer = "The banker is making you an offer of " + setting['offer_display'] + "<BR><BR> Deal Or No Deal?<BR><BR>" + buttons;
	}

	// display the banker's offer after a 2 second delay
	setTimeout("overlay.innerHTML = message_offer;", 2000);
}



/**
 * Player has taken the bankers offer
 */
function deal(){
	overlay.innerHTML = "DEAL!! <br><br>But just how good of a deal? Let's see what was in your case....";

	takedeal = setting['offer_display']; // This is the deal was made for.

	message_win = "<p>You made a deal for " + takedeal + "<br><br>Inside your case is: <br><br>$" + addCommas(pickone.value.val) + "<br><br>Thank you for playing!<BR><BR>";

	winningAmount = takedeal;

	setTimeout("endGame();", 2000);
}


/**
 * Player has refused the banks offer
 */
function nodeal(){
	timeinseconds = 2000;

	// Mystery offer settings... if playing with a myster offer and it's offered this round....
	if( setting['mysteryoffer'] && round == setting['mysteryround'] ){

		message = "You turned down the mystery offer... but what was the mystery offer?<br><br>The original offer would have been $" + addCommas(setting['mystery_original_offer']) + "<br>and the mystery offer was " + setting['offer_display'] + "<br><br>Let's continue picking some cases.<br><br>This round you have " + turn[round] + " cases to open.";

		// display overlay
		overlay.className = 'standard';
		overlay.innerHTML = message;

		setTimeout("overlay.innerHTML = ''; overlay.style.display ='none';", 5000);
	}
	// No more rounds, just a choice to keep the case or take the final case available.
	else if( round == 9 ){
		overlay.innerHTML = "There are only 2 cases left. Yours and the one still in play. So now you have to choose... <br><br>Keep your case?<br>Or take the other one?<br><br>" + buttons2;
	}
	else {
		overlay.innerHTML = 'No Deal!!! <br><br>Let\'s continue.. <br>this next round, you have ' + turn[round] + ' cases to open.';

		setTimeout("overlay.innerHTML = ''; overlay.style.display ='none';", 2000);
	}

	// Keep a record of all previous offers
	prevOffers[prevOffers.length] = setting['offer_display']; // store to display later.
}

// If the player decides to keep his/her case at the very end.
function keep(){
	overlay.innerHTML = "You have decided to keep the case that you picked when you started this game.<br><br>But was it a wise choice???";

	message_win = "Inside your case is: <br><br>$" + addCommas(pickone.value.val) + "<br><br>Thank you for playing!<BR><BR>";

	// The winning total
	winningAmount = '$' + addCommas(pickone.value.val);

	setTimeout("endGame();", 2000);
}

function take(){
	tmpObj = '';

	// first we need to know which case is still in play
	for(i=1; i <= number_of_cases; i++){
		objCase = eval('case' + i);

		if( objCase.open == 0 && objCase.number != pickone.number ){
			tmpObj = objCase;
			break;
		}
	}

	overlay.innerHTML = "You have decided to take the case that was still in play rather than keep the one you've had all this... <br><br>But was it a wise choice???";

	message_win = "The case you chose holds: <br><br>$" + addCommas(tmpObj.value.val) + "<br><br>Which means that inside your case was: <br><br>$" + addCommas(pickone.value.val) + "<br><br>Thank you for playing!<BR><BR>";

	// player's final case is the one found that's still not open
	winningAmount = '$' + addCommas(tmpObj.value.val);

	setTimeout("endGame();", 2000);
}


function previousOffers(){
	var tmp = '';

	tmp = "<ul id='offersList'>\n";
	tmp += "<li><strong>Previous Offers</strong></li>\n";

	for( i=prevOffers.length-1; i>=0; i--){
		tmp += "<li>"+ prevOffers[i] + "</li>\n";
	}

	tmp += "</ul>\n";

	return tmp;
}



/**
 * endGame()	 Game over... display message and reset button
 */
function endGame(){
	gameover = 1; // Set this first, since... well, it's game over.

	if( setting['gameType'] == 'quest' && winningAmount == '$1,000,000' ){
		questGame = 0; // Reset this.
		message_win = message_win + "You WON THE MILLION!!! You completed the quest and are the ultimate Deal or No Deal winner!!!<br><br>This quest will be reset for the next round.<br><br>";
	}
	else if( setting['gameType'] == 'quest' && questGame == questRounds ){
		questGame = 0; // Reset this.
		message_win = message_win + "You didn't win the million, even after all those tries.<br><br>But you did win " + finalCase.valueDollar + " so it's not a total loss!<br><br>The values have now been reset. Give it another try!";
	}
	else if ( setting['gameType'] == 'quest' ){
		message_win = message_win + "You didn't win the million which means that the next game will have ANOTHER million added to it!";
	}

	overlay.className = 'standard';
	overlay.innerHTML = message_win + resetButton;
}





function newgame(operation){

	// If this is the quest for a million... 
	if( setting['gameType'] == 'quest' && gameover == 1 ){
		questGame++; // Add one to the number of times we've tried for a million.
	}
	else {
		questGame = 1;
	}

	// If the mystery offer has been checked... then pick a random round to display a mystery offer
	if( setting['mysteryoffer'] ){ setting['mysteryround'] = rand(2, turn.length-3); }

	// default start up
	pickone = 0;
	winningAmount = 0;
	turncount = 0;
	round = 0;
	response = 0;
	takedeal = 0;
	gameover = 0;
	finalCase = '';
	questRounds = 6;
	prevOffers = Array();


	// Sets things like the divs and buttons
	Defaults();

	// Create case objects
	createCases();

	// Create value objects
	createValues();

	// Shuffle the values
	if( setting['shuffle'] == true ){
		shuffle(values);
	}

	// Assign values to each case
	for(i=1; i<= number_of_cases; i++){
		// Each case object
		caseObj = eval('case' + i);

		// Assign the value object to the case's value property
		caseObj.value = eval(values[i-1]);
	}


	// If the reset button has been pressed, destroy all the divs and layers created by the javascript
	if( operation == 'reset' ){
		// remove cases
		for( i=1; i <= number_of_cases; i++){
			casesDiv.removeChild(document.getElementById('case' + i));
		}
		// remove values
		for(i=1; i<= number_of_cases / 2; i++){
			leftDiv.removeChild(document.getElementById('value' + i));
		}
		for(i=number_of_cases/2+1; i<=number_of_cases; i++){
			rightDiv.removeChild(document.getElementById('value' + i));
		}
		message.innerHTML = '';
		overlay.innerHTML = '';
		overlay.style.display = 'none';
	}

	// Now create the elements
	caseHTML();
	valueHTML();

	// Assign event handlers to each case so that they know what to do when clicked
	for(i = 1; i <= number_of_cases; i++){
		document.getElementById('case' + i).addEventListener( 'click', function(ev){
			// No clicks if this case has already been used.
			if( this.className != 'usedcase' ){
				this.className = 'usedcase';

				objCase = eval(this.id); // Change the element's id to the object reference.

				openCase(objCase);
			}

		}, false);
	}

	// Set up the welcome screen
	overlay.className = 'offer';
	overlay.innerHTML = eval('welcome_' + setting['gameType']);
	overlay.style.display = '';

	//depricated... now there is a "begin" button
	//setTimeout("overlay.style.display = 'none';", 2000);	
}





// The preference window
function preferences() {

	var tmp = "<br><br><form name='prefsForm'><table width='100%'>\n";

	tmp = tmp + "<tr><td><input type='radio' name='gameType' value='normal' id='normal'";
	if( setting['gameType'] == 'normal' ){ tmp = tmp + 'checked'; }
	tmp = tmp + "></td><td><label for='normal'>Normal Edition</label></td></tr>\n";

	tmp = tmp + "<tr><td><input type='radio' name='gameType' value='xmas' id='xmas'";
	if( setting['gameType'] == 'xmas' ){ tmp = tmp + 'checked'; }
	tmp = tmp + "></td><td><label for='xmas'>Christmas Edition</label></td></tr>\n";

	tmp = tmp + "<tr><td><input type='radio' name='gameType' value='quest' id='quest'";
	if( setting['gameType'] == 'quest' ){ tmp = tmp + 'checked'; }
	tmp = tmp + "></td><td><label for='quest'>Quest for a Million</label></td></tr>\n";

	tmp = tmp + "<tr><td><input type='checkbox' name='shuffle' value='1' id='shuf'";
	if( setting['shuffle'] ){ tmp = tmp + 'checked'; }
	tmp = tmp + "></td><td><label for='shuf'>Shuffle the amounts in the cases</label></td></tr>\n";

	tmp = tmp + "<tr><td><input type='checkbox' name='mysteryoffer' value='1' id='mystery'";
	if( setting['mysteryoffer'] ){ tmp = tmp + 'checked'; }
	tmp = tmp + "></td><td><label for='mystery'>Add Mystery Offer</label></td></tr>\n";

	tmp = tmp + "<tr><td colspan='2' align='center'><br><input type='button' value='Save Preferences' onClick='setPrefs();'> <input type='button' value='Cancel' onClick=\"prefs.style.display = 'none'\"></td></tr>\n";
	tmp = tmp + "</table></form>Warning, saving preferences will restart the game. Click Cancel if you wish to return without saving.";

	prefs.className = 'standard';
	prefs.innerHTML = 'Preferences' + tmp;
	prefs.style.display = '';
	rules.style.display = 'none'; // have to hide these.
}

// Save the preferences
function setPrefs( gameType, shuf ){
	for(i=0; i < document.prefsForm.gameType.length; i++){
		if( document.prefsForm.gameType[i].checked ){
			setting['gameType'] = document.prefsForm.gameType[i].value;
		}
	}
	setting['shuffle'] = document.prefsForm.shuffle.checked;
	setting['mysteryoffer'] = document.prefsForm.mysteryoffer.checked;

	prefs.className = 'offer';
	prefs.innerHTML = '<p>Settings Saved</p>';

	setTimeout("prefs.style.display = 'none';newgame('reset');", 1500);
}


// Display the rules
function showRules(){
	tmp = 'The Rules<br><br>\n';
	tmp = tmp + "<table><tr><td>This game closely follows the same rules as the hit television show, Deal or No Deal. You begin the game by selecting a case to hold on to. This becomes your case until you decide to sell it or keep it until the end of the game. Both of which will be described in a minute.<br><br>";
	tmp = tmp + "Once you have selected a case, you must pick 6 cases to open... what ever you find in those cases will be eliminated from the board and the banker will make you an offer based on the remaining amounts. This offer is to try to get you to sell your case.<BR><BR>";
	tmp = tmp + "If you sell your case, you win the amount of the offer. If you decide 'No Deal', then you keep your case and must open 5 more cases. This process repeats itself until there are only 2 cases left and each time, you will have 1 less case to open per turn.<BR><BR>";
	tmp = tmp + "If you continue until only your case and one other case is left, you will then be given the choice to open your case and win what is inside OR to switch your case with the last one in play and win what is in there.<br><br>";
	tmp = tmp + "Additional versions of the game as well and cheats/settings can be accessed using the preferences screen.<BR><BR>";
	tmp = tmp + "<center><strong>Game Types</strong></center><br>\n";
	tmp = tmp + "<u><strong>Normal Edition</strong></u>: Standard game. Cases go from $0.01 to $1,000,000.<BR><BR>\n";
	tmp = tmp + "<u><strong>Christmas Edition</strong></u>: The same as normal game except that the final values are much higher, for potentially higher winnings<br><br>\n";
	tmp = tmp + "<u><strong>Quest For a Million</strong></u>: This game starts out like a normal game BUT if you don't win the million, the next round starts with two $1,000,000 values. The game will continue to add $1,000,000 values for 6 rounds or until you win the million, which ever comes first. If you don't win after 6 rounds, the quest for a million begins again.<br><br>\n";
	tmp = tmp + "<center><strong>Game Options</strong></center><br>\n";
	tmp = tmp + "<u><strong>Shuffle Amounts</strong></u>: This is defaulted to on, so that the $ amounts are randomly placed in the cases. If cheating is your life, then turn this off to have the $ amounts placed into the cases in order.<br><br>\n";
	tmp = tmp + "<u><strong>Mystery Offer</strong></u>: The mystery offer is an offer from the bank that you don't get to see. The value of which can be anywhere between HALF of the normal offer to up to $100,000 higher than the normal offer. So making a deal could be a risk that pays off, or not. The mystery offer can happen at any time in the game. If you say no deal, you will be told what the offer was and what it would have been normally... so you know if you made a good choice or not.";
	tmp = tmp + "<center><a href='#' onClick=\"rules.style.display = 'none';\">Close Rules</a></center><BR></td></tr></table>";
	rules.innerHTML = tmp;
	rules.style.display = '';
}


function showUpdates(){
	tmp = 'Updates<br><br>\n';
	tmp = tmp + "<table>\n";
	tmp = tmp + "<tr><td><u><i>Mar 6, 2008</i></u>: Added Mystery Offer option to the preferences.</td></tr>\n";
	tmp = tmp + "<tr><td><u><i>Mar 4, 2008</i></u>: Added 'Quest for a Million' game type to the preferences.</td></tr>";
	tmp = tmp + "<tr><td><center><a href='#' onClick=\"rules.style.display = 'none';\">Close Updates</a></center><BR></td></tr></table>";
	rules.innerHTML = tmp;
	rules.style.display = '';
}







// Initialize the game on load
function init(){

	// Defaults to be set on initial load
	gameOver = 0;

	// Game settings
	setting = Array();
	setting['gameType'] = 'normal';
	setting['shuffle'] = true;

	// Set listeners
	document.getElementById('newgame_link').addEventListener('click', function(ev){ newgame('reset'); }, false);
	document.getElementById('preferences_link').addEventListener('click', function(ev){ preferences(); }, false);
	document.getElementById('rules_link').addEventListener('click', function(ev){ showRules(); }, false);
	document.getElementById('updates_link').addEventListener('click', function(ev){ showUpdates(); }, false);

	newgame('new');
}




// initialize the widget
if( document.all ){ callMethod = attachEvent; callLoad = 'onload'; callClick = 'onclick'; }
else { callMethod = addEventListener; callLoad = 'load'; callClick = 'click'; }

window.callMethod( callLoad , function(ev)
{
	init();
},false);