var score  = 0;

var total_questions = 15;

var max_score = total_questions * 5;

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

function init() {  
  if ($('score')) {
    new Ajax.Updater('question','/bb/apple_addiction/1?score='+$('score').value+'&random='+random_int());
  } else {
    new Ajax.Updater('question','/bb/apple_addiction/1?question_id=0&random='+random_int());
  } 
}


function answer(question_id,answer_id) {
  
  $('question').innerHTML = 'Loading Next Question...';
  
  new Ajax.Request('/bb/apple_addiction/1',
    {
      method:'get',
      parameters: { question_id: question_id, answer_id: answer_id, random: random_int() },
      
      onSuccess: function(transport){
        
        var response = transport.responseText || "no response text";
        
        if (response.match(/SCORE:(\d+)/)) {
        
          score += parseFloat(RegExp.$1);

          percentage = Math.floor((score / max_score) * 100);
          
          $('needle_full').style.height = 137 + Math.floor(percentage) + 'px';

        }
        
        question_id++;
        
        if (question_id >= total_questions) {
          dest_url = escape('www.oneplusyou.com/bb/apple_addiction?score=' + percentage);
          window.location = "http://www.oneplusyou.com/q/v/m2_interstitial?dest_url=" + dest_url + '&view_key=apple_addiction';
          //new Ajax.Updater('question','/bb/apple_addiction/1?score='+percentage+'&random='+random_int());
          
        } else {
          new Ajax.Updater('question','/bb/apple_addiction/1?question_id='+question_id+'&random='+random_int());
        }
        
      },
      onFailure: function(){ alert('Something went wrong...') }
    });
}


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