<!--

// Copyright 2000 A Walk Beyond, LLC.  All rights reserved.

// Class Context

function Context()
{
	this.imageDir = "../../images/games/images/";
	this.animDir = "../../images/games/animations/";

	this.maxGroupSize = 5;
	this.maxIncorrect = 10;
	this.defaultGroupSize = 5;
	this.current = 0;
	this.incorrect = 0;
	this.remaining = 0;

	this.bufferId = 0;
	this.bufferLoaded = false;
	this.buffer = new Image();
	this.buffer.src = this.imageDir + "preload_570.gif";
	
	this.catalog = new Array();
	this.group = null;

	this.domPreload = null;
	this.domInstruct = null;
	this.domReward = null;
	this.domStar = null;
	this.domCurrentAnimal = null;
	this.domGageOn = null;
	this.domGageOff = null;
	this.domShowMap = null;
	this.domNextGroup = null;
	
	this.domWorldMap = null;
	this.domAnimations = new Array();

	this.Bind = Context_Bind;
	this.ToggleMap = Context_ToggleMap;
	this.ShowReward = Context_ShowReward;
	this.FlashReward = Context_FlashReward;
	this.ShowHelp = Context_ShowHelp;
	this.ShowMap = Context_ShowMap;
	
	this.IsCurrentInRegion = Context_IsCurrentInRegion;
	this.IsCurrentIdentified = Context_IsCurrentIdentified;
	this.SetCurrentIdentified = Context_SetCurrentIdentified;
	this.SetCorrect = Context_SetCorrect;
	this.TryAgain = Context_TryAgain;

	this.AddToCatalog = Context_AddToCatalog;
	this.NewGroup = Context_NewGroup;
	this.NextAnimal = Context_NextAnimal;
	this.ClickMap = Context_ClickMap;
	this.SetGage = Context_SetGage;
	
	this.Preload = Context_Preload;
	this.DisplayPreload = Context_DisplayPreload;
	this.NextBuffer = Context_NextBuffer;
	
	function Context_Bind()
	{
		this.domPreload = new DocObject("preload");
		this.domInstruct = new DocObject("instruct");
		this.domReward = new DocObject("reward");
		this.domStar = new DocObject("star");
		this.domWorldMap = new DocObject("worldmap");
		this.domCurrentAnimal = FindObject(document,"image_animal");
		this.domGageOn = FindObject(document,"gage_on");
		this.domGageOff = FindObject(document,"gage_off");
		this.domShowMap = new GraphicButton("showmap");
		this.domNextGroup = new GraphicButton("nextgroup");
		
		this.group = new Group(this.catalog);

		this.domShowMap.Bind();
		this.domShowMap.On();
		this.domNextGroup.Bind();

		for (var i = 0; i < this.maxGroupSize; i++) this.domAnimations[i] = FindObject(document, "animation_" + i);
	}
	
	function Context_ToggleMap()
	{
		if (this.domWorldMap && this.domShowMap)
		{
			this.domWorldMap.SetVisible(! this.domShowMap.state);
			this.domInstruct.SetVisible(this.domShowMap.state);
			this.domReward.SetVisible(! this.domShowMap.state)
			this.domShowMap.Toggle();
		}
	}
	
	function Context_IsCurrentInRegion ( sRegion )
	{
		if (this.group)
		{
			var sCurrentMemberName = this.group.GetMembersId(this.current);
			var oMember = this.group.members[sCurrentMemberName];

			for (var i in oMember.regions) if (oMember.regions[i] == sRegion) return true;
		}
		
		return false;
	}
	
	function Context_IsCurrentIdentified()
	{
		if (this.group)
		{
			var sCurrentMemberName = this.group.GetMembersId(this.current);
			return this.group.identified[sCurrentMemberName];
		}
		
		return false;
	}

	function Context_SetCurrentIdentified ( bValue )
	{
		if (this.group)
		{
			var sCurrentMemberName = this.group.GetMembersId(this.current);
			this.group.identified[sCurrentMemberName] = typeof(bValue) == "boolean" ? bValue : true;
		}
	}
	
	function Context_SetCorrect ( bValue )
	{
		if (typeof(bValue) == "boolean")
		{
			if (bValue == false)
			{
				this.incorrect++;
				this.incorrect > this.maxIncorrect ? this.TryAgain("restart") : this.TryAgain();
			}
			else if (this.IsCurrentIdentified() == false)
			{
				this.SetCurrentIdentified(true);
				this.remaining--;
				this.remaining < 1 ? this.ShowReward(true) : this.NextAnimal();
			}
		}
	}
	
	function Context_TryAgain ( sMode )
	{
		if (typeof(sMode) == "string" && sMode == "restart")
		{
			this.NewGroup();
		}
		else
		{
			this.SetGage(this.incorrect / this.maxIncorrect * 100);
		}
	}

	function Context_AddToCatalog ( oAnimal )
	{
		var iCount = 0;
		if (oAnimal) this.catalog[oAnimal.name] = oAnimal;
		for (var i in this.catalog) iCount++;
		this.catalog.length = iCount;
	}
	
	function Context_NextBuffer()
	{
		var sMembersId;

		if (this.current < this.remaining)
		{
			sMembersId = this.group.GetMembersId(this.current);

			this.bufferLoaded = false;
			this.buffer.src = this.bufferId == 0 ? this.group.members[sMembersId].still : this.group.members[sMembersId].animation; 
			this.bufferId = this.bufferId == 0 ? 1 : 0;
			this.current++;

			this.Preload();
		}
		else
		{
			this.current = 0;
			this.bufferLoaded = true;
			this.bufferId = 0;

			sMembersId = this.group.GetMembersId(this.current);

			this.domCurrentAnimal.src = this.group.members[sMembersId].still;
			this.domAnimations[this.current].src = this.group.members[sMembersId].animation;
			
			this.DisplayPreload(false);
		}
	}
	
	function Context_DisplayPreload ( bShow )
	{
		this.domPreload.SetVisible(bShow); this.domPreload.SetOrder(bShow ? 9 : 1);
	}

	function Context_Preload()
	{
		this.DisplayPreload(true);
		
		if (this.bufferLoaded || nTimeoutCount > 20)
		{
			nTimeoutCount = 0;
			this.DisplayPreload(false);
			return;
		}

		if (navigator.appName == "Netscape")
		{
			if (this.buffer.complete)
			{
				this.NextBuffer();
			}
			else
			{
				nTimeoutCount++;
				window.setTimeout("oContext.Preload()", 500);
			}
		}
		else
		{
			if (this.buffer.readyState == "complete")
			{
				this.NextBuffer();
			}
			else
			{
				nTimeoutCount++;
				window.setTimeout("oContext.Preload()", 500);
			}
		}
	}

	function Context_NewGroup()
	{
		this.current   = 0;
		this.incorrect = 0;
		this.bufferId = 0;
		this.bufferLoaded = false;
		this.remaining = this.defaultGroupSize;
		this.maxIncorrect = 2 * this.defaultGroupSize;
		this.group.Populate(this.defaultGroupSize);
		this.ShowMap(true);
		this.SetGage(1);
		
		this.Preload();
	}

	function Context_NextAnimal()
	{
		this.current++;
		if (this.current >= this.defaultGroupSize) this.current = 0;
		var sMembersId = this.group.GetMembersId(this.current);

		this.domCurrentAnimal.src = this.group.members[sMembersId].still;
		this.domAnimations[this.current].src = this.group.members[sMembersId].animation;
	}		

	function Context_FlashReward( nMilliseconds)
	{
		this.domStar.SetVisible(true); this.domStar.SetOrder(9);
		window.setTimeout ("oContext.domStar.SetVisible(false); oContext.domStar.SetOrder(0);", nMilliseconds);
	}

	function Context_ShowReward ( bShow )
	{
		if (typeof(bShow) == "boolean")
		{
			if (navigator.appName == "Netscape" && bShow)
			{
				this.FlashReward(4000);
				return;
			}

			if (bShow) this.FlashReward(4000);
			this.domReward.SetVisible(bShow);
			this.domWorldMap.SetVisible(! bShow);
			this.domInstruct.SetVisible(! bShow);
			bShow ? this.domShowMap.On() : this.domShowMap.Off();
		}
	}
	
	function Context_ShowHelp ( bShow )
	{
		if (typeof(bShow) == "boolean")
		{
			this.ShowReward(false);
			this.domWorldMap.SetVisible(! bShow);
			this.domInstruct.SetVisible(bShow);
			bShow ? this.domShowMap.On() : this.domShowMap.Off();
		}
	}
	
	function Context_ShowMap ( bShow )
	{
		if (typeof(bShow) == "boolean")
		{
			this.ShowReward(false);
			this.domWorldMap.SetVisible(bShow);
			this.domInstruct.SetVisible(! bShow);
			bShow ? this.domShowMap.On() : this.domShowMap.Off();
		}
	}
	
	function Context_SetGage ( nPercent )
	{
		if (this.domGageOn && this.domGageOff)
		{
			var iPercent = NormalizeInRange(nPercent, 1, 99);

			this.domGageOn.height = "" + iPercent + "%";
			this.domGageOff.height = "" + (100 - iPercent) + "%";
		}
	}

	function Context_ClickMap ( sIndex )
	{
		if (this.remaining < 1)
		{
			this.NewGroup();
		}
		else if (this.IsCurrentInRegion(sIndex))
		{
			if (this.IsCurrentIdentified() == false)
			{
				this.SetCorrect(true);
			}
		}
		else
		{
			this.SetCorrect(false);
		}
	}
}


// Class Animal

function Animal ( oCurrentContext, sName, sPrimaryRegion )
{
	this.name = sName;
	this.still = oCurrentContext.imageDir + "st_" + this.name + ".gif";
	this.animation = oCurrentContext.animDir + "ag_" + this.name + ".gif";
	this.regions = new Array();

	this.AddRegion = Animal_AddRegion;
	this.LivesInRegion = Animal_LivesInRegion;
	
	this.AddRegion(sPrimaryRegion);
	
	function Animal_AddRegion ( sRegion )
	{
		this.regions[this.regions.length] = sRegion;
	}
	
	function Animal_LivesInRegion ( sRegion )
	{
		for (var i = 0; i < this.regions.length; i++) if (this.regions[i] == sRegion) return true;
		return false;
	}
}


// Class Group

function Group ( oCatalog )
{
	this.members = new Array();
	this.identified = new Array();
	this.catalog = oCatalog;
	this.maxIterations = 50;

	this.AddMember = Group_AddMember;
	this.GetMembersId = Group_GetMembersId;
	this.GetCatalogLength = Group_GetCatalogLength;
	this.GetCatalogId = Group_GetCatalogId;
	this.Populate = Group_Populate;

	function Group_AddMember ( oAnimal )
	{
		var iCount = 0;

		this.members[oAnimal.name] = oAnimal;
		this.identified[oAnimal.name] = false;
		for (var i in this.members) iCount++;
		this.members.length = iCount;
		this.identified.length = iCount;
	}

	function Group_GetMembersId ( nIndex )
	{
		var iCount = 0;
		for (var o in this.members) { if (nIndex == iCount) return o; iCount++; }
		return null;
	}

	function Group_GetCatalogLength()
	{
		var iCount = 0;
		for (var i in this.members) iCount++;
		return iCount;
	}
		
	function Group_GetCatalogId ( nIndex )
	{
		var iCount = 0;
		for (var o in this.catalog) { if (nIndex == iCount) return o; iCount++; }
		return null;
	}

	function Group_Populate ( nCount )
	{
		var oCandidate;
		var iCount = 0;
		var jCount = 0;

		this.members = null;
		this.identified = null;
		
		this.members = new Array();
		this.identified = new Array();
		
		while (jCount < nCount && iCount < this.maxIterations)
		{
			oCandidate = this.catalog[this.GetCatalogId(RandomInRange(0,this.catalog.length-1))];
			this.members[oCandidate.name] = oCandidate;
			this.identified[oCandidate.name] = false;
			jCount=0; for (var i in this.members) jCount++;
			this.members.length = jCount;
			iCount++;
		}
	}
}

// -->

