var score = 0;

var total_questions = 15;

Event.observe(window, 'load', init, false);

function init() {  
  if ($('score')) {
    new Ajax.Updater('question','/bb/quizzes/keyboard.php?score='+$('score').value+'&random='+random_int());
    $('morestuff').style.display='block';
  } else {
    new Ajax.Updater('question','/bb/quizzes/keyboard.php?question=0&random='+random_int());
  } 
  
}

function answer(question_id,answer) {
  
  $('question').innerHTML = 'Loading Next Question...';
  
  new Ajax.Request('/bb/quizzes/keyboard.php',
    {
      method:'get',
      parameters: { question: question_id, answer: answer, random: random_int() },
      onSuccess: function(transport){
        var response = transport.responseText || "no response text";
        
        if (response.match(/POINTS:(\d+)/)) {
        
          score += parseFloat(RegExp.$1);
          
          percentage = Math.floor((score / (total_questions * 5)) * 100);
          
          $('progress').style.width = (percentage * 4) + 'px';
          
        } 
        
        question_id++;
        
        if (question_id >= total_questions) {
          dest_url = escape('www.oneplusyou.com/bb/keyboard?score=' + percentage);
          window.location = "http://www.oneplusyou.com/q/v/m2_interstitial?dest_url=" + dest_url + '&view_key=keyboard';
          //new Ajax.Updater('question','/bb/quizzes/keyboard.php?score='+percentage+'&random='+random_int());
          //$('morestuff').style.display='block';
        } else {
          new Ajax.Updater('question','/bb/quizzes/keyboard.php?question='+question_id+'&random='+random_int());
        }
        
      },
      onFailure: function(){ alert('Something went wrong...') }
    });
}


function random_int() {
 return Math.floor(Math.random()*100000);
}