var seconds = 0;
var startSeconds = 30;
var begun = false;

var clickCount = 0;

function begin() {

  if (!begun) {  
    document.getElementById('countdown_timer').style.display='block';
    document.getElementById('countdown_timer').value = startSeconds;
    countdown();
    begun = true;
  } else {
    document.getElementById('countdown_timer').style.backgroundImage = 'url(/q/img/caffeine/arrows.png)';
    document.getElementById('big_button').style.backgroundImage = 'url(/q/img/caffeine/button.png)';
    increment_clicks();
  }
}

function countdown() {
  seconds += 1;
  document.getElementById('countdown_timer').value = startSeconds - seconds;
  if (document.getElementById('countdown_timer').value > 0) {
    setTimeout("countdown()",1000)
    document.getElementById('countdown_timer').value += ' SECONDS REMAIN';
  } else {
    //document.getElementById('big_button').style.display='none';
    //document.getElementById('countdown_timer').style.display='none';
    document.getElementById('CaffeineClickCount').value = clickCount;
    alert('Times up! Lets see how you did');
    document.getElementById('caffeine_form').submit();
  }
}

function increment_clicks() {
  clickCount++;
  document.getElementById('score_fill').style.width = Math.round((clickCount * 2.28)) + 'px';
  
  if (clickCount % 2 == 0) {
    document.getElementById('countdown_timer').style.backgroundImage = 'url(/q/img/caffeine/arrows_off.png)';
    document.getElementById('big_button').style.backgroundImage = 'url(/q/img/caffeine/button_off.png)';
  }
   
}