count=0;	//------------------------------------the counter to keep track of time and duration
frameActions="";	//----------------------------variable to keep track of whether a frame in the timeline is associated with and action and what action
imgToShow=0;	//--------------------------------variable that keeps track of the current image: array reference
currentStory=0;		//----------------------------variable that keeps track of the current story: array reference
adjustment=false;	//----------------------------variable to track if an adustment was made or not
audioplaylist="0*"; //-----------------------------the default play list
padding=15;	//------------------------------------amount of padding for the proximity detection.  In pixels
audioLoaded=false;//------------------------------default value for audioLoaded status
audioplaying=false;//-----------------------------default value for audioplaying status
refreshRate=1200; //------------------------------the duration in milliseconds to refresh the counter.  In milliseconds
debugMode=false;	//---------------------------- throws a debug window to receive data.  Change to false for off; true for on
if(debugMode==false){document.getElementById('msgArea').style.display = 'none';}else{document.getElementById('msgArea').style.display = 'block';}
function updateAvatarPosition(targetX,targetY)
{
/*
for(i=0;i<avatarArray.length-1;i++)
	{
	tempData = avatarArray[i]
	tempDataArray=tempData.split(",");
	tempX=tempDataArray[1];
	tempY=tempDataArray[0];
	if(tempX<(targetX+padding) && tempX>(targetX-padding) && tempY>(targetY-padding) && tempY<(targetY+padding) && adjustment==false)
		{
		count=i;	
		adjustment=true;
		setTimeout("adjustment=false;",1000);
		}
	}
	*/
	offPosData=findPosition('imgwin');
	offPosX=(curleft/2)-17; //Number(offPosData[1]);
	offPosY=(curtop/2)-17; //Number(offPosData[0]);
	
	
	
	tempAvatarX=Number(targetX);
	tempAvatarY=Number(targetY);
	
	avDataX=offPosX+tempAvatarX;
	avDataY=offPosY+tempAvatarY;
	document.getElementById('avatar').style.top=avDataY+"px";
	document.getElementById('avatar').style.left=avDataX+"px";
	document.getElementById('avatar').style.visibility="visible";
	
	/*
	document.getElementById('avatar').style.top=targetY+"px";
	document.getElementById('avatar').style.left=targetX+"px";
	document.getElementById('avatar').style.visibility="visible";
	*/
}
function updateAvatar(count)
{
	offPosData=findPosition('imgwin');
	offPosX=(curleft/2)-17; //Number(offPosData[1]);
	offPosY=(curtop/2)-17; //Number(offPosData[0]);
	avatarData = avatarArray[count];
	avatarDataArray=avatarData.split(",");
	tempAvatarX=avatarDataArray[1];
	tempAvatarX=Number(tempAvatarX);
	tempAvatarY=avatarDataArray[0];
	tempAvatarY=Number(tempAvatarY);
	avatarDataX=offPosX+tempAvatarX;
	avatarDataY=offPosY+tempAvatarY;
	document.getElementById('avatar').style.top=avatarDataY+"px";
	document.getElementById('avatar').style.left=avatarDataX+"px";
	document.getElementById('avatar').style.visibility="visible";
	//-----------------------------------------------------------------------------check to see where the avatar is, then compare to the target array
	//-----------------------------------------------------------------------------and load the appropriate audio file when there is a match of coords
		for(var z=0;z<targetArray.length-1;z++)
			{
				var tempTargetData=targetArray[z];
				var tempTargetDataArray = tempTargetData.split(",");
				var tempTargetX = tempTargetDataArray[0];
				var tempTargetY = tempTargetDataArray[1];
				tempTargetX=Number(tempTargetX)+Number(offPosX);
				tempTargetY=Number(tempTargetY)+Number(offPosY);
				if(audioplaylist.indexOf(z)>-1){audioLoaded=true;}else{audioLoaded=false;}
				//----------------------------------------------------------------------------debug info only shows when msgArea div is visible
				//document.getElementById("msgArea").innerHTML += "<strong>Audio Loaded:</strong>"+ audioLoaded +"<br />";
				//document.getElementById("msgArea").innerHTML = "<strong>Target Array Data:</strong>"+ tempTargetX +" X "+tempTargetY +"<br />";
				//document.getElementById("msgArea").innerHTML += "<strong>Avatar Data:</strong>"+ avatarDataX +" X "+avatarDataY +"<br />";
				//----------------------------------------------------------------------------end the debug info
				if(avatarDataX<(tempTargetX+padding) 
					&& avatarDataX>(tempTargetX-padding) 
					&& avatarDataY>(tempTargetY-padding) 
					&& avatarDataY<(tempTargetY+padding) 
					&& audioplaying==false 
					&& audioLoaded==false
					){
					document.getElementById("msgArea").innerHTML += "<strong>Audio Playing</strong>";
					audioplaying=true;
					var tempNumToPlay = z+1;
					playStory(tempNumToPlay);
					audioplaylist=audioplaylist+"*"+z;
					setTimeout("audioplaying=false;",20000);
					}//-------ends the if
			}//--------------ends the forloop
}
function updateFrame()
{
	count++;
	//updateAvatar(count);
	showImage(count);
}
function playStory(storyNum)
{
	tempStoryNum = storyNum; 
	currentStory=tempStoryNum;
	tempStoryFile = audioArray[tempStoryNum];
	document.getElementById('mp3win').src=tempStoryFile;
	tempImageData = imageArray[storyNum];
	imageToPlayArray = tempImageData .split("~");
	if(frameActions==""){setTimer();}
}
function showImage(count)
{
	if(count%4==0)
	{
	//document.getElementById.('msgArea').innerHTML = "showImage called";
	if(imgToShow>=imageToPlayArray.length-1){imgToShow = 0;}else{imgToShow++;}
	tempImgToShow = imageToPlayArray[imgToShow];
	document.getElementById('imageViewer').src = tempImgToShow;
	}
}
function setTimer()
{
	frameActions = setInterval("updateFrame()",refreshRate);
	findPosition('imgwin');
}
function clearTimer()
{
	clearInterval(frameActions);
	frameActions="";
}
function changeArrow(status,id)
{
if(status=='on')
	{
		document.getElementById(id).src = "images/"+id+"On.png";	
	}else{
		document.getElementById(id).src = "images/"+id+".png";	
	}
}
function findPosition(id)
{
var off=document.getElementById(id)
	if (off.offsetParent)
	{
	curleft = off.offsetLeft;
	curtop = off.offsetTop;
	while (off) 
		{
		curleft += off.offsetLeft;
		curtop += off.offsetTop;
		off=off.offsetParent;
		}
	}
return [curleft,curtop];
}

