var score = 0;

var total_questions = 22;

var gender = 'guy';

function begin(set_gender) {
  if ($('score')) {
    new Ajax.Updater('question','/bb/quizzes/geek.php?score='+$('score').value + '&random=' + random_int());
  } else {
    gender = set_gender;
    new Ajax.Updater('question','/bb/quizzes/geek.php?question=0&random='+random_int);  
  } 
}


function answer(question_id,answer) {
  
  new Ajax.Request('/bb/quizzes/geek.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);
          
          base_height = gender == 'guy' ? 313 : 342;
          
          
          new_height = base_height - Math.floor(base_height * score / (total_questions * 5));
          
          new_height = new_height < 0 ? 0 : new_height; 
          
          $('empty_'+gender).style.height = new_height + 'px';
          
        } 
        
        question_id++;
        
        if (question_id >= total_questions) {
          dest_url = escape('www.oneplusyou.com/bb/geek?score=' + percentage);
          window.location = "http://www.oneplusyou.com/q/v/m2_interstitial?dest_url=" + dest_url + '&view_key=geek';
          //new Ajax.Updater('question','/bb/quizzes/geek.php?score='+percentage+'&random='+random_int());
        } else {
          new Ajax.Updater('question','/bb/quizzes/geek.php?question='+question_id+'&random='+random_int());
        }
        
      },
      onFailure: function(){ alert('Something went wrong...') }
    });
}


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