//Screen Animation variables //moves:no. of moves to change screen
var Img,Img2,timer,timer2,afterAnim,lastTop,lastLeft,lastLeft2,the_style,movLeft,animType,screenWidth=320,moves=15,adsTimer,adsTimeSet=5,pauseTime=1000;
//Game Variables
var numColor,numPos,numCount,numSum,numPlacedLeft,numPlacedTop1,numPlacedTop2,numPlacedTop3,currPos,currChip,clearAll,gameEnd,spinned,chipPos,chipType,totalBet,Cash,chipPlacedGap=10,wheelPoint,prevResults,resultsCount,colorResults;
//WheelSpin Variables
var spinTime=2,speedLevels=10,wheel1,wheel2,wheel3,timer3,currLevel,currCol,movGap,setCount,movLeftWheel,finalPos,colWidth=54,stopASAP,count,setCount,wheelSpeed=500,currPoint1,currPoint2,currPoint3;
//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 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="";
	adsTimer=adsTimeSet-1;
	numPos=new Array(1,38,27,10,25,29,12,8,19,31,18,6,21,26,16,4,23,35,14,2,37,20,9,28,32,11,30,33,7,17,5,22,34,15,3,24,36,13);
	currCol=20;
	//0-red, 1- black, 2-green
	numColor=new Array(0,2,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,2,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1);
	bgColor=new Array("#d00000","#000000","#0fbe00");
	//position of coins
	chipType=new Array(1,5,25,100);
	chipPos=new Array(130,185,240,295);
	numSum=new Array();
	numCount=new Array();
	numPlacedTop1=new Array(125,148,170,190,210,232,252,272,294,314,334,354);
	numPlacedTop2=new Array(103,103,376,376,376);
	numPlacedTop3=new Array(140,222,305,125,334,170,294,210,252);
	numPlacedLeft=new Array(120,168,216,135,197,120,168,216,97,70);
	animType=0;
	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;
	afterAnim="timer2=setInterval('adsTime()',1000);";
	lastLeft=-2*screenWidth;lastLeft2=lastLeft;
	animType=0;
	selectChip(0);
	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;
	prevResults=new Array(0,0,0,0,0);
	//0-lose 1-win
	colorResults=new Array(0,0,0,0,0);
	resultsCount=0;
	timer=setInterval("AnimateScreen()",10);
}
function loadScreen2()
{
	var temp=GetCookie("Roulette");
	if((temp=="")||(parseInt(temp)==0))
		Cash=500;
	else
	Cash=parseInt(temp);
	document.getElementById("cashScore").innerHTML=Cash;
	//alert(temp);
	gameEnd=0;
	document.getElementById("TXTBox").style.top="220";
	document.getElementById("TXTBox").style.height="120";
	document.getElementById("TXTstatus").innerHTML="To Play, select your bet amount and then select you bet.<br/><br/>When all bets are placed <br/>press SPIN<br/>";
	document.getElementById("TXTBox").style.visibility="visible";
	document.getElementById("OKButton").style.visibility="visible";
}
function newMove()
{
	var i,Results;
	spinned=0;
	totalBet=0;
	document.getElementById("TXTBox").style.visibility="hidden";
	document.getElementById("TXTBox").style.height="80";
	clearCoins();
	document.getElementById("OKButton").style.visibility="hidden";
	Results="";
	for(i=0;i<5&&prevResults[i]!=0;i++)
	{
		switch(colorResults[i])
		{
			case 0: Results+="<span class='LoseResult'>";
					break;
			case 1: Results+="<span class='WinResult'>";
					break;
		}
		switch(prevResults[i])
		{
			case 37: Results+=" 0</span>";
					break;
			case 38: Results+=" 00</span>";
					break;
			default: Results+=" "+prevResults[i]+"</span>";
					break;
		}
	}
	document.getElementById("results").innerHTML=Results;
	if(Cash==0)
	{
		spinned=1;
		endGame();
	}
}
function endGame()
{
	document.getElementById("TXTstatus").innerHTML="Game Over<br/>You are out of money!<br/><span class='WinStatus'>Get money from Cashier?</span><br/><a class='RedButton' style='position:relative;top:40;' onClick='loadScreen2();'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;YES&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a><br/><a onClick='goToMenu()' class='RedButton' style='position:relative;top:60;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NO&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>";
	document.getElementById("TXTBox").style.visibility="visible";
	document.getElementById("TXTBox").style.top="150";
	document.getElementById("TXTBox").style.height="155";
	document.getElementById("TXTBox").style.zIndex=50;
	gameEnd=1;
}
function clearCoins()
{
	if(spinned==0)
	{
		var i;
		for(i=0;i<51;i++)
			numSum[i]=numCount[i]=0;
		clearAll=1;
		Cash+=totalBet;
		totalBet=0;
		document.getElementById("betScore").innerHTML=totalBet;
		document.getElementById("cashScore").innerHTML=Cash;
		document.getElementById("coinsPlaced").innerHTML="";
	}
}
function selectBox(n)
{
	if(spinned==0)
	{
		if(Cash<chipType[currChip])
		{
			document.getElementById("TXTBox").style.visibility="visible";
			document.getElementById("TXTstatus").innerHTML="Not Enough Balance";
			document.getElementById("TXTBox").style.height="50";
			setTimeout("removeBox()",pauseTime);
			return;
		}
		else
		{
			numSum[n]+=chipType[currChip];
			totalBet+=chipType[currChip];
			Cash-=chipType[currChip];
			document.getElementById("betScore").innerHTML=totalBet;
			document.getElementById("cashScore").innerHTML=Cash;
			//alert(chipType[currChip]+" "+numSum[n]);	
			var chipDiv=document.createElement("div");
			var topImg,leftImg,chipImg="<img src='images/"+chipType[currChip]+"chipPLACED.gif' onClick='selectBox("+n+");'";
			if(n<37)
			{
				topImg=numPlacedTop1[Math.floor((n-1)/3)];
				leftImg=numPlacedLeft[(n-1)%3]+(chipPlacedGap*(numCount[n]%4));
			}
			else if(n<42)
			{
				leftImg=numPlacedLeft[n-34]+(chipPlacedGap*(numCount[n]%4));
				topImg=numPlacedTop2[n-37];
			}
			else if(n<45)
			{
				leftImg=numPlacedLeft[8];
				topImg=numPlacedTop3[n-42]+(chipPlacedGap*(numCount[n]%4));
			}
			else
			{
				leftImg=numPlacedLeft[9];
				topImg=numPlacedTop3[n-42]+(chipPlacedGap*(numCount[n]%4)*2/3);
			}
			if(navigator.appName=="Microsoft Internet Explorer")
				leftImg-=115;
			chipImg+=" style='position:absolute; left:"+leftImg+";top:"+topImg+";'/>";
			chipDiv.innerHTML=chipImg;
			numCount[n]++;
			document.getElementById("coinsPlaced").appendChild(chipDiv);
			clearAll=0;
		}
	}
}
function selectChip(i)
{
	currChip=i;
	document.getElementById("chipActive").style.top=chipPos[i];
	document.getElementById("arrowActive").style.top=chipPos[i]+10;
	
}
function goToMenu()
{
	document.getElementById("TXTBox").style.top="220";
	document.getElementById("TXTBox").style.height="80";
	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;
	if(gameEnd==0)
		document.getElementById("returnToGame").style.visibility="visible";
	else
	{
		document.getElementById("returnToGame").style.visibility="hidden";
		document.getElementById("TXTBox").style.visibility="hidden";
	}
	timer=setInterval("AnimateScreen()",10);
}
function returnToGame()
{
	Img=document.getElementById("screen2");
	Img2=document.getElementById("screen4");
	movLeft=-1*screenWidth/moves;
	afterAnim="";
	lastLeft=-2*screenWidth;lastLeft2=parseInt(Img2.style.left)-screenWidth;
	animType=0;
	timer=setInterval("AnimateScreen()",10);
}
function spinWheel()
{
	if(clearAll==1)
	{
		document.getElementById("TXTBox").style.visibility="visible";
		document.getElementById("TXTstatus").innerHTML="Select a bid before spinning!";
		document.getElementById("TXTBox").style.height="50";
		setTimeout("removeBox()",pauseTime);
		return;
	}
	if(spinned==0)
	{
		wheel1=document.getElementById("wheel1");
		wheel2=document.getElementById("wheel2");
		wheel3=document.getElementById("wheel3");
		currLevel=0;
		stopASAP=0;
		spinned=1;
		wheelPoint=Shuffle(37)-1;
		setAnimateWheel();
	}
}
function setAnimateWheel()
{
	currLevel++;
	setCount=Math.floor(1000*spinTime/(speedLevels*20));
	count=0;
	if(currLevel<=speedLevels)
		movGap=(wheelSpeed*(11-currLevel)+200)/100;
	//alert(currCol+" "+wheelPoint);
	timer3=setInterval("AnimateWheel()",20);
}
function AnimateWheel()
{
	wheel1.style.left = parseFloat(wheel1.style.left) + movGap;
	wheel2.style.left = parseFloat(wheel2.style.left) + movGap;
	wheel3.style.left = parseFloat(wheel3.style.left) + movGap;
	count++;
	if(count>setCount)
	{
		clearInterval(timer3);
		setAnimateWheel();
		return;
	}
	var temp=null;
	if(parseInt(wheel1.style.left)>70)
		temp=wheel1;
	else if(parseInt(wheel2.style.left)>70)
		temp=wheel2;
	else if(parseInt(wheel3.style.left)>70)
		temp=wheel3;
	if(stopASAP==1&&currCol!=wheelPoint)
	{
		if(parseInt(currPoint2.style.left)>7)
		{
			clearInterval(timer3);
			currPoint2.style.left=(70-colWidth)/2;
			currPoint1.style.left=((70-colWidth)/2)-colWidth;
			currPoint3.style.left=((70-colWidth)/2)+colWidth;
			ComputeCost();
			return;
			//alert("finallyhere");
		}
	}
	if(temp!=null)
	{
		//alert("here");
		currCol=(currCol+37)%38;;
		temp.style.left = parseFloat(temp.style.left)-3*colWidth;
		temp.style.backgroundColor=bgColor[numColor[currCol]];
		temp.innerHTML=numPos[currCol];
		if(numPos[currCol]>36)
		{
			if(numPos[currCol]==37)
				temp.innerHTML="0";
			else
				temp.innerHTML="00";
		}
		currPoint3=currPoint2;
		currPoint2=currPoint1;
		currPoint1=temp;
		if(currLevel>speedLevels&&currCol==wheelPoint)
			stopASAP=1;
	}
}
function removeBox()
{
	document.getElementById("TXTBox").style.visibility="hidden";
	document.getElementById("TXTBox").style.height="80";
}
function ComputeCost()
{
	var i,j,win=0,loss=0,winAmt=0;
	for(i=1;i<39;i++)
	{
		loss+=numSum[i];
	}
	win=numSum[numPos[wheelPoint]];
	loss-=win;
	winAmt=win*35;//pays 35 to 1
	if(numPos[wheelPoint]<37)
	{
		for(i=39;i<42;i++)
		{
			if(i-39==(numPos[wheelPoint]-1)%3)
			{
				win+=numSum[i];
				winAmt+=numSum[i]*2; //pays 2 to 1
			}
			else
				loss+=numSum[i];
		}
		for(i=42;i<45;i++)
		{
			if(i-42==Math.floor((numPos[wheelPoint]-1)/12))
			{
				win+=numSum[i];
				winAmt+=numSum[i]*2; //pays 2 to 1
			}
			else
				loss+=numSum[i];
		}
		for(i=45;i<47;i++)
		{
			if(i-45==Math.floor((numPos[wheelPoint]-1)/18))
			{
				win+=numSum[i];
				winAmt+=numSum[i]; //pays 2 to 1
			}
			else
				loss+=numSum[i];
		}
		if(numPos[wheelPoint]%2==0)
		{
			win+=numSum[47];
			winAmt+=numSum[47]; //pays 1 to 1
			loss+=numSum[48];
		}
		else
		{
			win+=numSum[48];
			winAmt+=numSum[48]; //pays 1 to 1
			loss+=numSum[47];
		}
		switch(numColor[wheelPoint])
		{
			case 0:	win+=numSum[49];
					winAmt+=numSum[49]; //pays 1 to 1
					loss+=numSum[50];
					break;
			case 1:	win+=numSum[50];
					winAmt+=numSum[50]; //pays 1 to 1
					loss+=numSum[49];
					break;
			case 2:	loss+=numSum[49]+numSum[50];
					break;
		}
	}
	document.getElementById("TXTBox").style.visibility="visible";
	if(resultsCount==5)
		{
			var i;
			for(i=0;i<4;i++)
			{
				prevResults[i]=prevResults[i+1];
				colorResults[i]=colorResults[i+1];
			}
			resultsCount--;
		}
	prevResults[resultsCount]=numPos[wheelPoint];
	if(winAmt>loss)
	{
		var temp=winAmt-loss;
		document.getElementById("TXTstatus").innerHTML="<span class='WinStatus'>You win! <br/> Winning Amount:$"+temp+"</span><br/><br/>";
		colorResults[resultsCount]=1;
	}
	else
	{
		var temp=loss-winAmt;
		document.getElementById("TXTstatus").innerHTML="<span class='WinStatus'>You Lost:$"+temp+"</span><br/><br/><br/>";
		colorResults[resultsCount]=0;
	}
	resultsCount++;
	totalBet=0;
	Cash+=win+winAmt;
	SetCookie("Roulette",Cash, exp);
	document.getElementById("betScore").innerHTML=totalBet;
	document.getElementById("cashScore").innerHTML=Cash;
	document.getElementById("OKButton").style.visibility="visible";
}
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 DeleteCookie (name) {
var exp = new Date();  
exp.setTime (exp.getTime() - 1);  
var cval = GetCookie (name);  
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
function getCookieVal (offset) {  
var endstr = document.cookie.indexOf (";", offset);  
if (endstr == -1)    
endstr = document.cookie.length;  
return unescape(document.cookie.substring(offset, endstr));
}
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("");
}