<!--
function writeImageForSpacing()
{
	if (IE4 || NS4)
	{
		document.write('<img src="../images/pixel.gif" width="1" height="' + BOARD_TOTAL_HEIGHT + '">');
		document.write('<p>');
	}
}

// To ensure page is fully loaded:
// IE4 version reacts to the onreadystatechange, calls fnStartInit()
// NS4 version reacts to the onLoad event (in the html file's body tag), calls readyToGo()
if (IE4)
{
	document.onreadystatechange=fnStartInit;
}
else if (NS4)
{
	// nothing here; look at readyToGo()
}
else if (GECKO)
{
	document.write('|'); // prime it? Why is it that some text has to be entered? Without this, get javascript errors.
}
else
{
	document.write("This game requires Internet Explorer 4+ or Netscape 4+ or Mozilla");
}

function fnStartInit()
{
	if (document.readyState=="complete")
	{
		readyToPlay = true;
		showMessageLabel(true, WELCOME_MSG);
		updateInfo();
	}
}

function readyToGo()
{
	if (NS4 || GECKO)
	{
		readyToPlay = true;
		showMessageLabel(true, WELCOME_MSG);
		updateInfo();
	}
}

// * * * * * * * * * 
// layout setup

// buttons
for (var i=0; i<NUM_COLS; i++)
{
	writeButtons(i);
	makeSectionC("play"+ i, -1, i, true, '#ffffff', false);
}

rowOdd = true;
// pegs
for (var r=0; r<NUM_ROWS; r++)
{
	var COLS = (rowOdd) ? NUM_COLS : NUM_COLS - 1;
	for (var c=0; c<COLS; c++)
	{ 
		var section = "r" + r + "c" + c;
		writePegs(section);
		makeSection(section,r,c,rowOdd);
	}
	rowOdd = !rowOdd;
}

// last row - payout
for (var c=0; c<NUM_COLS; c++)
{
	var winningAmt = getWinningAmt(c);
	var winningAmtImage = findPicForAmt(winningAmt);

	var lastrow = "payout" + c;
	writeLastRow(winningAmtImage, SECTION_W, SECTION_H, lastrow);
	makeSectionC(lastrow, NUM_ROWS, c, true, winRowColor, false);
}

function findPicForAmt(amt)
{
	var picForAmt;
	if (amt==0)
	{
		picForAmt = "../images/plinko0.gif";
	}
	else if (amt==100)
	{
		picForAmt = "../images/plinko100.gif";
	}
	else if (amt==500)
	{
		picForAmt = "../images/plinko500.gif";
	}
	else if (amt==1000)
	{
		picForAmt = "../images/plinko1000.gif";
	}
	else if (amt==5000)
	{
		picForAmt = "../images/plinko5000.gif";
	}
	//else
	//{
	//	return amt;
	//}

	//picForAmt = "<img src='" + picForAmt + "' width=" + SECTION_W + " height=" + SECTION_H + " border=0>";

	return picForAmt;
}

// chip
writeChip();
setChipProperties();

// borders
writeBorders();
setBordersProperties();

// results
writeResultLabels();
setResultLabelsProperties();

// messages
writeMessageLabel();
setMessageLabelProperties();

function makeSection(section, r, c, rOdd)
{	
	makeSectionC(section, r, c, rOdd, '#cccccc', true);
}

/*
 * makeSectionC
 * param section - name of object
 * param r - row number
 * param c - column number
 * param rOdd - if the row is odd (as opposed to even)
 * param clr - color
 * param isPeg - if this is a peg (the pegs and sections both use this function)
 */
function makeSectionC(section, r, c, rOdd, clr, isPeg)
{
	var rowOfEvenAdj = (rOdd) ? 0 : SECTION_W / 2;

	var baseLeft = BOARD_LEFT + (c * SECTION_W) + rowOfEvenAdj;
	var baseTop = BOARD_TOP + (r * SECTION_H);

	setSectionCProperties(section, baseLeft, baseTop, clr, isPeg);
}

// * * * * * * * * * 
// action logic

function play(col)
{
	if (!readyToPlay)
	{
		return;
	}
	readyToPlay = false;

	showMessageLabel(false, "");

	nextChip();

	currentPosC = col;
	currentPosR = 0;
	var chipLeft = BOARD_LEFT + ((SECTION_W / 2) - (CHIP_WH / 2)) + (currentPosC * SECTION_W);
	var chipTop = BOARD_TOP - CHIP_WH;

	setChipLeft(chipLeft);
	setChipTop(chipTop);

	rowOdd = true; // starting point is a line with five columns

	bouncingUP = true;
	bounceCount = STEPS_UP;

	var pauseS = setTimeout("startDropping()", PAUSE_START);
}

function nextChip()
{
	numChipsCount++;
	updateInfo();
}

function updateInfo()
{
	winningsText = WINNINGS_PREFIX + gameTotalWinnings + WINNINGS_SUFFIX;
	setWinningsText();

	var chipsPic = "";
	for (var i=0; i<(NUM_CHIPS-numChipsCount); i++)
	{
		chipsPic += "* ";
	}

	setChipsRemainingText(chipsPic);
}

function startDropping()
{
	// initial little drop
	moveChipTop(CHIP_WH);

	startOfGame = false;

	var pauseSD = setTimeout("animation()", PAUSE);
}

function animation()
{
	incrY = (bouncingUp) ? (-bounceCount * adjustmentY) : (bounceCount * adjustmentY);

	if (bouncingUp)
	{
		if (bounceCount==STEPS_UP) // first step, need to pick x direction and go to next level
		{
			nextLevel();
		}
		bounceCount--;
	}
	else
	{
		bounceCount++;
	}

	
	if ((!bouncingUp) && (bounceCount >= STEPS_DOWN)) // move to next level
	{
		bouncingUp = true;
		bounceCount = STEPS_UP;

		setChipLeft(endpointX);
		setChipTop(endpointY);

		if (currentPosR >= NUM_ROWS) // done
		{
			gameTotalWinnings += getWinningAmt(currentPosC);
			
			if (numChipsCount+1 > NUM_CHIPS)
			{
				numChipsCount = 0;
				updateInfo(); // update before showing message, to get latest winningsText
				showMessageLabel(true, GAME_OVER + winningsText);
				gameTotalWinnings = 0;
			}
			else
			{
				updateInfo();
			}
			readyToPlay = true;
			return;
		}
	}
	else
	{
		moveChipLeft(incrX);
		moveChipTop(incrY);

		if (bounceCount <= 0)
		{
			bouncingUp = false;
		}
	}

	var pauseAnim = setTimeout("animation()", PAUSE);
}

function nextLevel()
{
	// pick way to move (left or right) for x,y positioning
	var wtm = wayToMove(currentPosC, rowOdd);
	incrX = wtm * (DISTANCE_X / (STEPS_DOWN+STEPS_UP));

	endpointX = getChipLeft() + (wtm * SECTION_W / 2);
	endpointY = getChipTop() + SECTION_H;

	incrY = (-bounceCount * adjustmentY); // starting pt for a bounce, so direction is negative

	currentPosC += direction;
	currentPosR++;

	rowOdd = !rowOdd;
}

function wayToMove(c, rOdd)
{
	if (rOdd)
	{
		if (c<=0)
		{
			direction = RIGHT_ODD;
			return (RIGHT);
		}
		else if (c>=4)
		{
			direction = LEFT_ODD;
			return (LEFT);
		}
	}
	var rand = getRandom(2);

	direction = (rand) ? ((rOdd) ? LEFT_ODD : LEFT_EVEN) : ((rOdd) ? RIGHT_ODD : RIGHT_EVEN);

	var dirMove = (rand) ? LEFT : RIGHT;
	return (dirMove);
}

function getRandom(upperRange)
{
	var rand = Math.random();
	return (Math.floor(rand*(upperRange)));
}

function getWinningAmt(columnX)
{
	// the calculation for the amount to win is optimized for 9 columns
	var refPt = Math.floor(NUM_COLS / 2);
	var winAmt = 0;

	if (columnX==refPt) {	winAmt = 5000; winRowColor = "#FF0000"; }
	else if (columnX==refPt-1 || columnX==refPt+1) { winAmt = 0; winRowColor = "#900000"; }
	else if (columnX==refPt-2 || columnX==refPt+2) { winAmt = 1000; winRowColor = "#FF0000"; }
	else if (columnX==refPt-3 || columnX==refPt+3) { winAmt = 500; winRowColor = "#900000"; }
	else if (columnX==refPt-4 || columnX==refPt+4) { winAmt = 100; winRowColor = "#FF5A13"; }

	return (winAmt);
}

//-->
