//Screen Animation variables //moves:no. of moves to change screen
var Img,Img2,loadImg,timer,timer2,timerBall,timerBall2,timerBall3,bonusTimer,afterAnim,lastTop,lastLeft,lastLeft2,the_style,movLeft,animType,screenWidth=320,moves=10,adsTimer,adsTimeSet=5;
//Game Variables
//ballstatus 1: on move (before dropping), 0: ball out of scene, 2: ball in main screen
var Level,ballStatus,ball,ballArr,ballsTotal,ballColor,count,obstacles,bonusFreq,bonusMap,txtFade=15,bonusType,Score,bonusClearCount,bonusX,bonusY,bonusMultiplier,multiplier,obsCount,speedBonus,speedBalls=4,speedBalls2,holesLeft,gameState,gamePaused,bonusScore,mulChk,speedTimeout=2000,gameRandomMode=0,randObs,randObsColor,obsColor;//gameRandomMode=1 gives random placement of obstacles and =0 gives fixed placement of obstacles
//Balls Animation and Movements
var ballX,ballY,speedX,speedY,accX,accY;
//Level variables
var ballsCount,obstaclesCount,holesH,holesV,leftPos,topPos,holesStatus,holesCount,holesColor,bonusVars,bonusClear;
//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 i,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;
	ball=new Array(10); ballColor=new Array(10);
	ballX=new Array(10);ballY=new Array(10);
	bonusType=new Array(10); count=new Array(0,0,0,0,0,0,0,0,0,0);
	speedX=new Array(10);speedY=new Array(10);
	ballsCount=new Array(90,85,80,75,70,65,60);
	ballArr=new Array(3);
	holesCount=new Array(60,60,55,50,45,40,35);
	obstaclesCount=new Array(4,5,7,9,10,10,12);
	//it stores a no. b/w 0 to 79 for 80 positions (rowno.*8 +col.no.) where row no.  is b/w 0 to 9 and col b/w 0 to 7
	randObs=new Array(	new Array(4,18,64,71),//Level1
						new Array(9,6,20,15,66),//Level2
						new Array(10,6,20,15,38,4,60),//Level3
						new Array(10,6,20,15,38,4,68,64,45),//Level4
						new Array(10,6,20,15,38,4,60,50,45,2,43),//Level5
						new Array(10,6,20,15,38,4,60,50,45,2,43),//Level6
						new Array(10,6,20,15,38,4,60,50,45,2,43,24,34)//Level7
					  );
	randObsColor=new Array(	new Array(3,2,4,5),//Level1
							new Array(1,4,2,3,6),//Level2
							new Array(1,6,2,1,3,4,5),//Level3
							new Array(1,6,2,1,3,4,5,5,4),//Level4
							new Array(1,6,2,1,3,4,6,5,4,2,3),//Level5
							new Array(1,6,2,1,3,4,6,5,4,2,3),//Level6
							new Array(1,6,2,1,3,4,6,5,4,2,3,4,5)//Level7
						  );
	bonusClear=new Array(200,600,700,800,900,1000,1100);//threshold score for each level
	/*BONUS SCORES
	0-bonus clearing
	1-freeball opening
	2-bonus opening
	3- match 3
	4- speed round
	5- duplicate
	*/
	bonusScore=new Array(100,20,100,50,60,10);
	bonusMultiplier=new Array(2,3,4,5,10);
	holesColor=new Array(10);
	holesStatus=new Array(10);
	//accX=1; accY=15;
	accX=1; accY=200;//accY=30//060909 was 30
	timer=setInterval("AnimateScreen()",10);//10
}
function GoToMenu()
{
	var temp=((Level+1)%2)+1,temp2;
	document.getElementById("TXTBox"+temp).style.visibility="hidden";
	document.getElementById("objects"+temp).innerHTML="";
	Img=document.getElementById("screen"+(3+temp));
	Img2=document.getElementById("screen2");
	temp2=(-2-temp)*screenWidth;
	Img.style.left=temp2+"px";
	movLeft=screenWidth/moves;
	afterAnim="";
	lastLeft=(-1-temp)*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;
	Score=0; Level=1;
	/*BONUS FREQUENCIES
	0-no bonus(is used for nulling the effect of bonus); 
	  1-duplicate; 
	  2-free ball opening; 
	  3-Multiplier opening; 
	  4-bonus opening;  */
	bonusVars=new Array(new Array(1,10,2,5,15),//Level1
						new Array(10,6,20,10,10),//Level2
						new Array(25,7,3,3,2),//Level3
						new Array(25,6,3,2,2),//Level4
						new Array(25,6,3,2,2),//Level5
						new Array(25,6,3,2,2),//Level6
						new Array(25,6,3,2,2)//Level7
						);
	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()
{
	var i,c,x,col,hole,temp=((Level+1)%2)+1,area,temp2=(Level%2)+1;
	ballStatus=new Array(0,0,0,0,0,0,0,0,0,0);
	obstacles=new Array();
	obsColor=new Array();
	speedBonus=0; bonusX=10; bonusY=10;
	document.getElementById("objects"+temp2).innerHTML="";
	document.getElementById("ballsArea"+temp2).innerHTML="";
	document.getElementById("objects"+temp).innerHTML="";
	document.getElementById("score"+temp).innerHTML=Score;
	document.getElementById("level"+temp).innerHTML=Level;
	speedBalls2=speedBalls;
	var temp2=GetCookie("BounceTrap");
	if((temp2=="")||(parseInt(temp2)==0))
		document.getElementById("highscore"+temp).innerHTML=0;
	else
		document.getElementById("highscore"+temp).innerHTML=parseInt(temp2);
	for(i=1;i<=12;i++)
	{
		area=document.createElement("img");
		area.id="area"+i;
		area.src="images/ball"+Shuffle(5)+".gif";
		area.style.position="absolute";
		area.style.left=(i-1)*11;
		area.style.top=0;
		document.getElementById("ballsArea"+temp).appendChild(area);
	}
	for(i=0;i<3;i++)
	{
		ballArr[i]=Shuffle(5);
		document.getElementById("ball_"+temp+""+(i+1)).src="images/ball"+ballArr[i]+".gif";
	}
	bonusMap=new Array();
	bonusFreq=0;
	bonusClearCount=0;
	gameState=0; gamePaused=0; mulChk=0;
	for(i=0;i<5;i++)
	{
		while(bonusVars[Level-1][i]>0)
		{
			bonusMap[bonusFreq]=i;
			bonusFreq++;
			bonusVars[Level-1][i]--;
		}
	}
	for(i=0;i<10;i++)
	{
		holesStatus[i]=new Array(0,0,0,0,0,0,0,0);
		holesColor[i]=new Array(0,0,0,0,0,0,0,0);
	}
	c=0;
	while(c!=obstaclesCount[Level-1])
	{
		if(gameRandomMode==0)
		{
			x=randObs[Level-1][c];
			col=randObsColor[Level-1][c];
		}
		else
		{	
			x=Shuffle(71)-1;
			col=Shuffle(5);
		}
		if((holesStatus[parseInt(x/8)][x%8]==0&&holesStatus[parseInt(x/8)+1][x%8]==0)||gameRandomMode==0)
		{
			obstacles[c]=x;
			obsColor[c]=col;
			holesColor[parseInt(x/8)][x%8]=col;
			holesColor[parseInt(x/8)+1][x%8]=col;
			c++;
			holesStatus[parseInt(x/8)][x%8]=5;
			holesStatus[parseInt(x/8)+1][x%8]=5;
			hole=document.createElement("img");
			hole.id="obstacle"+parseInt(x/8)+"_"+(x%8);
			hole.src="images/obstacle"+col+".gif";
			hole.style.position="absolute";
			hole.style.left=48+(x%8)*34;
			hole.style.top=165+parseInt(x/8)*23;
			document.getElementById("objects"+temp).appendChild(hole);
		}
	}
	c=0;
	while(c!=holesCount[Level-1])
	{
		x=Shuffle(79)-1;
		if(holesStatus[parseInt(x/8)][x%8]==0)
		{
			c++;
			holesStatus[parseInt(x/8)][x%8]=1;
			hole=document.createElement("img");
			hole.id="hole"+parseInt(x/8)+"_"+(x%8);
			hole.src="images/holeRegular.gif";
			hole.style.position="absolute";
			hole.style.left=54+(x%8)*34;
			hole.style.top=160+parseInt(x/8)*23;
			document.getElementById("objects"+temp).appendChild(hole);
		}
	}
	holesLeft=holesCount[Level-1];
	document.getElementById("ballsLeft"+temp).innerHTML=ballsCount[Level-1];
	document.getElementById("progressBar"+temp).style.height=85;
	if(Level>1)
		ballsTotal+=ballsCount[Level-1];
	else
		ballsTotal=ballsCount[Level-1];
	document.getElementById("nextBall"+temp).innerHTML="<img src='images/ball"+ballArr[0]+".gif' />";
	document.getElementById("TXTBox"+temp).style.visibility="visible";
	if(Level==1)
		document.getElementById("TXTstatus"+temp).innerHTML="Press the DROP button (to your left) to drop the ball during the game.<br/><br/>Close all the holes to proceed to the next level.<br/><br/>Watch for Bonus Actions to gain big points.<br/><br/>Click OK to start the game.<br/><div align='center' class='RedButtonMenu' style='position:relative;top:5px;' ><a class='buttons' onClick='readySet1()'>&nbsp;OK</a></div>";
	else
		document.getElementById("TXTstatus"+temp).innerHTML="<br/><br/>Level "+Level+"<br/>Click OK to start the game.<br/><div align='center' class='RedButtonMenu' style='position:relative;top:5px;' ><a class='buttons' onClick='readySet1()'>&nbsp;OK</a></div>";
}
function readySet1()
{
	var temp=((Level+1)%2)+1;
	setTimeout("readySet2()",1000);
	document.getElementById("TXTBox"+temp).style.visibility="hidden";
	document.getElementById("readySet"+temp).innerHTML="Ready";
}
function readySet2()
{
	var temp=((Level+1)%2)+1;
	setTimeout("readySet3()",1000);
	document.getElementById("readySet"+temp).innerHTML="Set";
}
function readySet3()
{
	var temp=((Level+1)%2)+1;
	clearTimeout(timerBall3);
	timerBall3=setTimeout("nextBall()",1000);
	document.getElementById("readySet"+temp).innerHTML="Go";
}

function nextLevel()
{
	var temp=((Level+1)%2)+1,bonusLevelScore;
	Level++;
	bonusLevelScore=ballsTotal*5;
	Score+=bonusLevelScore;
	if(Level==8)
	{
		gameOver();
		return;
	}
	loadImg=new Image;
	loadImg.src="images/BKlevel"+Level+".jpg";
	timer=setInterval("chkingLoad()",10);
	document.getElementById("TXTBox"+temp).style.visibility="visible";
	document.getElementById("TXTstatus"+temp).innerHTML="<br/>Level Complete<br/>Completion Bonus:"+bonusLevelScore+"<br/><br/>Loading next Level";
	leftPos=new Array();
	topPos=new Array();
}
function chkingLoad()
{
	if(loadImg.complete)
	{
		clearInterval(timer);
		startNewLevel();
	}
}
function startNewLevel()
{
	Img=document.getElementById("screen4");
	Img2=document.getElementById("screen5");
	var temp=((Level+1)%2)+1;
	document.getElementById("LevelBG"+temp).src="images/BKlevel"+Level+".jpg";
	temp=-3*screenWidth;
	if(Level%2==0)
	{
		Img.style.left=temp+"px";
		Img2.style.left=temp+"px";
		lastLeft=-4*screenWidth;lastLeft2=-4*screenWidth;
	}
	else
	{
		Img.style.left=(temp+screenWidth)+"px";
		Img2.style.left=(temp-screenWidth)+"px";
		lastLeft=-3*screenWidth;lastLeft2=-5*screenWidth;
	}
	movLeft=-1*screenWidth/moves;
	afterAnim="loadScreen2()";
	animType=0;
	timer=setInterval("AnimateScreen()",10);
}
function nextBall()
{
	if(gamePaused==1)
		return;
	var i,counting,temp=((Level+1)%2)+1,txtMsg,txt,c,x,j;
	document.getElementById("TXTBox"+temp).style.visibility="hidden";
	document.getElementById("readySet"+temp).innerHTML="";
	document.getElementById("holesLeft"+temp).innerHTML=holesLeft;
	clearTimeout(bonusTimer);
	if(mulChk==1)
		mulChk=0;
	else
		multiplier=1;
	obsCount=new Array();
	for(i=0;i<obstaclesCount[Level-1];i++)
		obsCount[i]=0;
	if(ballsTotal<=0)
	{
		gameOver();
		return;
	}
	counting=0;
	for(x=0;x<10;x++)
	{
		for(y=0;y<8;y++)
		{
			if(holesStatus[x][y]>10)
				holesStatus[x][y]=1;
			if(holesStatus[x][y]==1)
				counting++;
		}
	}
	document.getElementById("holesLeft"+temp).innerHTML=counting;
	if(counting==0)
	{
		nextLevel();
		return;
	}
	ballsTotal--;
	document.getElementById("ballsLeft"+temp).innerHTML=ballsTotal;
	if(ballsTotal<ballsCount[Level-1])
		document.getElementById("progressBar"+temp).style.height=85*ballsTotal/ballsCount[Level-1];
	else
		document.getElementById("progressBar"+temp).style.height=85;
	if(ballsTotal<12&&(document.getElementById("area"+(ballsTotal+1))))
		document.getElementById("ballsArea"+temp).removeChild(document.getElementById("area"+(ballsTotal+1)));
	i=0;
	while(ballStatus[i]!=0&&i<10)
		i++;
	if(i==10)
		return;
	bonusType[i]=bonusMap[Shuffle(bonusFreq-1)-1];
	txtMsg=document.getElementById("txtMsg2"+temp);
	//alert("txtMsg2"+temp);
	if(bonusX!=10&&bonusY!=10)
	{
		document.getElementById("hole"+bonusX+"_"+bonusY).src="images/holeRegular.gif";
		bonusX=10;	bonusY=10;
	}
	if(bonusClearCount<parseInt(Score/bonusClear[Level-1]))//bonus clear
	{
		switch(ballArr[0])
		{
			case 1: txt="Blue";
					break;
			case 2: txt="Green";
					break;
			case 3:	txt="Orange";
					break;
			case 4:	txt="Purple";
					break;
			case 5:	txt="Red";
					break;
			case 6:	txt="Yellow";
					break;
		}
		bonusClearCount++;
		txtMsg.innerHTML="Bonus Clearing - "+txt;
		txtMsg.style.top=145;
		txtMsg.style.left=100;
		bonusType[i]=6;
		setTimeout("upAndFade2(100,2)",25);
	}
	if(bonusType[i]>1&&bonusType[i]<5)
	{
		while(1)
		{
			x=Shuffle(79)-1;
			if(holesStatus[parseInt(x/8)][x%8]==1)
			{
				bonusX=parseInt(x/8);
				bonusY=x%8;
				holesStatus[bonusX][bonusY]=10+bonusType[i];
				//alert("Dfsdfsdf");
				break;
			}
		}
	}
	switch(bonusType[i])
	{
		case 1://Bonus Ball DUPLICATE
				for(j=0;j<obstaclesCount[Level-1];j++)
				{
					if(obsColor[j]==ballArr[0])
						break;
				}
				if(j<obstaclesCount[Level-1])
					txtMsg.innerHTML="Bonus Ball";
				else
					txtMsg.innerHTML="";
				break;
		case 2://FreeBallOpening
				BlinkOpening(i,200);
				txtMsg.innerHTML="FreeBall Opening";
				break;
		case 3://Multiplier Opening
				BlinkOpening(i,34);
				txtMsg.innerHTML="Multiplier Opening";
				break;
		case 4://Bonus Opening
				BlinkOpening(i,26);
				txtMsg.innerHTML="Bonus Opening";
				break;
		default://No Bonus
				break;
	}
	if(bonusType[i]>=1&&bonusType[i]<=5)
	{
		txtMsg.style.top=145;
		txtMsg.style.left=120;
		setTimeout("upAndFade2(100,2)",25);
	}
	ball[i]=document.createElement("img");
	ball[i].src="images/ball"+ballArr[0]+".gif";
	ballColor[i]=ballArr[0];
	document.getElementById("objects"+temp).appendChild(ball[i]);
	ballStatus[i]=1;
	ball[i].style.position="absolute";
	ball[i].style.left=305; ballX[i]=305;
	ball[i].style.top=100; ballY[i]=100;
	ballArr[0]=ballArr[1];
	ballArr[1]=ballArr[2];
	ballArr[2]=Shuffle(5);
	for(i=0;i<3;i++)
		document.getElementById("ball_"+temp+""+(i+1)).src="images/ball"+ballArr[i]+".gif";
	document.getElementById("nextBall"+temp).innerHTML="<img src='images/ball"+ballArr[0]+".gif' />";
	clearInterval(timerBall2);
	if(speedBonus>0)
	{
		speedBonus--;
		clearTimeout(timerBall3);
		timerBall3=setTimeout("nextBall()",speedTimeout);
	}
	gameState=1;
	timerBall2=setInterval("hoverBall()",5);//060909 was 20
}
function hoverBall()
{
	var temp=((Level+1)%2)+1,chk=0,chk2=0;
	for(i=0;i<10;i++)
	{
		if(ballStatus[i]!=0)
			chk2=1;
		if(ballStatus[i]==1)
		{
			chk=1;
			if(ballY[i]>=125)
			{
				ballX[i]-=6;//060909 was 2
				ball[i].style.left=ballX[i];
				if(ballX[i]<40)
				{
					document.getElementById("objects"+temp).removeChild(ball[i]);
					ballStatus[i]=0;
				}
			}
			else
			{
				ballY[i]+=2;
				ball[i].style.top=ballY[i];
			}
		}
	}
	if(chk==0)
	{
		clearInterval(timerBall2);
		if(speedBonus==0&&chk2==0)
		{
			clearTimeout(timerBall3);
			timerBall3=setTimeout("nextBall()",600);
		}
		gameState=0;
		return;
	}
}
function distance(x,y)
{
	var xsq,ysq;
	xsq=x*x;
	ysq=y*y;
	return(Math.sqrt(xsq+ysq));
}

//Move the ball
function moveBall()
{
	var i,chk=0,chk2=0,temp=((Level+1)%2)+1;
	for(i=0;i<10;i++)
	{
		if(ballStatus[i]==1||ballStatus[i]==2)
			chk=1;
		if(ballStatus[i]!=2)
			continue;
		chk2=1;
		if(ballY[i]>385)
		{
			document.getElementById("objects"+temp).removeChild(ball[i]);
			ballStatus[i]=0;
		}
		else
		{
			speedY[i]+=accY;
			if(speedY[i]<0)
				speedY[i]+=accY;
		}
		if(ballY[i]<145)
			speedY[i]=Math.abs(speedY[i]);
		if(ballX[i]<45)
			speedX[i]=Math.abs(speedX[i]);
		if(ballX[i]>310)
			speedX[i]=-1*Math.abs(speedX[i]);
		if(speedX[i]>0)
		{
			if(speedX[i]>accX)
				speedX[i]-=accX;
			else
				speedX[i]=0;
		}
		else
		{
			if(Math.abs(speedX[i])>accX)
				speedX[i]+=accX;
			else
				speedX[i]=0;
		}
		ballX[i]+=(speedX[i]/100);//100
		ballY[i]+=(speedY[i]/100);//100
		var tempX,tempY,j,k;
		ball[i].style.top=ballY[i];
		ball[i].style.left=ballX[i];
		if(ballX[i]>45&&ballX[i]<310&&count[i]==0)
		{
			if(ballY[i]>152&&ballY[i]<380)
			{
				for(j=0;j<obstaclesCount[Level-1];j++)//for obstacle
				{
					k=obstacles[j];//218,165
					if((ballX[i]>(42+(k%8)*34))&&(ballX[i]<(75+(k%8)*34))&&(ballY[i]>(160+parseInt(k/8)*23))&&(ballY[i]<(190+parseInt(k/8)*23)))// the no. 75 in the term "ballX[i]<(75+(k%8)*34)" can be modified to set Obstacle's right side distance
					{
						ballStatus[i]=4;
						hitObstacle((k%8),parseInt(k/8),i);
						//alert(ballX[i]+"   "+ballY[i]+"   "+k);
						return;
					}
				}
				if(parseInt(ballX[i]-50)%34<18&&parseInt(ballY[i]-155)%23<18)// for holes
				{
					tempX=parseInt((ballX[i]-50)/34);
					tempY=parseInt((ballY[i]-152)/23);
					ballStatus[i]=4;
					switch(holesStatus[tempY][tempX])
					{
						case 1://empty hole
								ballFixed(tempX,tempY,i);
								break;
						case 2://the object has not been hit
						case 3://the object has been hit once
								hitObject(tempX,tempY,i);
								break;
						case 5://obstacle
								//alert("here");
								hitObstacle(tempX,tempY,i);
								break;
						case 12://FreeBallOpening
						case 13://Multiplier Opening
						case 14://Bonus Opening
								ballFixed(tempX,tempY,i);
								break;
						case 4://hit twice (hole/ball doesn't exist)
						default:ballStatus[i]=2;
								break;
					}
				}
			}
		}
		if(count[i]>0)
			count[i]--;
	}
	if(chk2==0)
	{
		clearInterval(timerBall);
		if((speedBonus==0||speedBonus==speedBalls)&&chk==0)
		{
			clearTimeout(timerBall3);
			timerBall3=setTimeout("nextBall()",600);
		}
		gameState=0;
	}
}
function ballFixed(tempX,tempY,i)
{
	//alert(tempX+"df"+tempY+"fgf"+holesStatus[tempY][tempX]);
	var x,y,z,txtMsg,stars=0,tempCountX=new Array(),tempCountY=new Array(),counting,temp=((Level+1)%2)+1,chk=0;
	Score+=parseInt(20*multiplier);
	ball[i].style.left=57+(tempX)*34;
	ball[i].style.top=163+(tempY*23);
	txtMsg=document.getElementById("txtMsg2"+temp);
	txtMsg.style.top=145;
	txtMsg.style.left=100;
	if(holesStatus[tempY][tempX]>11)
	{
		document.getElementById("hole"+tempY+"_"+tempX).src="images/holeRegular.gif";
		stars=document.createElement("img");
		stars.style.position="absolute";
		stars.style.left=45+(tempX)*34;
		stars.style.top=153+(tempY*23);
		document.getElementById("objects"+temp).appendChild(stars);
		stars.id="stars";
		setTimeout("document.getElementById('objects"+temp+"').removeChild(document.getElementById('stars'))",1000);
		switch(holesStatus[tempY][tempX])
		{
			case 12://FreeBallOpening
					txtMsg.innerHTML="Free Ball Awarded";
					ballsTotal++;
					document.getElementById("ballsLeft"+temp).innerHTML=ballsTotal;
					if(ballsTotal<ballsCount[Level-1])
						document.getElementById("progressBar"+temp).style.height=85*ballsTotal/ballsCount[Level-1];
					else
						document.getElementById("progressBar"+temp).style.height=85;
					stars.src="images/StarsBonus1.gif";
					Score+=parseInt(bonusScore[1]*multiplier);
					holesStatus[tempY][tempX]=2;
					document.getElementById("score"+temp).innerHTML=Score;
					break;
			case 13://Multiplier Opening
					multiplier=bonusMultiplier[Shuffle(4)-1];
					mulChk=1;
					txtMsg.innerHTML="Bonus Multiplier - "+multiplier+"X";
					holesStatus[tempY][tempX]=2;
					stars.src="images/StarsBonus2.gif";
					break;
			case 14://Bonus Opening
					txtMsg.innerHTML="Bonus Clear";
					//alert("here2");
					chk=1;
					stars.src="images/StarsBonus3.gif";
					fader(ball[i].style,20);
					fader(document.getElementById("hole"+tempY+"_"+tempX).style,20);
					holesStatus[tempY][tempX]=4;
					for(x=tempX-1;x<tempX+2;x++)
					{
						for(y=tempY-1;y<tempY+2;y++)
						{
							if(x>=0&&y>=0&&x<8&&y<10)
							{
								if(holesStatus[y][x]==1)
									holesLeft--;
								if(holesStatus[y][x]>=1&&holesStatus[y][x]<=3)
								{
									if(holesStatus[y][x]==2||holesStatus[y][x]==3)
										fader(document.getElementById("ball"+y+"_"+x).style,20);
									holesStatus[y][x]=4;
									fader(document.getElementById("hole"+y+"_"+x).style,20);
								}
							}
						}
					}
					//alert("herererer");
					Score+=parseInt(bonusScore[2]*multiplier);
					document.getElementById("score"+temp).innerHTML=Score;
					break;
			default: //alert("kya");
		}
		setTimeout("upAndFade2(100,2)",25);
	}
	else
		holesStatus[tempY][tempX]=2;
	holesLeft--;
	document.getElementById("holesLeft"+temp).innerHTML=holesLeft;
	ball[i].id="ball"+tempY+"_"+tempX;
	holesColor[tempY][tempX]=ballColor[i];
	ballStatus[i]=0;
	//Bejeweled
	counting=0;
	y=tempY; x=tempX+1;
	if(chk==0)
	{
		while(x<8)
		{
			if(holesStatus[y][x]==0)
				x++;
			else
				break;
		}
		if(x<8)
		{
			if(holesStatus[y][x]>=2&&holesStatus[y][x]<=3&&ballColor[i]==holesColor[y][x])
			{
				tempCountX[counting]=x;	tempCountY[counting]=y;
				counting++;
				x++;
				while(x<8)
				{
					if(holesStatus[y][x]==0)
						x++;
					else
						break;
				}
				if(x<8)
				{
					if(holesStatus[y][x]>=2&&holesStatus[y][x]<=3&&ballColor[i]==holesColor[y][x])
					{
						tempCountX[counting]=x;	tempCountY[counting]=y;
						counting++;
						//alert("here");
					}
				}
			}
		}
		y=tempY; x=tempX-1;
		while(x>=0)
		{
			if(holesStatus[y][x]==0)
				x--;
			else
				break;
		}
		if(x>=0)
		{
			if(holesStatus[y][x]>=2&&holesStatus[y][x]<=3&&ballColor[i]==holesColor[y][x])
			{
				tempCountX[counting]=x;	tempCountY[counting]=y;
				counting++;
				//alert("herea"+counting);
				x--;
				while(x>=0)
				{
					if(holesStatus[y][x]==0)
						x--;
					else
						break;
				}
				if(x>=0)
				{
					if(holesStatus[y][x]>=2&&holesStatus[y][x]<=3&&ballColor[i]==holesColor[y][x])
					{
						tempCountX[counting]=x;	tempCountY[counting]=y;
						counting++;
					}
				}
			}
		}
		if(counting>=2)//bejeweled
		{
			y=tempY;
			//alert("erere");
			tempCountX[counting]=tempX;	tempCountY[counting]=tempY;
			counting++;
			for(i=0;i<counting;i++)
			{
				x=tempCountX[i]; y=tempCountY[i];
				holesStatus[y][x]=4;
				fader(document.getElementById("hole"+y+"_"+x).style,20);
				fader(document.getElementById("ball"+y+"_"+x).style,20);
			}
			if(stars==0)
			{
				stars=document.createElement("img");
				stars.src="images/Stars1.gif";
				stars.style.position="absolute";
				stars.style.left=45+(tempX)*34;
				stars.style.top=153+(tempY*23);
				document.getElementById("objects"+temp).appendChild(stars);
				stars.id="stars";
				Score+=parseInt(bonusScore[3]*multiplier);
				document.getElementById("score"+temp).innerHTML=Score;
				setTimeout("document.getElementById('objects"+temp+"').removeChild(document.getElementById('stars'))",1000);
			}
		}
		counting=0;
		y=tempY+1; x=tempX;
		while(y<10)
		{
			if(holesStatus[y][x]==0)
				y++;
			else
				break;
		}
		if(y<10)
		{
			if(holesStatus[y][x]>=2&&holesStatus[y][x]<=3&&ballColor[i]==holesColor[y][x])
			{
				tempCountX[counting]=x;	tempCountY[counting]=y;
				counting++;
				y++;
				while(y<10)
				{
					if(holesStatus[y][x]==0)
						y++;
					else
						break;
				}
				if(y<10)
				{
					if(holesStatus[y][x]>=2&&holesStatus[y][x]<=3&&ballColor[i]==holesColor[y][x])
					{
						tempCountX[counting]=x;	tempCountY[counting]=y;
						counting++;
					}
				}
			}
		}
		y=tempY-1; x=tempX;
		//alert(holesStatus[y][x]);
		while(y>=0)
		{
			if(holesStatus[y][x]==0)
				y--;
			else
				break;
		}
		if(y>=0)
		{
			if(holesStatus[y][x]>=2&&holesStatus[y][x]<=3&&ballColor[i]==holesColor[y][x])
			{
				tempCountX[counting]=x;	tempCountY[counting]=y;
				counting++;
				y--;
				while(y>=0)
				{
					if(holesStatus[y][x]==0)
						y--;
					else
						break;
				}
				if(y>=0)
				{
					if(holesStatus[y][x]>=2&&holesStatus[y][x]<=3&&ballColor[i]==holesColor[y][x]&&y>=0)
					{
						tempCountX[counting]=x;	tempCountY[counting]=y;
						counting++;
					}
				}
			}
		}
		if(counting>=2)//bejeweled
		{
			//alert("dfdsfsdf");
			tempCountX[counting]=tempX;	tempCountY[counting]=tempY;
			counting++;
			for(i=0;i<counting;i++)
			{
				y=tempCountY[i]; x=tempCountX[i];
				holesStatus[y][x]=4;
				fader(document.getElementById("hole"+y+"_"+x).style,20);
				fader(document.getElementById("ball"+y+"_"+x).style,20);
			}
			if(stars==0)
			{
				stars=document.createElement("img");
				stars.src="images/Stars1.gif";
				stars.style.position="absolute";
				stars.style.left=45+(tempX)*34;
				stars.style.top=153+(tempY*23);
				document.getElementById("objects"+temp).appendChild(stars);
				Score+=parseInt(bonusScore[3]*multiplier);
				document.getElementById("score"+temp).innerHTML=Score;
				stars.id="stars";
				setTimeout("document.getElementById('objects"+temp+"').removeChild(document.getElementById('stars'))",1000);
			}
		}
	}
	//End Bejeweled
	txtMsg=document.getElementById("txtMsg1"+temp);
	//alert("txtMsg1"+temp);
	document.getElementById("score"+temp).innerHTML=Score;
	txtMsg.innerHTML="20";
	txtMsg.style.top=153+(tempY*23); txtMsg.style.left=60+(tempX)*34;
	setTimeout("upAndFade(100,1)",25);
}
function hitObject(tempX,tempY,i)
{
	var hole,y,x,hyp,cosine,sine,cosine2,temp=((Level+1)%2)+1,sine2,newx,newy,stars;
	//alert("ouch");
	holesStatus[tempY][tempX]++;
	hole=document.getElementById("hole"+tempY+"_"+tempX);
	object=document.getElementById("ball"+tempY+"_"+tempX).style;
	//alert(holesStatus[tempY][tempX]);
	if(holesStatus[tempY][tempX]==3)
		opacity=60;
	else
		opacity=20;
	fader(hole.style,opacity);
	fader(object,opacity);
	if(bonusType[i]==6&&holesColor[tempY][tempX]==ballColor[i])
	{
		for(x=0;x<8;x++)
		{
			for(y=0;y<10;y++)
			{
				if(holesStatus[x][y]>=2&&holesStatus[x][y]<=3&&holesColor[x][y]==ballColor[i])
				{
					holesStatus[x][y]=4;
					fader(document.getElementById("hole"+x+"_"+y).style,20);
					fader(document.getElementById("ball"+x+"_"+y).style,20);
				}
			}
		}
		Score+=parseInt(bonusScore[0]*multiplier);
		document.getElementById("score"+temp).innerHTML=Score;
		stars=document.createElement("img");
		stars.src="images/Stars1.gif";
		stars.style.position="absolute";
		stars.style.left=45+(tempX)*34;
		stars.style.top=153+(tempY*23);
		document.getElementById("objects"+temp).appendChild(stars);
		stars.id="stars2";
		setTimeout("document.getElementById('objects"+temp+"').removeChild(document.getElementById('stars2'))",1000);
	}
	y=parseFloat(hole.style.top)-parseFloat(ball[i].style.top);
	x=parseFloat(hole.style.left)-parseFloat(ball[i].style.left);
	hyp=distance(x,y);
	cosine=x/hyp; sine=y/hyp;
	cosine2=cosine*cosine-sine*sine;
	sine2=2*cosine*sine;
	newx=parseInt(speedX[i]*cosine2-speedY[i]*sine2);
	newy=parseInt(speedY[i]*cosine2+speedX[i]*sine2);
	if(y<0)
		speedX[i]=-1*newx;
	else
		speedX[i]=newx;
	speedY[i]=newy; 
	count[i]=4;
	if(ballColor[i]==5)
	{
		speedX[i]=speedX[i]*2;
		speedY[i]=speedY[i]*2;
	}
	//alert(sine2+"   "+cosine2);
	ballStatus[i]=2;
}
function fader(object,opacity)
{
	object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}
function hitObstacle(tempX,tempY,i)
{
	//alert("gulp"+tempX+""+tempY);
	var j,x,y,speed=500,temp=((Level+1)%2)+1,tempMul,tempMul2=0;
	
	tempMul=(tempY*8)+tempX;
	for(j=0;j<obstaclesCount[Level-1];j++)
	{
		if(obstacles[j]==tempMul)
		{
			obsCount[j]=1;
		}
		if(obsCount[j]==0)
			tempMul2=1;
	}
	if(speedBonus==0&&tempMul2==0)
	{
		speedBonus=speedBalls2;
		speedBalls2=0;
		Score+=parseInt(bonusScore[4]*multiplier);
		txtMsg=document.getElementById("txtMsg2"+temp);
		txtMsg.innerHTML="Speed Bonus Round";
		txtMsg.style.top=145;
		txtMsg.style.left=100;
		setTimeout("upAndFade2(100,2)",25);
		document.getElementById("score"+temp).innerHTML=Score;
	}
	if(bonusType[i]==1&&(ballColor[i]==holesColor[tempY][tempX]))
	{
		j=0;
		while(ballStatus[j]!=0&&j<10)
			j++;
		if(j==10)
			return;
		//alert(ballColor[i]+"w"+holesColor[tempY][tempX]);
		ball[j]=document.createElement("img");
		ball[j].src="images/ball"+ballColor[i]+".gif";
		ballColor[j]=ballColor[i];
		document.getElementById("objects"+temp).appendChild(ball[j]);
		ballStatus[j]=2;
		bonusType[j]=0;
		ball[j].style.position="absolute";
		ball[j].style.left=ball[i].style.left; ballX[j]=ballX[i];
		ball[j].style.top=ball[i].style.top; ballY[j]=ballY[i];
		Score+=parseInt(bonusScore[5]*multiplier);
		document.getElementById("score"+temp).innerHTML=Score;
		if(ballX[j]<54+34*tempX)
			speedX[j]=-1.25*speed;
		else
			speedX[j]=1.25*speed;
		if(ballY[j]<172+tempY*23)
			speedY[j]=-0.5*speed;
		else
			speedY[j]=0.5*speed;
		count[j]=3;		
	}
	if(bonusType[i]==6&&holesColor[tempY][tempX]==ballColor[i])
	{
		for(x=0;x<8;x++)
		{
			for(y=0;y<10;y++)
			{
				if(holesStatus[x][y]>=2&&holesStatus[x][y]<=3&&holesColor[x][y]==ballColor[i])
				{
					holesStatus[x][y]=4;
					fader(document.getElementById("hole"+x+"_"+y),20);
				}
			}
		}
	}
	if(ballX[i]<57+34*tempX)
		speedX[i]=-1*speed;
	else
		speedX[i]=speed;
	if(ballY[i]<172+tempY*23)
		speedY[i]=-1*speed;
	else
		speedY[i]=speed;
	if(ballColor[i]==5)
	{
		speedX[i]=speedX[i]*2;
		speedY[i]=speedY[i]*2;
	}
	ballStatus[i]=2;
	count[i]=1;
}
function LeaveGame()
{
	PauseGame();
	var temp=((Level+1)%2)+1;
	document.getElementById("TXTstatus"+temp).innerHTML="<br/>Are You sure you want to leave the game. <div align='center' class='RedButtonMenu' style='position:relative;top:15;'> <a onClick='GoToMenu()' class='buttons'>&nbsp;&nbsp;&nbsp;&nbsp;YES&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a></div><div align='center' class='RedButtonMenu' style='position:relative;top:5;'> <a onClick='UnpauseGame()' class='buttons'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NO&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a></div>";
}
function PauseGame()
{
	var temp=((Level+1)%2)+1;
	gamePaused=1;
	switch(gameState)
	{
		case 1:clearInterval(timerBall2);
				break;
		default:clearInterval(timerBall);
				break;
	}
	document.getElementById("TXTBox"+temp).style.visibility="visible";
	document.getElementById("TXTstatus"+temp).innerHTML="<br/>The game has been paused. To resume the game, press OK button.<div align='center' class='RedButtonMenu' style='position:relative;top:15;'> <a onClick='UnpauseGame()' class='buttons'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OK&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a></div>";
}
function UnpauseGame()
{
	var temp=((Level+1)%2)+1;
	gamePaused=0;
	switch(gameState)
	{
		case 1:timerBall2=setInterval("hoverBall()",5);//060909 was 20
				break;
		default:timerBall=setInterval("moveBall()",5);//060909 was 20
				break;
	}
	document.getElementById("TXTBox"+temp).style.visibility="hidden";
}
function DropBall()
{
	var temp=((Level+1)%2)+1,leftmost=320,m=0,i;
	//clearInterval(timerBall2);
	gameState=2;
	while(m<10)
	{
		if(ballX[m]<leftmost&&ballStatus[m]==1)
		{
			leftmost=ballX[m];
			i=m;
		}
		m++;
	}
	if(leftmost==320)
		return;
	else
		document.getElementById("dropArea"+temp).innerHTML="<tr height='9'><td width='8' background='images/DropBarArea3.gif'></td><td width='"+(ballX[i]-66)+"' background='images/DropBarArea2.gif'></td><td width='8' background='images/DropBarArea3.gif'></td><td width='18'></td><td width='8' background='images/DropBarArea3.gif'></td><td width='"+(259-ballX[i]+32)+"' background='images/DropBarArea2.gif'></td><td width='8' background='images/DropBarArea3.gif'></td></tr>";
		speedX[i]=-50; speedY[i]=0;
		ballStatus[i]=2;
		clearInterval(timerBall);
		count[i]=0;
		timerBall=setInterval("moveBall()",5);//060909 was 20
		setTimeout("DroppedBall()",1000);
}
function DroppedBall()
{
	var temp=((Level+1)%2)+1;
	document.getElementById("dropArea"+temp).innerHTML="<tr height='9'><td width='8' background='images/DropBarArea3.gif'></td><td width='259' background='images/DropBarArea2.gif'></td><td width='8' background='images/DropBarArea3.gif'></td></tr>";
}
function BlinkOpening(type,count)
{
	//alert("here");
	if(gamePaused==1)
	{
		bonusTimer=setTimeout("BlinkOpening("+type+","+count+")",50+((bonusType[type]-2)*200));
		return;
	}
	if(bonusX==10||bonusY==10)
		return;
	if(holesStatus[bonusX][bonusY]<10)
	{
		document.getElementById("hole"+bonusX+"_"+bonusY).src="images/holeRegular.gif";
		return;
	}
	count--;
	if(count%2==1)
		document.getElementById("hole"+bonusX+"_"+bonusY).src="images/holeBonus"+(bonusType[type]-1)+".gif";
	else
	{
		document.getElementById("hole"+bonusX+"_"+bonusY).src="images/holeRegular.gif";
	}
	if(count>=0)
		bonusTimer=setTimeout("BlinkOpening("+type+","+count+")",50+((bonusType[type]-2)*200));
	else
	{
		holesStatus[bonusX][bonusY]=1;
		document.getElementById("hole"+bonusX+"_"+bonusY).src="images/holeRegular.gif";
	}
}
function upAndFade(opacity,t)
{
	if(gamePaused==1)
	{
		setTimeout("upAndFade("+opacity+","+t+")",25);
		return;
	}
	var temp=((Level+1)%2)+1;
	var obj=document.getElementById("txtMsg"+t+""+temp);
    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);
		setTimeout("upAndFade("+opacity+","+t+")",25);
	}
}
function upAndFade2(opacity,t)
{
	if(gamePaused==1)
	{
		setTimeout("upAndFade2("+opacity+","+t+")",25);
		return;
	}
	var temp=((Level+1)%2)+1;
	var obj=document.getElementById("txtMsg"+t+""+temp);
    var object = obj.style;
    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);
		setTimeout("upAndFade2("+opacity+","+t+")",25);
	}
}
function gameOver()
{
	var temp=((Level+1)%2)+1;
	document.getElementById("TXTBox"+temp).style.visibility="visible";
	document.getElementById("TXTstatus"+temp).innerHTML="Game Over<br/>You scored:"+Score+"<br/><div align='center' class='RedButtonMenu' style='position:relative;' ><a class='buttons' href=''>&nbsp;High Scores</a></div><br/><div align='center' class='RedButtonMenu' style='position:relative;top:-20;'><a class='buttons' href=''>&nbsp;More Games</a></div><br/><div align='center' class='RedButtonMenu' style='position:relative;top:-40;'> <a onClick='GoToMenu()' class='buttons'>&nbsp;&nbsp;Play Again&nbsp;&nbsp;</a></div>";
	var temp2=GetCookie("BounceTrap");
	if((temp2=="")||(parseInt(temp2)==0)||(parseInt(temp2)<Score))
	{
		SetCookie("BounceTrap",Score);
		document.getElementById("highscore"+temp).innerHTML=Score;
	}
}
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));
}
