//Screen Animation variables //moves:no. of moves to change screen  moves=15
var Img,Img2,timer,timer2,timer3,gameTimer,gameTimer2,afterAnim,lastTop,lastLeft,lastLeft2,the_style,movLeft,animType,screenWidth=320,moves=16,adsTimer,adsTimeSet=5;
//Game Variables  //hDirection 0-right 1-left; vDirection: 0-straight 1-up 2-down
//fireSpeed=300  fireLimit=20 (distance to drop)   objGap=6000    txtFade=10
var Submarine,TA,hDirection,vDirection,subSpeed,objSpeed,bonusSpeed,fireSpeed=600,subVar,objVar,fireCounter,fireLimit=15,fired,objCounter,objStart,objArray,objGap=7000,objFreq,objMap,totObjFreq,Lives,Score,shield,multiplier,shieldTimer,mulCounter,mulTimer,txtFade=15;
//Variables for Cookies
var expDays = 30;
var exp = new Date(); 
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
function Shuffle(max){
var num=Math.random()*max;
return Math.round(num)+1;
}
function AnimateScreen()
{
	the_style=Img.style;
	the_style2=Img2.style;
	the_style.left = parseFloat(the_style.left) + movLeft;
	the_style2.left=parseFloat(the_style2.left) + movLeft;
  	if((parseInt(the_style.left)<=lastLeft&&animType==0)||(parseInt(the_style.left)>=lastLeft&&animType==1))
	{
		the_style.left=lastLeft; 
		the_style2.left=lastLeft2;
		clearInterval(timer);eval(afterAnim);
		return;
	}
}
function adsTime()
{
	document.getElementById("adstime").innerHTML=adsTimer;
	if(adsTimer==0)
	{
		clearInterval(timer2);
		loadScreen();
		return;
	}
	adsTimer--;
}
function SkipAds()
{
	clearInterval(timer2);
	loadScreen();
}
function startGame()
{
	var container=document.getElementById("screen1");
	container.style.left="0px";
	lastLeft=-1*screenWidth;lastLeft2=lastLeft;
	Img=container; movLeft=-1*screenWidth/moves;
	Img2=document.getElementById("screen2");
	Img2.style.left="0px";
	afterAnim="";
	animType=0;
	// Frequency: 0-bomb; 1-bonus_life; 2-bonus_multiplier(2X); 3-bonus_multiplier(4X); 4-bonus_safe;
	objFreq=new Array(10,2,1,1,2);
	objMap=new Array();
	var i,j=0;
	for(i=0;i<5;i++)
	{
		while(objFreq[i]!=0)
		{
			objMap[j++]=i;
			objFreq[i]--;
		}
	}
	totObjFreq=j;
	Submarine=document.getElementById("Submarine");
	TA=document.getElementById("TA");
	timer=setInterval("AnimateScreen()",10);
}
function GoToMenu()
{
	document.getElementById("TXTBox").style.visibility="hidden";
	Img=document.getElementById("screen4");
	Img2=document.getElementById("screen2");
	var temp=-3*screenWidth;
	Img.style.left=temp+"px";
	movLeft=screenWidth/moves;
	afterAnim="";
	lastLeft=-2*screenWidth;lastLeft2=parseInt(Img2.style.left)+screenWidth;
	animType=1;
	timer=setInterval("AnimateScreen()",10);
}
function newGame()
{
	Img=document.getElementById("screen2");
	Img2=document.getElementById("screen3");
	var temp=-1*screenWidth;
	Img.style.left=temp+"px";
	Img2.style.left=temp+"px";
	movLeft=-1*screenWidth/moves;
	adsTimer=adsTimeSet-1;
	afterAnim="timer2=setInterval('adsTime()',1000);";
	lastLeft=-2*screenWidth;lastLeft2=lastLeft;
	animType=0;
	objArray=new Array(20);
	for(i=0;i<20;i++)
		objArray[i]=-1;
	hDirection=0; vDirection=0;
	subSpeed=250;
	//subSpeed=100; smaller number like 10 is really really slow
	objSpeed=120;
	bonusSpeed=50;
	objCounter=0; objStart=0;
	var temp2=GetCookie("TAgame");
	if((temp2=="")||(parseInt(temp2)==0))
		Score=0;
	else
	Score=parseInt(temp2);
	temp2=GetCookie("TA_Lives");
	if((temp2=="")||(parseInt(temp2)==0))
	{
		Lives=4;
		Score=0;
	}
	else
	Lives=parseInt(temp2);
	document.getElementById("score").innerHTML=Score;
	document.getElementById("lives").innerHTML=Lives;
	fired=0; shield=0; multiplier=1;
	document.getElementById("adstime").innerHTML=adsTimeSet;
	timer=setInterval("AnimateScreen()",10);
}
function loadScreen()
{
	Img=document.getElementById("screen3");
	Img2=document.getElementById("screen4");
	var temp=-2*screenWidth;
	Img.style.left=temp+"px";
	Img2.style.left=temp+"px";
	movLeft=-1*screenWidth/moves;
	afterAnim="loadScreen2()";
	lastLeft=-3*screenWidth;lastLeft2=lastLeft;
	animType=0;
	timer=setInterval("AnimateScreen()",10);
}
function loadScreen2()
{
	Submarine.style.top=120;
	Submarine.style.left=-20;
	Submarine.src="images/sub1_1.gif";
	TA.style.visibility="hidden";
	document.getElementById("objects").innerHTML="";
	for(i=0;i<20;i++)
		objArray[i]=-1;
	hDirection=0; vDirection=0;
	objCounter=0; objStart=0;
	gameTimer=setInterval("subAnim()",25);
	getObj();
}
function subAnim()
{
	var hMove,vMove,objMove,bonMove,i,len,obj;
	hMove=subSpeed/100;
	vMove=subSpeed/100;
	objMove=objSpeed/100;
	bonMove=bonusSpeed/100;
	if(hDirection==0)
	{
		Submarine.style.left=parseFloat(Submarine.style.left)+hMove;
		if(parseInt(Submarine.style.left)>318)
			Submarine.style.left=-70;
	}
	else
	{
		Submarine.style.left=parseFloat(Submarine.style.left)-hMove;
		if(parseInt(Submarine.style.left)<-70)
			Submarine.style.left=318;
	}
	if(vDirection==1)
	{
		if(parseInt(Submarine.style.top)>50)
			Submarine.style.top=parseFloat(Submarine.style.top)-vMove;
		else
			vDirection=0;
	}
	if(vDirection==2)
	{
		if(parseInt(Submarine.style.top)<320)
			Submarine.style.top=parseFloat(Submarine.style.top)+vMove;
		else
			vDirection=0;
	}
	i=objStart;
	while(i!=objCounter)
	{
		if(objArray[i]!=-1)
		{
			obj=document.getElementById("obstacle"+i);
			if(objArray[i]==0)
				obj.style.top=parseFloat(obj.style.top)-objMove;
			else
				obj.style.top=parseFloat(obj.style.top)-bonMove;
			if(parseInt(obj.style.top)<0)
			{
				document.getElementById("objects").removeChild(obj);
				objArray[i]=-1;
				objStart++;
				if(objStart>20)
					objStart=0;
			}
			//Collision detection
			if((parseInt(obj.style.top)+33>parseInt(Submarine.style.top))&&(parseInt(obj.style.top)<parseInt(Submarine.style.top)+70))//same vertically
			{
				if((parseInt(obj.style.left)+33>parseInt(Submarine.style.left))&&(parseInt(obj.style.left)<parseInt(Submarine.style.left)+75))//same horizontally
				{
					objCollide(i);
				}
			}
			//TA collision detection
			if(fired==1)
			{
				if((parseInt(obj.style.top)+33>parseInt(TA.style.top))&&(parseInt(obj.style.top)<parseInt(TA.style.top)+20))//same vertically
				{
					if((parseInt(obj.style.left)+33>parseInt(TA.style.left))&&(parseInt(obj.style.left)<parseInt(TA.style.left)+49))//same horizontally
					{
						TACollide(i);
					}
				}
			}
		}
		i++;
		if(i>20)
			i=0;
	}
}
function objCollide(i)
{
	var text="";
	var obj=document.getElementById("obstacle"+i);
	if(objArray[i]!=0)
	{
		document.getElementById("txtMsg").style.top=parseInt(obj.style.top);
		document.getElementById("txtMsg").style.left=parseInt(obj.style.left)+15;
	}
	switch(objArray[i])
	{
		case 1: text="1 UP";
				Lives++;
				document.getElementById("lives").innerHTML=Lives;
				document.getElementById("objects").removeChild(obj);
			break;
		case 2: Submarine.src="images/sub2_"+(hDirection+1)+".gif";
				shield=15;
				document.getElementById("txtTop").innerHTML="Shield: "+shield+" seconds";
				clearTimeout(shieldTimer);
				document.getElementById("objects").removeChild(obj);
				shieldTimer=setTimeout("shieldCountDown()",1000);
			break;
		case 3: text="2X";
				mulCounter=15;
				multiplier=2;
				document.getElementById("txtTop").innerHTML="4X Multiplier : "+mulCounter+" seconds";
				clearTimeout(mulTimer);
				document.getElementById("objects").removeChild(obj);
				mulTimer=setTimeout("mulCountDown()",1000);
			break;
		case 4: text="4X";
				mulCounter=15;
				multiplier=4;
				document.getElementById("txtTop").innerHTML="4X Multiplier : "+mulCounter+" seconds";
				clearTimeout(mulTimer);
				document.getElementById("objects").removeChild(obj);
				mulTimer=setTimeout("mulCountDown()",1000);
			break;
		default:text="";
				if(shield<=0)
				{
					Submarine.src="images/subExploded2_"+(hDirection+1)+".png";
					setTimeout("objCollide2("+i+")",500); 
					Lives--;
					document.getElementById("lives").innerHTML=Lives;
				}
				else
				{
					setTimeout("objCollide3("+i+")",1000); 
				}
				obj.src="images/explode1b.gif";
			break;
	}
	objArray[i]=-1;
	SetCookie("TA_Lives",Lives, exp);
	document.getElementById("txtMsg").innerHTML=text;
	clearTimeout(timer3);
	timer3=setTimeout("upAndFade(100)",25);
}
function objCollide2(i)
{
	Submarine.src="images/subExploded1_"+(hDirection+1)+".png";
	clearInterval(gameTimer);
	clearTimeout(gameTimer2);
	setTimeout("nextChance()",2000);
}
function objCollide3(i)
{
	var obj=document.getElementById("obstacle"+i);
	document.getElementById("objects").removeChild(obj);
}
function nextChance()
{
	document.getElementById("txtMsg").innerHTML="YOU LOST 1 LIFE";
	document.getElementById("txtMsg").style.top=250;
	document.getElementById("txtMsg").style.left=80;
	clearTimeout(mulTimer);
	clearTimeout(shieldTimer);
	document.getElementById("txtTop").innerHTML="";
	clearTimeout(timer3);
	timer3=setTimeout("upAndFade(100)",25);
	if(Lives>0)
		loadScreen2();
	else
	{
		document.getElementById("TXTBox").style.visibility="visible";
		document.getElementById("TXTstatus").innerHTML="Game Over<br/>You scored:"+Score+"<br/><a class='RedButton' style='position:relative;top:20;' href=''>&nbsp;High Scores</a><br/><a class='RedButton' style='position:relative;top:40;' href=''>&nbsp;More Games</a><br/><a onClick='GoToMenu()' class='RedButton' style='position:relative;top:60;'>&nbsp;&nbsp;Play Again&nbsp;&nbsp;</a>";
	}
}
function shieldCountDown()
{
	shield--;
	if(shield>0)
	{
		document.getElementById("txtTop").innerHTML="Shield: "+shield+" seconds";
	}
	else
	{
		document.getElementById("txtTop").innerHTML="";
		Submarine.src="images/sub1_"+(hDirection+1)+".gif";
		clearTimeout(shieldTimer);
		return;
	}
	shieldTimer=setTimeout("shieldCountDown()",1000);
}
function mulCountDown()
{
	mulCounter--;
	if(mulCounter>0)
	{
		document.getElementById("txtTop").innerHTML=multiplier+"X Multiplier : "+mulCounter+" seconds";
	}
	else
	{
		multiplier=1;
		document.getElementById("txtTop").innerHTML="";
		clearTimeout(mulTimer);
		return;
	}
	mulTimer=setTimeout("mulCountDown()",1000);
}
function getObj()
{
	var temp=Shuffle(totObjFreq-1)-1;
	var obj;
	obj=document.createElement("img");
	obj.style.position="absolute";
	obj.style.top=380;
	switch(objMap[temp])
	{
		case 1://yellow ball bonus life
		case 2: //yellow ball bonus safe
		case 3: //yellow ball bonus multiplier(2X)
		case 4: //yellow ball bonus multiplier(4X)
			obj.src="images/obstacle1.gif";
			break;
		default: obj.src="images/explode1a.gif";
			break;
	}
	obj.id="obstacle"+objCounter;
	objArray[objCounter]=objMap[temp];
	objCounter++;
	if(objCounter>20)
		objCounter=0;
	//obj.className="obstacles";
	temp=Shuffle(5);
	obj.style.left=(temp*50)-40;
	document.getElementById("objects").appendChild(obj);
	gameTimer2=setTimeout("getObj()",objGap);
}
function moveSub(dir)
{
	switch(dir)
	{
		case 1: 
		case 2: if(hDirection==dir-1)
					break;
				else
				{
					hDirection=dir-1;
					if(shield<=0)
						Submarine.src="images/sub1_"+dir+".gif";
					else
						Submarine.src="images/sub2_"+dir+".gif";
				}
				break;
		case 3:
		case 4: if(vDirection==dir-2)
					break;
				if(vDirection==0)
				{
					vDirection=dir-2;
				}
				else
					vDirection=0;
				break;
	}
}
function fireTA()
{
	if(fired==0)
	{
		fired=1;
		TA.src="images/TA"+(hDirection+1)+".gif";
		TA.style.left=parseInt(Submarine.style.left);
		TA.style.top=parseInt(Submarine.style.top)+60;
		TA.style.visibility="visible";
		fireCounter=0;
		timer2=setInterval("TAAnim("+hDirection+")",25);
	}
}
function TAAnim(pos)
{
	//Animate
	if(fireCounter<=fireLimit)
	{
		fireCounter++;
		if(pos==0)
		{
			TA.style.left=parseFloat(TA.style.left)+(fireSpeed/50);
			if(parseInt(TA.style.left)>318)
				TA.style.left=-45;
		}
		else
		{
			TA.style.left=parseFloat(TA.style.left)-(fireSpeed/50);
			if(parseInt(TA.style.left)<-40)
				TA.style.left=318;
		}
		return;
	}
	else
	{
		TA.style.top=parseFloat(TA.style.top)+(fireSpeed/50);
		if(parseInt(TA.style.top)>385)
		{
			clearInterval(timer2);
			fired=0;
		}
	}
}
function TACollide(i)
{
	var text="";
	switch(objArray[i])
	{
		case 1: 
		case 2: 
		case 3: 
		case 4: text="50";
				setTimeout("changeOpac(100,"+i+")",25);
				Score+=50*multiplier;
				TA.style.visibility="hidden";
			break;
		default:text="20";
				document.getElementById("obstacle"+i).src="images/explode1b.gif";
				TA.style.visibility="hidden";
				document.getElementById("obstacle"+i).style.left=parseInt(document.getElementById("obstacle"+i).style.left)-10;
				document.getElementById("obstacle"+i).style.top=parseInt(document.getElementById("obstacle"+i).style.top)-15;
				setTimeout("afterCollide("+i+")",500);
				Score+=20*multiplier;
			break;
	}
	if(objArray[i]!=-1)
	{
		document.getElementById("txtMsg").style.top=parseInt(document.getElementById("obstacle"+i).style.top);
		document.getElementById("txtMsg").style.left=parseInt(document.getElementById("obstacle"+i).style.left)+15;
		document.getElementById("txtMsg").innerHTML=text;
		clearTimeout(timer3);
		timer3=setTimeout("upAndFade(100)",25);
	}
	objArray[i]=-1;
	document.getElementById("score").innerHTML=Score;
	clearInterval(timer2);
	fired=0;
	SetCookie("TAgame",Score, exp);
	SetCookie("TA_Lives",Lives, exp);
}
function changeOpac(opacity, id) {
	var obj=document.getElementById("obstacle"+id);
    var object = obj.style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
	if(opacity<=0)
	{
		document.getElementById("objects").removeChild(obj);
		return;
	}
	else
	{
		opacity-=5;
		setTimeout("changeOpac("+opacity+", "+id+")",25);
	}
}
function afterCollide(i)
{
	var obj;
	obj=document.getElementById("obstacle"+i);
	document.getElementById("objects").removeChild(obj);
	TA.style.visibility="hidden";
}
function upAndFade(opacity)
{
	var obj=document.getElementById("txtMsg");
    var object = obj.style;
	object.top=parseInt(object.top)-1;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
	if(opacity<=0)
		return;
	else
	{
		opacity-=(txtFade/10);
		timer3=setTimeout("upAndFade("+opacity+")",25);
	}
}
function SetCookie (name, value) {  
var argv = SetCookie.arguments;  
var argc = SetCookie.arguments.length;  
var expires = (argc > 2) ? argv[2] : null;  
var path = (argc > 3) ? argv[3] : null;  
var domain = (argc > 4) ? argv[4] : null;  
var secure = (argc > 5) ? argv[5] : false;  
document.cookie = name + "=" + escape (value) + 
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
((path == null) ? "" : ("; path=" + path)) +  
((domain == null) ? "" : ("; domain=" + domain)) +    
((secure == true) ? "; secure" : "");
}
function GetCookie (name) {  
var arg = name + "=";  
var alen = arg.length;  
var clen = document.cookie.length;  
var i = 0;  
while (i < clen) {    
var j = i + alen;    
if (document.cookie.substring(i, j) == arg)      
return getCookieVal (j);    
i = document.cookie.indexOf(" ", i) + 1;    
if (i == 0) break;   
}  
return unescape("");
}
function getCookieVal (offset) {  
var endstr = document.cookie.indexOf (";", offset);  
if (endstr == -1)    
endstr = document.cookie.length;  
return unescape(document.cookie.substring(offset, endstr));
}
