Qualtrics.SurveyEngine.addOnload(function()
{
	/*Place your JavaScript here to run when the page loads*/
	// standard version of the BART
	
	

$(document).ready(function() { 

  // initialize values
  const ExpStart = Date.now();//record start of experiment
	
// old values from the BART task	
  var round = 0;
  var start_size = 150; // start value of width & height of the image; must correspond to the value that is specified for the #ballon id in style.css
  var increase = 2; // number of pixels by which balloon is increased each pump
  var size; // start_size incremented by 'increase'
  var pumps; 
  var thanks;
  var maximal_pumps=32;
  var pumpmeup; // number pumps in a given round; is updated each round
  var number_pumps = []; // arrays for saving number of pumps
  var total = 0; // money that has been earned in total
  var explosion; // will an explosion occur? 1 = yes, 0 = no
  var last_win = 0; // initialize variable that contains the win of the previous round
  var present_win = 0; //initialize variable of win in present around
  var rounds_played = 32; //change back to 40 changed for Stroop, 30 congruent, 10 incongruent
	var totalTimeCongruent=0;
	var totalTimeIncongruent=0;
	var aveTimeCongruent = 0;
	var aveTimeIncongruent=0;
	
	var RedCorrect ;
	var BlueCorrect;
	var GreenCorrect ;
	var YellowCorrect ;

	var roundchoice = 0;//Stroop variable added to track how many colors they've chosen, 1-2-3 and 4
	//might just save this variable as red choice, blue choice, green choice, yellow choice			
	
  var exploded = []; // array for saving whether ballon has exploded
  var incongruent = []; //array for saving the trial type in the stroop task
	var errors = [];// array for MG that saves the outcomes
	var RTs = []; // array for saving response times 
	var RedCorrects = []; //correct response order for red 
	var BlueCorrects = [];
	var GreenCorrects = [];
	var YellowCorrects = [];
	var RedRTs = [];//array for saving RT for picking red
	var BlueRTs = [];
	var GreenRTs = [];
	var YellowRTs = [];
	
	var TrialTypes = [];//incongruent above might be uneccessary, but it also checks that the experiment is running correctly
	var onsets = [];
	var incongruentT
	var errorT
	var RTT
	

	
	var RedRTT ;
	var BlueRTT;
	var GreenRTT ;
	var YellowRTT ;
	
	var onsetT
	var Time2
	var Time3
	var TimeA
	var TimeB
	var TimeC
	var TimeD
	
	
	var trialStart
  //var explode_array =  [15, 9,  16, 26, 22,  8, 27,  17, 23,  29,];
  var BurstProb; //Random variable that determines whether balloon burst
	
	
//new values added in for mixed gambles /// now redundant for stroop task
var GainT

var LossT //potential gains and losses per trial
var gainVal
var lossVal

	
var gains = []
for (let i = 10; i < 41; i+=6) {
  gains = gains.concat(i)
}
var losses = []
for (let i = 5; i < 21; i+=3) {
  losses = losses.concat(i)
}

console.log(gains)
console.log(losses)

gainsL = [] //or combos = new Array(2);
lossesL = []
for(var i = 0; i < gains.length; i++)
{
     for(var j = 0; j < losses.length; j++)
     {

        gainsL.push(gains[i])
        lossesL.push(losses[j])
     }
}

console.log(gainsL)
console.log(gainsL.length)
console.log(lossesL)
console.log(lossesL.length)
//this gets us the combos we want

rewardMatrix = gainsL.concat(lossesL)
console.log(rewardMatrix)
console.log(rewardMatrix[12])

	
	
// this is for the stroop task again	
var trialnums = []
for (let z = 0; z < rounds_played; z+=1) {
  trialnums = trialnums.concat(z)
}
console.log(trialnums)



let unshuffled = trialnums

let shuffled = unshuffled
  .map((a) => ({sort: Math.random(), value: a}))
  .sort((a, b) => a.sort - b.sort)
  .map((a) => a.value)
  
console.log(shuffled)

//Stroop trial type added
TrialTypes = []
GainsS = []
	LossesS = []
for(var y = 0; y < trialnums.length; y++)
{
	if (shuffled[y]<(rounds_played/4)){
    TrialTypes[y] = 1//1 is for incongruent
	} else if (shuffled[y]>=(rounds_played/4)){
    TrialTypes[y] = 0//0 is for congruent
	}
}
console.log(GainsS)
console.log(LossesS)	

//should now have GainsS and LossesS which are the shuffled gains and losses that should be paired
//together// some of this is redundant for the stroop task
	
	
	
						
  // initialize language
  var label_press = 'Reject Gamble';//changed for MG
  var label_collect = 'Finished';//changed for MG
  var label_red = "red"
  var label_blue = "blue"
  var label_green = "green"
  var label_yellow = "yellow"//added these labels and buttons in for this task
  var label_balance = 'Total credit:';
  var label_last = 'Outcome last round:';
  var label_currency = ' points';
  var label_times=' times';
  var label_header= 'Select the font color from top to bottom; Trial ';
  var label_gonext1 = 'Start next round';
  var label_gonext2 = 'end game';
  var msg_explosion1 = '<p> The balloon has exploded after pumping ';
  var msg_explosion2 = ' times.</p> <p>You did not make any money this round </p>';
	

	
  var label_present_time = 'Pumping times:';
  var label_present_value = 'Earn this round:';		

  //var msg_gamble1 = '<p> Choose to accept or reject the gamble </ p> <p> There is a 50% chance of gaining ';
  //var msg_gamble2 = ' points. </ p> <p> But, there is a 50% chance of losing ';
  //var msg_gamble3 = ' points.  </p>';
	
		 var msg_RedWord = '<p> RED </ p> <p>' ;
		var msg_BlueWord = '<p> RED </ p> <p>' ;
	  	var msg_GreenWord = '<p> GREEN </ p> <p>' ;
	 	 var msg_YellowWord = '<p> YELLOW </ p> <p>' ;
	  
  var msg_lost2 = ' points. </ p> <p> These points have been subtracted from your total.</p>';	

	 //probably change what word is put in here - this is the congruent position 
	var msg_Red = "<span style='color:red'>"+msg_RedWord+"</span>"
	var msg_Blue = "<span style='color:blue'>"+msg_BlueWord+"</span>"
	var msg_Green = "<span style='color:green'>"+msg_GreenWord+"</span>"
	var msg_Yellow = "<span style='color:yellow'>"+msg_YellowWord+"</span>"

	var msg_gamble1 = msg_Red;
    var msg_gamble2 = msg_Blue;
    var msg_gamble3 = msg_Green;
	var msg_gamble4 = msg_Yellow;
	var Letters = [msg_Red,msg_Blue,msg_Green, msg_Yellow];
	var WordLetters = [msg_RedWord, msg_BlueWord, msg_GreenWord, msg_YellowWord];
	
  var msg_collect1 = '<p> Your response has been recorded </ p> <p> This round you took ';
  var msg_collect2 = ' milliseconds to list the order of the font colors. </ p>';
	

	
	
	

  var msg_end1 = '<p> This completes this part of the study. </ p> <p> Congruent Response Time: ';
  var msg_end2 = '</ p> <p> Incongurent Response Time: </ p>';
  
  
  // initialize labels 
  $('#press').html(label_press); 
  $('#collect').html(label_collect);
  $('#red').html(label_red);//added in for Stroop task
  $('#blue').html(label_blue);//added in for Stroop task
  $('#green').html(label_green);//added in for Stroop task
  $('#yellow').html(label_yellow);//added in for Stroop task
	
 // $('#total_term').html(label_balance);
  //$('#total_value').html(total+label_currency);
  //$('#last_term').html(label_last);
  //$('#last_value').html(last_win+label_currency);
  //$('#present_value_term').html(label_present_value);
  //$('#present_times_term').html(label_present_time);
 // $('#present_value').html(present_win+label_currency);
  //$('#present_times').html(pumpmeup+label_times);
  //$('#outcomes').html(number_pumps);
  
  // below: create functions that define game functionality
  // Stroop task change this probably
  var gamble_message  = function() {
    $('#collect').hide();
	  $('#press').hide();
    $('#red').show();   
	$('#blue').show();
	$('#green').show();
	$('#yellow').show();   
	 
	  //shuffle the word order
	var wordnums = []
        for (let z = 0; z < 4; z+=1) {
        wordnums = wordnums.concat(z)
        }
        //console.log(trialnums)

let unshword = wordnums

let shuffword = unshword
  .map((a) => ({sort: Math.random(), value: a}))
  .sort((a, b) => a.sort - b.sort)
  .map((a) => a.value)
  //var Letters = [msg_Red,msg_Blue,msg_Green, msg_Yellow]; 
	  
	msg_gamble1 = Letters[shuffword[0]];
    msg_gamble2 = Letters[shuffword[1]];
    msg_gamble3 = Letters[shuffword[2]];
	msg_gamble4 = Letters[shuffword[3]];
	  
	  for (let i = 0; i < 4; i+=1){
	  if (shuffword[i]===0){
	      RedCorrect=i;
	  } else if (shuffword[i]===1){
	      BlueCorrect=i;
	  } else if (shuffword[i]===2){
	      GreenCorrect=i;
	  } else if (shuffword[i]===3){
	      YellowCorrect=i;
	  }
	  }
//console.log(shuffled)  
	  //show the shuffled words
    $('#message').html(msg_gamble1).show();
	  $('#message2').html(msg_gamble2).show();
	  $('#message3').html(msg_gamble3).show();
	  $('#message4').html(msg_gamble4).show();
  	//$('#message').html(msg_Red).show();
  
  };	
	
	  
	
	
  // new round - or Stroop trial  NEW ROUND START
	
  var new_round = function() {
    //console.log(number_pumps);
    //console.log(exploded);
	  
    $('#gonext').hide();
    $('#message').hide();  
    $('#collect').hide();
    $('#press').hide();
    $('#red').show();   
	$('#blue').show();
	$('#green').show();
	$('#yellow').show();	  
	$('#present_times').hide();
	$('#present_value').hide();
	  Time2 = Date.now();
	  onsetT = (Time2 - ExpStart);
	  //onsets.push(onsetT);
    round += 1;
    size = start_size;  
	  roundchoice = 0; //reset round choice
	  errorT = 0;
	  
	  msg_RedWord = '<p> RED </ p> <p>' ;
	  msg_BlueWord = '<p> BLUE </ p> <p>' ;
	  msg_GreenWord = '<p> GREEN </ p> <p>' ;
	  msg_YellowWord = '<p> YELLOW </ p> <p>' ;
	  
  
	  
	  incongruentT = TrialTypes[round-1];
	
	 //probably change what word is put in here - this is the congruent position 
	 if (TrialTypes[round-1]==0){//coungruent 
	msg_Red = "<span style='color:red'>"+msg_RedWord+"</span>"
	msg_Blue = "<span style='color:blue'>"+msg_BlueWord+"</span>"
	msg_Green = "<span style='color:green'>"+msg_GreenWord+"</span>"
	msg_Yellow = "<span style='color:gold'>"+msg_YellowWord+"</span>"
	
	Letters = [msg_Red,msg_Blue,msg_Green,msg_Yellow]; 
		 
	} else if (TrialTypes[round-1]==1){
		
		WordLetters = [msg_RedWord, msg_BlueWord, msg_GreenWord, msg_YellowWord];
		 //shuffle the word order
		var wordnums = []
        for (let z = 0; z < 4; z+=1) {
        wordnums = wordnums.concat(z)
        }
        //console.log(trialnums)

		let unshword = wordnums

		let shuffword = unshword
  			.map((a) => ({sort: Math.random(), value: a}))
  			.sort((a, b) => a.sort - b.sort)
  			.map((a) => a.value)
	
			var Word1 = WordLetters[shuffword[0]];	
			var Word2 = WordLetters[shuffword[1]]
			var Word3 = WordLetters[shuffword[2]]
			var Word4 = WordLetters[shuffword[3]]
		
			msg_Red = "<span style='color:red'>"+Word1+"</span>"
			msg_Blue = "<span style='color:blue'>"+Word2+"</span>"
			msg_Green = "<span style='color:green'>"+Word3+"</span>"
			msg_Yellow = "<span style='color:gold'>"+Word4+"</span>"
		
		   Letters = [msg_Red,msg_Blue,msg_Green,msg_Yellow];
	}
	
	//var Letters = [msg_Red,msg_Blue,msg_Green,msg_Yellow]; 
	
	
	 //unnecessary stuff from mixed gambles	
    pumps = 0;
	present_win =0;
	 //potential gain and loss for the current round or trial 
	 GainT = GainsS[round-1];
	 LossT = LossesS[round-1]; 

	   
	 gamble_message();// shows the gamble message - which will be the stroop message in Stroop task
	//show_present();
	//show_present_earns();
	 
    $('#ballon').width(size); 
    $('#ballon').height(size);
    $('#ballon').hide();
    $('#round').html('<h2>'+label_header+round+'<h2>');
	$('#present_round').hide();
	$('#outcomes').hide();
	$('#NextButton').hide();
  };
	

	
  
  // what happens when the game ends
  var end_game = function() {
	  
	  aveTimeIncongruent=(totalTimeIncongruent/(rounds_played/4));
	  aveTimeCongruent=(totalTimeCongruent/((rounds_played*3)/4));
	  
    $('#total').remove();
    $('#collect').remove();
    $('#ballon').remove();
    $('#press').remove();
    $('#gonext').remove();
    $('#round').remove();
    $('#last_round').remove();
    $('#goOn').show();
    $('#message').html(msg_end1+aveTimeCongruent+msg_end2+aveTimeIncongruent).show();
	$('#outcomes').show();
	$('#NextButton').show();
	 // will need to add something here 
	Qualtrics.SurveyEngine.setEmbeddedData('incongruent',incongruent);
      Qualtrics.SurveyEngine.setEmbeddedData('errors',errors);
	Qualtrics.SurveyEngine.setEmbeddedData('onsets',onsets.join(","));
	  Qualtrics.SurveyEngine.setEmbeddedData('RTs',RTs.join(","));//DAW added this join on 1/13/22 to try to save the data as a string
	  
	  Qualtrics.SurveyEngine.setEmbeddedData('RedRTs',RedRTs.join(","));
	  Qualtrics.SurveyEngine.setEmbeddedData('BlueRTs',BlueRTs.join(","));
	  Qualtrics.SurveyEngine.setEmbeddedData('GreenRTs',GreenRTs.join(","));
	  Qualtrics.SurveyEngine.setEmbeddedData('YellowRTs',YellowRTs.join(","));
	  
	    Qualtrics.SurveyEngine.setEmbeddedData('RedCorrects',RedCorrects);
	  Qualtrics.SurveyEngine.setEmbeddedData('BlueCorrects',BlueCorrects);
	  Qualtrics.SurveyEngine.setEmbeddedData('GreenCorrects',GreenCorrects);
	  Qualtrics.SurveyEngine.setEmbeddedData('YellowCorrects',YellowCorrects);

	
  };
	

  
  // message shown if balloon explodes
  var explosion_message = function() {
    $('#collect').hide();
    $('#press').hide();
    $('#message').html(msg_explosion1+pumpmeup+msg_explosion2).show();
  };
  
  // message shown if balloon does not explode
  var collected_message = function() {
    $('#collect').hide();
    $('#press').hide();
   $('#red').hide();   
	$('#blue').hide();
	$('#green').hide();
	$('#yellow').hide(); 	  
    $('#message').html(msg_collect1+RTT+msg_collect2).show();
	  $('#message2').hide(); 
	  $('#message3').hide(); 
	  $('#message4').hide(); 
  };
	
  // message shown if balloon does not explode
  var lost_message = function() {
    $('#collect').hide();
    $('#press').hide();    
    $('#message').html(msg_lost1Red+outcomeT+msg_lost2Red).show();
  };	

  // animate explosion using $ UI explosion
  var balloon_explode = function() {
    $('#ballon').hide( "explode", {pieces: 100}, 800 );
    document.getElementById('explosion_sound').play();
  };  
  
  // show button that starts next round
  var gonext_message = function() {
    $('#ballon').hide();
	$('#collect').hide();
    $('#press').hide();
    $('#red').hide();   
	$('#blue').hide();
	$('#green').hide();
	$('#yellow').hide();  
    if (round < rounds_played) {
      $('#gonext').html(label_gonext1).show();
    }
    else {
      $('#gonext').html(label_gonext2).show();
    }
  };

  // add money to bank
  var increase_value = function() {
    $('#total_value').html(total+label_currency);
  };
  
  var show_last = function() {
    $('#last_value').html(last_win+label_currency);
  };
  var show_present=function(){
	$('#present_times').html(pumps+label_times)
  }
  var show_present_earns=function(){
	$('#present_value').html(present_win+label_currency)
  }
   var show_array=function(){
	$('#pump_array').html(number_pumps)
   }
  
  // button functionalities
  
 
	
	
	  // red button
   $('#red').click(function() {
	   
	   if (roundchoice===RedCorrect){
	   		TimeA = Date.now();
		   RedRTT = (TimeA - Time2)/1000;
		   
		   roundchoice+=1
	   choiceT = 1;
	   outcomeT = 0;
	   
	   $('#red').hide();  
	   } else {
	   		errorT = 1; // log the error
	   }
	   
	   	if (roundchoice>=4){
	   Time3 = Date.now();
	  RTT = (Time3 - Time2)/1000;
	  
	  choiceT = 2;
	
		 collected_message();
		  gonext_message();
		
							RTs.push(RTT);
	           onsets.push(onsetT);
			errors.push(errorT);
			incongruent.push(incongruentT);
			RedCorrects.push(RedCorrect);
			BlueCorrects.push(BlueCorrect);
			GreenCorrects.push(GreenCorrect);
			YellowCorrects.push(YellowCorrect);
			RedRTs.push(RedRTT);
			BlueRTs.push(BlueRTT);
			GreenRTs.push(GreenRTT);
			YellowRTs.push(YellowRTT);
			
			if (incongruentT===1){
			totalTimeIncongruent+=RTT 
			} else if (incongruentT===0) {
			totalTimeCongruent+=RTT
			}
			
    if (round < rounds_played) {
      new_round();
    }
    else {
      end_game();
    }
		  
	
	$('#ballon').hide();
	$('#present_round').hide();	
	}
	  
  });//end of click function
	
	  // blue button
   $('#blue').click(function() {
	    if (roundchoice===BlueCorrect){
			TimeB = Date.now();
		   BlueRTT = (TimeB - Time2)/1000;
			
	   roundchoice+=1
	   choiceT = 1;
	   outcomeT = 0;
	   
	   $('#blue').hide();
		} else {
	   		errorT = 1; // log the error
	   }
	   
	   	if (roundchoice>=4){
	   Time3 = Date.now();
	  RTT = (Time3 - Time2)/1000;
	  
	  choiceT = 2;
	
		 collected_message();
		  gonext_message();
							RTs.push(RTT);
	           onsets.push(onsetT);
			errors.push(errorT);
			incongruent.push(incongruentT);
			RedCorrects.push(RedCorrect);
			BlueCorrects.push(BlueCorrect);
			GreenCorrects.push(GreenCorrect);
			YellowCorrects.push(YellowCorrect);
			RedRTs.push(RedRTT);
			BlueRTs.push(BlueRTT);
			GreenRTs.push(GreenRTT);
			YellowRTs.push(YellowRTT);	
			
			if (incongruentT===1){
			totalTimeIncongruent+=RTT 
			} else if (incongruentT===0) {
			totalTimeCongruent+=RTT
			}
			
    if (round < rounds_played) {
      new_round();
    }
    else {
      end_game();
    }
		  
	
	$('#ballon').hide();
	$('#present_round').hide();	
	}
	  
  });//end of click function
	
	  // green button
   $('#green').click(function() {
	    if (roundchoice===GreenCorrect){
		  TimeC = Date.now();
		   GreenRTT = (TimeC - Time2)/1000;
	   roundchoice+=1
	   choiceT = 1;
	   outcomeT = 0;
	  $('#green').hide();
		} else {
	   		errorT = 1; // log the error
	   }
	   
	   	if (roundchoice>=4){
	   Time3 = Date.now();
	  RTT = (Time3 - Time2)/1000;
	  
	  choiceT = 2;
	
		 collected_message();
		  gonext_message();
			
							RTs.push(RTT);
	           onsets.push(onsetT);
			errors.push(errorT);
			incongruent.push(incongruentT);
			RedCorrects.push(RedCorrect);
			BlueCorrects.push(BlueCorrect);
			GreenCorrects.push(GreenCorrect);
			YellowCorrects.push(YellowCorrect);
			RedRTs.push(RedRTT);
			BlueRTs.push(BlueRTT);
			GreenRTs.push(GreenRTT);
			YellowRTs.push(YellowRTT);
			
			if (incongruentT===1){
			totalTimeIncongruent+=RTT 
			} else if (incongruentT===0) {
			totalTimeCongruent+=RTT
			}
					
			
    if (round < rounds_played) {
      new_round();
    }
    else {
      end_game();
    }
		  
	
	$('#ballon').hide();
	$('#present_round').hide();	
	}
	   
	   
  });//end of click function
	
	  // yellow button
   $('#yellow').click(function() {
	   
	    if (roundchoice===YellowCorrect){
			
		   TimeD = Date.now();
		   YellowRTT = (TimeD - Time2)/1000;
			
	   roundchoice+=1
	   choiceT = 1;
	   outcomeT = 0;
	  $('#yellow').hide();
		} else {
	   		errorT = 1; // log the error
	   }
	   
	   	if (roundchoice>=4){
	   Time3 = Date.now();
	  RTT = (Time3 - Time2)/1000;
	  
	  choiceT = 2;
	
		 collected_message();
		  gonext_message();
			
							RTs.push(RTT);
	           onsets.push(onsetT);
			errors.push(errorT);
			incongruent.push(incongruentT);
			RedCorrects.push(RedCorrect);
			BlueCorrects.push(BlueCorrect);
			GreenCorrects.push(GreenCorrect);
			YellowCorrects.push(YellowCorrect);
			RedRTs.push(RedRTT);
			BlueRTs.push(BlueRTT);
			GreenRTs.push(GreenRTT);
			YellowRTs.push(YellowRTT);		
			
			if (incongruentT===1){
			totalTimeIncongruent+=RTT 
			} else if (incongruentT===0) {
			totalTimeCongruent+=RTT
			}		
			
			
    if (round < rounds_played) {
      new_round();
    }
    else {
      end_game();
    }
	
	$('#ballon').hide();
	$('#present_round').hide();	
	}
	   
  });//end of click function	
	
  
  // collect in MG was if they accepted the gamble
	//collect in Stroop is going to be like Done
  //$('#collect').click(function() {
	  
	//   Time3 = Date.now();
	//  RTT = (Time3 - Time2);
	  
	//  choiceT = 2;
	
	//	collected_message();
	//	  gonext_message();
	
		  
	
	//$('#ballon').hide();
	//$('#present_round').hide();

      //end of the if statement for pumps >0
//  });
	

	
	//choices.push(choiceT); // save whether balloon has exploded or not
	//outcomes.push(outcomeT);
	//RTs.push(RTT);
	//onsets.push(onsetT);	
  
  // click this button to start the next round (or end game when all rounds are played)
  $('#gonext').click(function() {
    if (round < rounds_played) {
      new_round();
    }
    else {
      end_game();
    }
  });  


  // start the game!
  new_round();
  
});

});

Qualtrics.SurveyEngine.addOnReady(function()
{
	/*Place your JavaScript here to run when the page is fully displayed*/

});

Qualtrics.SurveyEngine.addOnUnload(function()
{
	/*Place your JavaScript here to run when the page is unloaded*/

});