// JavaScript Document
//Yo Seven
//Franky Aguilar 
var index = 0;
//index used for interval loop
var diceOneValue;;
//Number for dice one value
var diceTwoValue;
//Number for dice Two value
var throwValue;
//Added value of dice
var throwDone;
//Boolean to check if throw is done or not
var pointRoll = false;
//Point roll establishes if the throw value is a point value
var pointDiceValue;
//Added point dice value
var score = 0;
//Global score
var betValue = 10;
//Bet value
//var hottValue = 2;
var status = "Throw your Dice!"
//Message status
var pointDice = "&nbsp;"
//Point status
//alert(rndNum);

//imgArray is used to hold my dice images
var imgArray = new Array(5);
imgArray[0] = new Image();

imgArray[1] = new Image();
imgArray[1].src = "images/dice_01.png";

imgArray[2] = new Image();
imgArray[2].src = "images/dice_02.png";

imgArray[3] = new Image();
imgArray[3].src = "images/dice_03.png";

imgArray[4] = new Image();
imgArray[4].src = "images/dice_04.png";

imgArray[5] = new Image();
imgArray[5].src = "images/dice_05.png";

imgArray[6] = new Image();
imgArray[6].src = "images/dice_06.png";

function rollDice(){
	//Dice roll, used by roll button function
var stId = setInterval(setImg, 75);
	//Interval to animate dice

function setImg(){
	//Set image by interval
var rndDice1 = Math.ceil(Math.random()*6);
//Random number 1
var rndDice2 = Math.ceil(Math.random()*6);
//Random number 2
document.getElementById("diceOne").src = imgArray[rndDice1].src;
//Change dice 1 by random number
document.getElementById("diceTwo").src = imgArray[rndDice2].src;
//Change dice 2 by random number
document.getElementById("rollBTNImage").src = "images/btn_rollDown.png";
//Change roll button when rolling
index++;
//Increase index with interval

if (index == 20){
	//if Index is 20 stop roll
//index = 0;
//alert("Dice Stop");
diceOneValue = rndDice1;
//Set dice value
diceTwoValue = rndDice2;
//Set dice value
//alert(diceOneValue + diceTwoValue);
throwValue = diceOneValue + diceTwoValue;
//Add dice valued
//alert(throwValue);
clearInterval(stId);
//Clear interval and stop
throwDone = true;
//Throw is done
document.getElementById("rollBTNImage").src = "images/btn_rollUP.png";
//Reset rollBtn img

	//This condition only runs on second roll if a pointRoll has been established
	if(throwDone == true && pointRoll == true){
		
		status = throwValue + " is your Point Dice";
		//Display throw value for status
		pointDice = "Point: " + pointDiceValue;
		//show your point dice value
		
		if(pointDiceValue == throwValue){
			//if your pointDiceValue equals your throwValue
			status = "You Hit your Point!";
			//You Win
			pointDice = "Win";
			//update pointDice status
			pointRoll = false;
			//pointRoll is false, go back to first roll
			index = 0;
			//Reset index for new roll
			score += betValue;
			//increase the score
			updateScore();
			//update score to show winnings
			return false;
			//jump out of the if
			
		} else if(throwValue == 7){
			//if yu hit a seven you lose
			status = "Seven Out! You Lose!";
			pointDice = "Lose!";
			pointRoll = false;
			//pointRoll is false, go back to first roll 
			index = 0;
			//Reset index for new roll
			score -= betValue;
			//decrease the score
			updateScore();
			//update score to show Loosings
			return false
			//jump out of the if
			
		} else {
		//if you dont hit your point or a seven, roll again
		status = "Miss, Roll Again";
		pointDice = "Point: " + pointDiceValue;
		}
		updateScore();
		//Update score updates status and score
	}
	
	//FIRST ROLL
	if(throwDone == true && pointRoll == false){
		//If throw is done and pointRoll is false
		if(throwValue == 2 || throwValue == 3 || throwValue == 12){
		//If you hit a 2, 3, or 12 then you lose
		status = "Craps, You Lose!";
		//Update status
		if(throwValue==2){status = "Snake Eyes";}
		//if you hit a 2 then display Snake Eyes
		//alert("You Lose");
		score -= betValue;
		//Decrease your score from your bet value
		pointDice = "&nbsp;";
		
		
		} else if (throwValue == 7 || throwValue == 11){
		//If you throw a 7 or 11 you win
		status = "You WIN!";
		//update status
		pointDice = "&nbsp;";
		//Point dice status is blank
		
		//Display status depending on 7 or 11
		if(throwValue == 7){status = "7 You WIN!";}
		if(throwValue == 11){status = "11 You WIN!";}
		
		//alert("You Win");
		score += betValue;
		//Increase score
		} else {
		//Else we need to establish point
		status = throwValue + " is your Point Dice";
		//Display status
		pointRoll = true;
		//Point roll is true, so go to first if statement
		pointDiceValue = throwValue;
		//pointDice is the throwvalue
		pointDice = "Point: " + pointDiceValue;
		//Display your point
		}
		
		updateScore();
		}
		index = 0;
}
}
//document.getElementById("betAmm")[0].innerHTML = "betValue";
document.getElementsByTagName("h3")[0].innerHTML = "$" + betValue;
//Update bet value
document.getElementsByTagName("h4")[0].innerHTML = "Score: $" + score;
//Update score value
}//rollDice
function updateScore(){
//UPDATE SCORE
document.getElementsByTagName("h4")[0].innerHTML = "Score: $" + score;
//Update score value
document.getElementsByTagName("h4")[1].innerHTML = pointDice;
//Update pointDice Status
document.getElementsByTagName("h4")[2].innerHTML = status;
//Update status
}
function increaseBet(){
//function triggered by increase bet btn
betValue += 5;
document.getElementsByTagName("h3")[0].innerHTML = "$" + betValue;
//Update bet value
}
function decreaseBet(){
//function triggered by decrease bet btn
betValue -= 5;
document.getElementsByTagName("h3")[0].innerHTML = "$" + betValue;
//Update bet value
}

