<!-- 
// Magic Crystal Ball
// JavaScript copyright (c) 2000-2002 by Karen M. Glatt
// This game is not freeware nor is it in the public domain.
// If you would like to use this game on your web site, 
// contact KMG Associates at www.kmgassociates.com.


// global variables
var numCards = 5;			// cards displayed in ball
var cards = new Array(1, 2, 4, 8, 16);
var currentCard = -1;		// index into cards array
var numBallMsgs = 10;		// number of msgs from ball
var ballMsgs = new Array(numBallMsgs);
var currentBallMsg = -1;		// index into ballMsgs array
var playersNumber = 0;		// will keep track of player's total
var state = 0;				// finite state machine

// Displays a card with numbers in the crystal ball.
function DisplayCard()
{
    document.images.imgballmiddle.src = eval('"card' + cards[currentCard] + '.jpg"');
}

// Displays a msg below the crystal ball.
function DisplayBallMsg()
{
    document.images.imgballmsg.src = eval('"bmsg' + ballMsgs[currentBallMsg] + '.gif"');
}

// Displays a msg revealling the player's number.
function DisplayPlayersNumber()
{
    document.images.imgballmiddle.src = eval('"num' + playersNumber + '.jpg"');
}

// Fake out the player by mixing up the order of the cards
// each time they play.
function ShuffleCards()
{
    var i, n, x1, x2, temp;
    
    // shuffle the cards a random number of times to create different games
    n = Math.floor((Math.random() * numCards) + 50);
    for (i = 0;  i < n; i++)
        {
        x1 = Math.floor(Math.random() * numCards);
        x2 = Math.floor(Math.random() * numCards);
        if (x1 != x2)
            {
            temp = cards[x2];
            cards[x2] = cards[x1];
            cards[x1] = temp;
            }
         }
}

// The player will only see the first numCards of the ball
// msgs which are displayed below the crystal ball. Shuffle
// the msgs so they see a different set of quips each time they play.
function ShuffleBallMsgs()
{
    var i, n, x1, x2, temp;
    
    // shuffle the msg a random number of times to create different games
    n = Math.floor((Math.random() * numBallMsgs) + 50);
    for (i = 0;  i < n; i++)
        {
        x1 = Math.floor(Math.random() * numBallMsgs);
        x2 = Math.floor(Math.random() * numBallMsgs);
        if (x1 != x2)
            {
            temp = ballMsgs[x2];
            ballMsgs[x2] = ballMsgs[x1];
            ballMsgs[x1] = temp;
            }
         }
}

// Initialize the game
function InitGame()
{
    var i;

    // Reset the global variables
	playersNumber = 0;
    state = 0;

	// Shuffle the number cards.
	ShuffleCards();
	currentCard = -1;

	// Set up the ball msg array. We just need 0 to numBallMsgs
	// in the array to start, then shuffle them.
    for (i = 0; i < numBallMsgs; ++i)
        {
        ballMsgs[i] = i;
        }
	ShuffleBallMsgs();
	currentBallMsg = -1;

	// Put back the default images.
	document.images.imgballmiddle.src = "ballmid.jpg";
	document.images.imgballmsg.src = "bmsgstrt.gif";
	document.images.imgno.src = "butnoblk.gif";
}

// The user has clicked either the "yes" or "no" buttons.
// This program is a finite state machine set up as follows:
// State 0: waiting for player to think of a number
//		no msg in crystal ball
//		ball msg is "think of a number"
//		only the yes button is displayed
// States 1-5: is player's number displayed in ball?
//		random number card displayed in crystal ball
//		random ball msg is displayed below crystal ball
//		both yes and no buttons active
// State 6: player's number is revealed
//		number is displayed in crystal ball
//		"try again?" msg displayed below crystal ball
//		only the yes button is displayed.
function ChangeState(yesFlag)
{

	// Did the user click "No" instead of "Yes" to start?
	// If so, ignore the click.
	if ((state == 0) && !yesFlag)
		return false;

	// If this is the transition between "Pick at number"
	// and "look at the cards", display the no button.
	if (state == 0)
		document.images.imgno.src = "butno.gif";

	// If the user was looking at a card and clicked "Yes",
	// update the running total for his secret number.
	if (yesFlag && (state >= 1) && (state <=5))
		playersNumber += cards[currentCard];

	// Show player next card & msg
	if ((state >= 0) && (state <= 4))
		{
		++currentCard;		// starts w/-1
		DisplayCard();
		++currentBallMsg;	// starts w/-1
		DisplayBallMsg();
		}

	// If the player has seen all 5 cards, reveal his number
	// and ask if he wants to play again.
	if (state == 5)
		{
		DisplayPlayersNumber();
		document.images.imgballmsg.src = "bmsgnew.gif";
		document.images.imgno.src = "butnoblk.gif";
		}

	// No matter what the player clicked, reinit the game.
	if (state == 6)
		{
		InitGame();
		return false;	
		}

	++state;
	return false;
}
// End JavaScript -->
