var coyote1,coyote2,snake,road1,road2,road3,road4,starting,fins,message; var prevRow=2; var flicker; var speed=50; var level=0,timer=0,lives=3,score=0; var playerLeft,playerRight; var plantC1,plantC2; var player,theTimeout; var timerTab,scoreTab; var maintimer,seconds; var counter,starter; var multiplier=2,leveler=5; var animCounter; var holes; var moves=50; var lastmove; var holeStart,holeEnd; var endGame=1; var gameStart,newrow; playerwidth=23;
var pPos=0; var totalHeight=250; totalLength=370; var jumps=9; var steps=3;
var loc; var logNames=["road4","road3","road2","road1","coyote2","coyote1","snake"];
var widths=[27,27,67,34,38,38]; var holePos=[164,250,333,418];
var imgs = new Array(), current=0;

	
	
function so_init() {
	imgs = document.getElementById("imageContainer").getElementsByTagName("img");
	for(i=1;i<imgs.length;i++) imgs[i].xOpacity = 0;
	imgs[0].style.display = "block";
	imgs[0].xOpacity = .99;
	
	setTimeout(so_xfade,3000);
}

function so_xfade() {
	cOpacity = imgs[current].xOpacity;
	nIndex = 1;
		nOpacity = imgs[nIndex].xOpacity;
	cOpacity-=.05; 
	nOpacity+=.05;
	
	imgs[nIndex].style.display = "block";
	imgs[current].xOpacity = cOpacity;
	if(current!=nIndex)
		imgs[nIndex].xOpacity = nOpacity;
	
	setOpacity(imgs[current]); 
	setOpacity(imgs[nIndex]);
	
	if(cOpacity<=0) {
		imgs[current].style.display = "none";
		setTimeout("LoadImages()",1000);
	} else {
		setTimeout(so_xfade,50);
	}
	
	function setOpacity(obj) {
		if(obj.xOpacity>.99) {
			obj.xOpacity = .99;
			return;
		}
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
	}
	
}
function Shuffle(max){
var num=Math.random()*max;
return Math.round(num)+1;
}
function Reset()
{
	var i;
	for(i=0;i<28;i++)
	loc[i]=1;
}
function MM_preloadImages() { 
  var d=document; if(d.images){ 
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i]; };for (i = 0; i < d.MM_p.length; i++) { 
		loaded[i] = false
	} document.getElementById("coyote1").innerHTML="<span style='position:relative; top:45px; left:80px;' class='LoadingAlert'>Loading Game...";checkLoad();}
}
function checkLoad() {
	if (currCount == document.MM_p.length) {
		so_init(); 
		return
	}
	for (i = 0; i <= document.MM_p.length; i++) {
		if (loaded[i] == false && document.MM_p[i].complete) {
			loaded[i] = true
			eval("document.img" + currCount + ".src=dots[1].src")
			currCount++
		}
	}
	timerID = setTimeout("checkLoad()",10) 
}

function LoadingGame()
{
	MM_preloadImages('images/GameBK.png','images/Game_ArrowLft.png','images/Game_ArrowRt.png','images/Game_ArrowUp.png','images/Game_ArrowDown.png','images/Game_Score.png','images/ArmadilloAnim1.gif','images/ArmadilloAnim2.gif','images/ArmadilloAnim3.gif','images/ArmadilloAnim4.gif','images/bike1.gif','images/bike2.gif','images/carA1.gif','images/carA2.gif','images/carB1.gif','images/carB2.gif','images/coyote1.gif','images/coyote2.gif','images/snake1.gif','images/snake2.gif','images/truck1.gif','images/truck2.gif','images/cave1.png','images/cave2.png','images/cave3.png','images/cave4.png','images/deadDown.gif','images/deadLeft.gif','images/deadRight.gif','images/deadUp.gif','images/ArmadilloSplash.gif','images/ArmadilloGuide.jpg','images/spacer.gif');
	coyote1=document.getElementById("coyote1");	coyote2=document.getElementById("coyote2");
	snake=document.getElementById("snake");	fins=document.getElementById("home");
	road1=document.getElementById("road1"); road2=document.getElementById("road2");
	road3=document.getElementById("road3"); road4=document.getElementById("road4");
	message=document.getElementById("message");
	timerTab=document.getElementById("time"); scoreTab=document.getElementById("score");
	road1.style.overflow="hidden";
	player=document.createElement("img");	player.style.position="absolute";	player.src="images/ArmadilloAnim1.gif"; player.id="player";
	starting=document.getElementById("starting");  starting.appendChild(player); player.style.visibility="hidden";
	road1.style.overflow = "hidden";
	road1.style.position = "relative";
    road1.style.width = "344px";
    road1.style.height = "26px";
document.getElementById("imageContainer").innerHTML="<img src='images/ArmadilloSplash.gif' /><img src='images/ArmadilloGuide.jpg' />";
}
function RestartGame()
{
	coyote1.innerHTML=""; coyote2.innerHTML="";
	LoadImages();
	
}
function LoadImages()
{
	document.getElementById("arrowup").innerHTML="<img src='images/Game_ArrowUp.png' style='z-index:2;position:absolute;left:0px;top:72px;'/>";
	document.getElementById("arrowdown").innerHTML="<img src='images/Game_ArrowDown.png' style='z-index:2;position:absolute;left:0px;top:178px;' />";
	document.getElementById("arrowleft").innerHTML="<img src='images/Game_ArrowLft.png' style='z-index:2;position:absolute;left:0px;top:120px;' />";
	document.getElementById("arrowright").innerHTML="<img src='images/Game_ArrowRt.png' style='z-index:2;position:absolute;left:68px;top:120px;' />";
	document.getElementById("scoreboard").innerHTML="<img src='images/Game_Score.png' style='z-index:2;position:absolute;left:0px;top:0px;' />";
	document.getElementById("gamebg").style.backgroundImage="url(images/GameBK.png)";
	document.getElementById("imageContainer").innerHTML="";
	document.getElementById("imageContainer").style.width="0px";
	document.getElementById("imageContainer").style.height="0px";
	StartGame();
}
function StartGame()
{
	score=0; level=1; timer=0; lives=3;
	 counter=new Array(7); starter=new Array(7);
	loc=new Array(28); Reset(); animCounter=steps; holeStart=new Array(4); holeEnd=new Array(4); holes=new Array(4);
	holeStart[0]=160;holeStart[1]=240;holeStart[2]=320;holeStart[3]=400;
	holeEnd[0]=180;holeEnd[1]=260;holeEnd[2]=340;holeEnd[3]=425; document.getElementById("topmost").innerHTML="";
	for(var i=0;i<4;i++)
		holes[i]=0;
	NewTurn();
}
function NewTurn()
{
	 player.style.left="297px"; player.style.top="225px"; player.style.visibility="visible";
	 scoreTab.innerHTML=""+score; document.getElementById("level").innerHTML=""+level; timerTab.innerHTML=""+timer; 
	 seconds=0; timer=0; lastmove="Up"; document.getElementById("lives").innerHTML=""+lives; maintimer=0;
	 road1.innerHTML="";road2.innerHTML=""; road3.innerHTML="";road4.innerHTML="";coyote1.innerHTML=""; coyote2.innerHTML="";snake.innerHTML=""; player.src="images/ArmadilloAnim1.gif"; document.getElementById("cactus1").innerHTML=""; document.getElementById("cactus2").innerHTML="";pPos=0;
	 if(lives==0)
	 {
		message.innerHTML="<div class='Message' align='center'>GAME OVER!<br />Your Score: "+score+"<br /><a href='highscore.php'>Add to High Score</a><br/><a onclick='RestartGame()'>PLAY  AGAIN?</a>  <a href='moregames.php>More Games</a></div>";
 		return;
	 }
	 for(var i=0;i<7;i++)
		{counter[i]=starter[i]=1;}
		PlantObjects();
	message.innerHTML="<div align='center' class='Message'>GO!</div>";
	setTimeout("Starter()",2000);
}
function PlantObjects()
{
	var plant,pos1,pos2,plantPos1,plantPos2,i;
	plantPos1=Shuffle(200);
	plantPos2=Shuffle(200);
	while(Math.abs(plantPos1+plantPos2-200)<80)
		plantPos2=Shuffle(200);
	pos1=200-plantPos1;
	pos2=200-plantPos2;	
	plantC1=new Array(4);
	plantC2=new Array(4);
	for(i=0;i<2;i++)
	{
		plant=document.createElement("img");
		plant.src="images/cactus1.gif";
		plant.style.position="relative";
		document.getElementById("cactus1").appendChild(plant);
		plant.style.left=plantPos1+"px";
		plant=document.createElement("img");
		plant.src="images/rock1.gif";
		plant.style.position="relative";
		document.getElementById("cactus2").appendChild(plant);
		plant.style.left=pos1+"px";
	}
	plantC1[0]=136+plantPos1;//cactus
	plantC2[0]=136+plantPos1+2*25;//cactus
	plantC1[3]=136+pos1;//rock
	plantC2[3]=136+pos1+2*18;//rock
	plantC1[1]=136+plantPos2+2*18;//cactus
	plantC2[1]=136+plantPos2+2*18+2*25;//cactus
	plantC1[2]=136+pos2+2*25;//rock
	plantC2[2]=136+pos2+2*25+2*18;//rock
	for(i=0;i<2*level;i++)
	{
		plant=document.createElement("img");
		plant.src="images/cactus1.gif";
		plant.style.position="relative";
		document.getElementById("cactus2").appendChild(plant);
		plant.style.left=plantPos2+"px";
		plant=document.createElement("img");
		plant.src="images/rock1.gif";
		plant.style.position="relative";
		document.getElementById("cactus1").appendChild(plant);
		plant.style.left=pos2+"px";
	}
}
function Starter()
{
	message.innerHTML="";
	timerTab.innerHTML=""+timer;
	endGame=0; gameStart=1; newrow=0;
	Movement();
}
function Movement()
{
	if(endGame==0)
	{
	maintimer++; seconds++;
	if(seconds>=1000/moves)
	{
		timer++;
		timerTab.innerHTML=""+timer;
		seconds=0;
		if(timer==120)
		{
			Timeup();
			return;
		}
	}
	
	if(maintimer==parseInt((level*20)/multiplier)||gameStart==1)//add object
	{
		var newobj,row,obj,temp,temprow,test,objWidth;
		row=Shuffle(28); row--;test=row;
		while(loc[row]==0)
		{
			row++; row=row%28;
			if(test==row)
			{
				Reset();
				break;
			}
		}
		loc[row]=0;
		row=(row%7)+1;
		if(gameStart==1)
		{
			row=newrow+1; newrow=row;
			if(newrow==8)//this is the no. of row starting from 1 to 7, make this no. row+1 for the no of rows of initial traffic
				{
					gameStart=0;
					Movement();
					return;
				}
				
		}
		obj=Shuffle(4);
		newobj=document.createElement("img"); newobj.style.position="absolute"; newobj.style.zIndex="1";
		objWidth=widths[obj-1];
		if(prevRow==row)
		row++;
		if(row>7)
			row=row-2;
		prevRow=row;
		if(row<5)
		switch(obj)
		{
			case 1: temp="carA";
					break;
			case 2: temp="carB";
					break;
			case 3: temp="truck";
					break;
			default: temp="bike";
					obj=4;
					break;
		}
		else
		switch(obj)
		{			
			case 1:
			case 2: temp="snake";
					objWidth=widths[4];
					break;
			case 3: 
			default: temp="coyote";
					objWidth=widths[5];
					obj=4;
					break;
		}
		switch(row)
		{
			case 1: temprow="road4";
					newobj.style.top="197px";
					break;
			case 2: temprow="road3";
					newobj.style.top="172px";
					break;
			case 3: temprow="road2";
					newobj.style.top="147px";
					break;
			case 4: temprow="road1";
					newobj.style.top="124px";
					break;
			case 5: temprow="coyote2";
					newobj.style.top="87px";
					break;
			case 6: temprow="coyote1";
					newobj.style.top="53px";
					break;
			default: temprow="snake";
					newobj.style.top="18px";
					row=7;
					break;
		}
		var pos;
		pos=row%2;
		var leftpos=80+(pos*390);
		newobj.style.left=""+leftpos+"px";
		newobj.style.zIndex="0"; newobj.style.width=objWidth+"px";
		pos++;
		newobj.src="images/"+temp+""+pos+".gif";
		newobj.id=row+"_"+counter[row-1];
		counter[row-1]++;
		counter[row-1]%=10;
		document.getElementById(temprow).appendChild(newobj);
		document.getElementById(temprow).style.overflow="hidden";
		maintimer=0;
	}
	var i,j,thestyle,lefttemp;
	for(i=1;i<8;i++)
	{
		if(i%2==0)
		{
			for(j=starter[i-1];j!=counter[i-1];j=(j+1)%10)
			{
				thestyle=document.getElementById(i+"_"+j);
				lefttemp=parseFloat(thestyle.style.left)+parseFloat(((leveler+level)*speed)/(10*leveler));
				thestyle.style.left=lefttemp+"px";
				if(parseInt(thestyle.style.left)>480)
				{
					document.getElementById(logNames[i-1]).removeChild(thestyle);
					starter[i-1]++;
					starter[i-1]%=10;
				}
			}
		}
		else
		{
			for(j=starter[i-1];j!=counter[i-1];j=(j+1)%10)
			{
				thestyle=document.getElementById(i+"_"+j);
				lefttemp=parseFloat(thestyle.style.left)-parseFloat(((leveler+level)*speed)/(10*leveler));
				thestyle.style.left=lefttemp+"px";
				if(parseInt(thestyle.style.left)<80)
				{
					document.getElementById(logNames[i-1]).removeChild(thestyle);
					starter[i-1]++;
					starter[i-1]%=10;
				}
			}
		}
	}
	if(pPos>0&&pPos<8)
	{
		var thestyle,temp1,temp2,temp3=pPos;
		for(j=starter[temp3-1];j!=counter[temp3-1];j=(j+1)%10)
			{
				thestyle=document.getElementById(temp3+"_"+j).style;
				temp1=parseInt(thestyle.left)+parseInt(thestyle.width);
				temp2=parseInt(thestyle.left)-playerwidth;
				if(parseInt(player.style.left)<temp1&&parseInt(player.style.left)>temp2&&temp2>0&&temp1>0)
				{ LostLife(); return; }
			}
	}
	theTimeout=setTimeout("Movement()",moves); 
	}
}

function MoveUp()
{
	if(endGame==0)
	{
	clearTimeout(theTimeout);
	maintimer=parseInt((level*20)/multiplier)-5;
	theTimeout=setTimeout("Movement()",moves);
	mov=(-1)*totalHeight/(jumps*steps);
	if(pPos>4)
		mov-=1.5;
	if(animCounter==steps) 
	{
		player.src="images/ArmadilloAnim1.gif";
		if(lastmove=="Left"||lastmove=="Right")
		{
			var temp=parseFloat(player.style.left)+5;
			player.style.left=temp+"px";
			temp=parseFloat(player.style.top)-5;
			player.style.top=temp+"px";
		}
		 lastmove="Up";
		if(chkMove("Up"))
		{
			animCounter=0; pPos++;	Animate("Up");
		}
	}
	}
}
function MoveDown()
{
	if(endGame==0)
	{
	clearTimeout(theTimeout);
	maintimer=parseInt((level*20)/multiplier)-5;
	theTimeout=setTimeout("Movement()",moves);
	mov=totalHeight/(jumps*steps);
	if(pPos>5)
		mov+=1.5;
	if(animCounter==steps) 
	{
		player.src="images/ArmadilloAnim3.gif";
		if(lastmove=="Left"||lastmove=="Right")
		{
			var temp=parseFloat(player.style.left)+5;
			player.style.left=temp+"px";
			temp=parseFloat(player.style.top)-5;
			player.style.top=temp+"px";
		}
		lastmove="Down";
		if(chkMove("Down"))
		{
			animCounter=0; pPos--;	Animate("Down");
		}
	}
	}
}
function MoveLeft()
{
	if(endGame==0)
	{
	clearTimeout(theTimeout);
	maintimer=parseInt((level*20)/multiplier)-5;
	theTimeout=setTimeout("Movement()",moves);
	mov=(-1)*totalLength/(jumps*steps);
	if(animCounter==steps) 
	{
		player.src="images/ArmadilloAnim4.gif";
		if(lastmove=="Up"||lastmove=="Down")
		{
			var temp=parseFloat(player.style.top)+5;
			player.style.top=temp+"px";
			temp=parseFloat(player.style.left)-5;
			player.style.left=temp+"px";
		}
		 lastmove="Left";
		 if(chkMove("Left"))
		 {
			 player.src="images/ArmadilloAnim4.gif";
			 animCounter=0;	Animate("Left");
		 }
	}
	}
}
function MoveRight()
{
	if(endGame==0)
	{
	clearTimeout(theTimeout);
	maintimer=parseInt((level*20)/multiplier)-5;
	theTimeout=setTimeout("Movement()",moves);
	mov=totalLength/(jumps*steps);
	if(animCounter==steps) 
	{
		player.src="images/ArmadilloAnim2.gif";
		if(lastmove=="Up"||lastmove=="Down")
		{
			var temp=parseFloat(player.style.top)+5;
			player.style.top=temp+"px";
			temp=parseFloat(player.style.left)-5;
			player.style.left=temp+"px";
		}
		 lastmove="Right";
		 if(chkMove("Right"))
		{
			animCounter=0;	Animate("Right");
		}
	}
	}
}
function chkMove(str)
{
	var topPos=parseFloat(player.style.top);
	var leftPos=parseFloat(player.style.left);
	var i,flag=0,cave,y;
	if((pPos==5&&str=="Up")||(pPos==6&&str=="Down"))
	{
		for(i=0;i<2;i++)
		{
			if((leftPos+playerwidth>plantC1[1+(i*2)])&&(leftPos<plantC2[1+(i*2)]))
				return 0;
		}
	}
	if((pPos==6&&str=="Up")||(pPos==7&&str=="Down"))
	{
		for(i=0;i<2;i++)
		{
			if((leftPos+playerwidth>plantC1[i*2])&&(leftPos<plantC2[i*2]))
				return 0;
		}
	}
	if(str=="Up")
	{
		if(topPos<30&&topPos>0)
		{
			for(i=0;i<4;i++)
			{
				if(leftPos>=holeStart[i]&&leftPos<=holeEnd[i]&&holes[i]==0)
				{
					flag=1; 
					y=i+1;
					cave=document.createElement("img");
					cave.style.position="absolute";	cave.style.left=holePos[i]+"px";
					cave.src="images/cave"+y+".png"; cave.style.top="0px";
					document.getElementById("topmost").appendChild(cave);
					break;
				}
			}
			if(flag==1)
				SuccessHole(i);
			return flag;
		}
		if(topPos<=0)
			return 0;
		return 1;
	}
	if((str=="Down")&&topPos<=210)
		flag=1;
	if((str=="Left")&&leftPos>=150)
		flag=1;
	if((str=="Right")&&leftPos<=430)
		flag=1;
	return flag;
}
function Animate(str)
{
	var temp;
	if(str=="Up"||str=="Down")
	{
		temp=parseFloat(player.style.top)+mov;
		player.style.top=temp+"px";
	}
	if(str=="Left"||str=="Right")
	{
		temp=parseFloat(player.style.left)+mov;
		player.style.left=temp+"px";
	}
	animCounter++;
	if(animCounter==steps)
		return;
	else
	setTimeout("Animate(\""+str+"\")",10);
}
function Timeup()
{
	message.innerHTML="<div class='Message' align='center'>OUT OF TIME. Try Again</div>";
	player.style.visibility="hidden";
	endGame=1;
	lives--;
	document.getElementById("lives").innerHTML=""+lives;
	setTimeout("NewTurn()",2000);
}
function LostLife()
{
	endGame=1;
	lives--;
	message.innerHTML="<div class='Message' align='center'>FAILED <br />LIVES: "+lives+"</div>";
	document.getElementById("lives").innerHTML=""+lives;
	flicker=0;
	DeadPlayer();
}
function DeadPlayer()
{
	var temp;
	temp="images/dead"+lastmove+".gif";
	if(flicker%2==0)
		player.src=temp;
	else
		player.src="images/spacer.gif";
	if(flicker>8)
	{
		player.style.visibility="hidden";
		NewTurn();
		return;
	}
	flicker++;
	setTimeout("DeadPlayer()",250);
}
function NextLevel()
{
	level++;
	player.style.visibility="hidden";
	document.getElementById("topmost").innerHTML="";
	endGame=1;
	for(var i=0;i<4;i++)
		holes[i]=0;
	if(level==5)
	{
		setTimeout("GameComplete()",2000);
		return;
	}
	message.innerHTML="<div class='Message' align='center'>LEVEL"+level+"</div>";
	setTimeout("NewTurn()",2000);
}
function SuccessHole(hole)
{
	message.innerHTML="<div class='Message' align='center'>SUCCESS</div>";
	player.style.visibility="hidden";
	endGame=1;
	holes[hole]=1;
	var flag=0;
	for(var i=0;i<4;i++)
	{
		if(holes[i]==0)
			{	flag=1;	break;	}
	}
	score+=(140-timer);
	scoreTab.innerHTML=""+score;
	if(flag==0)
		setTimeout("NextLevel()",2000);
	else
		setTimeout("NewTurn()",2000);
}
function GameComplete()
{
	message.innerHTML="<div class='Message' align='center'>GAME COMPLETE, CONGRATS!<br />Your Score: "+score+"<br /><a href='highscore.php'>Add to High Score</a><br/><a onclick='RestartGame()'>PLAY  AGAIN?</a><br/> <a href='moregames.php>More Games</a></div>";
}
