// =======================================================================================================
// WEATHERHEAD Experience Design Group LLC
// Module:      game.js
// Version:     1.1.0
// Build Date:  10/9/03
// Developer:	Jim L. Dixon
// Description: Handles all "concentration" game functionality for this presentation.
//
// Notes:
// - 
// Copyright (c) 2003, WEATHERHEAD Experience Design Group LLC
// =======================================================================================================

var symbols = new Array("07_symbol01_1A_3C.jpg",									// Symbol file names; embedded row/col IDs match to associated symbol.
				  "07_symbol02_1B_4B.jpg",
				  "07_symbol03_1C_6C.jpg",
				  "07_symbol04_1D_4C.jpg",
				  "07_symbol05_1E_1F.jpg",
				  "07_symbol06_2A_5E.jpg",
				  "07_symbol07_2B_4D.jpg",
				  "07_symbol08_2C_5A.jpg",
				  "07_symbol09_2D_5C.jpg",
				  "07_symbol10_2E_6F.jpg",
				  "07_symbol11_2F_3A.jpg",
				  "07_symbol12_3B_3D.jpg",
				  "07_symbol13_3E_6B.jpg",
				  "07_symbol14_3F_5B.jpg",
				  "07_symbol15_4F_5D.jpg",
				  "07_symbol16_4E_6A.jpg",
				  "07_symbol17_4A_6D.jpg",
				  "07_symbol18_5F_6E.jpg");
var imgSymbol = new Array();														// Cached symbol images.
var imgDefn = new Array();															// Cached symbol definitions.
var symPanel = new Array("", "Sym1", "Sym2", "Sym3", "Sym4", "Sym5", "Sym6");		// Names of symbol definition images.
var FlipID = new Array();															// Object IDs for currently selected cards.

var iSymCount = 0;																	// Number of visible symbol definitions.
var iCurrSym;																		// Current symbol array offset being displayed.
var iFoundCount = 0;																// Number of cards matched.
var iFoundGoal = 36;																// Number of cards to match.
var iFlipCount = 0;																	// Number of cards currently flipped.
var bVerify = false;																// True = verifying if cards are matching, False = less than 2 cards showing.
var bGameShowing = true;															// True = game board is showing, False = symbol definitions showing.

var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);	// True = Netscape 4.x, False = Newer Netscape and all IE.

function preloadImages() {
	// Preloads all images that aren't loaded initially.

	for (var i = 0; i < symbols.length; i++) {										// Look through all image file names.
		imgSymbol[i] = new Image();
		imgDefn[i] = new Image();
		imgSymbol[i].src = "Images/" + symbols[i];									// Load symbol card.
		imgDefn[i].src = "Images/" + symbols[i].slice(0, 12) + "definition.jpg";	// And the definition image.
	}

	imgDefn[symbols.length] = new Image();											// Also load the congratulations screen.
	imgDefn[symbols.length].src = "Images/07_text_congrat.jpg";
}

function findElement(n, d) {
	// Find a document element regardless of browser that supports some form of the DOM.

    var p,i,x;  

	if(!d) d=document;

	if((p=n.indexOf("?"))>0&&parent.frames.length) {
      d=parent.frames[n.substring(p+1)].document; 
	  n=n.substring(0,p);
	}

    if(!(x=d[n])&&d.all) x=d.all[n]; 
	
	for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];

    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findElement(n,d.layers[i].document); 

    if(!x && document.getElementById) x=document.getElementById(n); 
	
	return x;
}

function showElement(e) {
	// Shows in Netscape and IE browser DOMs.

	e = findElement(e);

	if (NS4) {
		e.visibility = 'show';
	} else {
		e.style.visibility = 'visible';
	}
}

function hideElement(e) {
	// Hides in Netscape and IE browser DOMs.

	e = findElement(e);

	if (NS4) {
		e.visibility = 'hide';
	} else {
		e.style.visibility = 'hidden';
	}
}

function flipCard(id) {
	// Determines card flip functionality.
	// Overloads use of 'id' for filename and for image object name and because the filename has the two card positions that are matched, we can 
	//   compare the filenames used to see if the two cards are the same.

	if ((bVerify) || (iFlipCount == 1 && FlipID[1] == id)) return;					// No new card flips until a pair is verified.

	for (var i = 0; i < symbols.length; i++) {										// Find image filename that contains this card ID in its name.
		if (symbols[i].indexOf(id) != -1) {											// Found it so show it.
			findElement(id).src = imgSymbol[i].src;									// Show associated symbol image.
			iCurrSym = i;															// Save symbol number in case we get a match.
			FlipID[++iFlipCount] = id;												// Increment card count and save value into ID array.
			break;
		}
	}

	if (iFlipCount == 2) {															// We have two cards showing so do a verification.
		bVerify = true;																// Stop other card flips from occurring until verify is done.
		setTimeout("flipCardFinish()", 500);
	}
}

function flipCardFinish() {
	// Time delay to allow both visible cards to show before removing or flipping back over.

	var e1 = findElement(FlipID[1]);												// Handles to 2 cards we are evaluating.
	var e2 = findElement(FlipID[2]);

	if (e1.src == e2.src) {															// We have a match if both use the same image source.
		// Fix all names to point to the DIV elements (ie. GameXX) so we can hide the whole thing, link and all.
		for (var i = 1; i < 3; i++) if (FlipID[i].length == 2) FlipID[i] = "Game" + FlipID[i];

		hideElement(FlipID[1]);														// Hide panels.
		hideElement(FlipID[2]);
		showNextSymbolDef();														// Show matching symbol definition on left side.

		iFoundCount += 2;															// 2 more cards matched.
		if (iFoundCount == iFoundGoal) setTimeout("flipCardFinal()", 1500);			// Found all of matches so small delay to show final congrats.
	} else {																		// Not matched.
		e1.src = "Images/07_symbol_back.jpg";										// Flip cards back.
		e2.src = "Images/07_symbol_back.jpg";
	}

	iFlipCount = 0;																	// Reset card count either way.
	bVerify = false;																// No longer verifying so new card flips can occur.
}

function showNextSymbolDef(id) {
	// Show the matching symbol on the left area, replacing the instructions image.

	hideElement("txtPanel");														// Hide instructions column.

	if (++iSymCount > 6) {															// Increment symbols showing on screen and slide all up by one if needed.
		for (var i = 2; i < 7; i++) findElement(symPanel[i-1]).src = findElement(symPanel[i]).src;
		iSymCount = 6;																// Leave symbol panel count at the max.
	}

	findElement(symPanel[iSymCount]).src = imgDefn[iCurrSym].src;					// Load new image into correct panel.
}

function flipCardFinal() {
	// All cards have been matched so show the final congratulations text.

	findElement("TextInfo").src = imgDefn[symbols.length].src;						// Pull congrats screen from cache.

	for (var i = 1; i < 7; i++) hideElement(symPanel[i]);							// Hide symbol list.

	showElement("txtPanel");														// Show contrats text.
}

