// Game:  the object to control the game
function Game() {
  // Array to hold questions and their information
  this.questions = new Array();
  this.userScore = '';
  this.userRank = '';
  
  /*** 
    Method: populate()
    Purpose: Fills in the game with the user's answers
  ***/
  this.populate = function() {
    // User score and rank
    try {
      if(this.userRank.length > 0 && this.userScore.length > 0) {
        $("div.game div.hr:first").after("<div class=\"user\" id=\"totals\"><span class=\"labelTotals\">Your Score</span><span class=\"entryScore\">"+this.userScore+"<span class=\"pts_big\">pts</span></span><span class=\"labelTotals\">Your Rank</span><span class=\"entryRank\">"+this.userRank+"</span><div class=\"hr\" style=\"margin-top:10px;\"><!-- --></div></div>");
      }
    }
    catch(e){}
     
    
    // loop through each question
    for (x in this.questions) {
      
      // User did not answer question
      if(this.questions[x]['userAnswer'].length == 0) {continue;}
      
      // Answered question
      else if(this.questions[x]['status'] == 'Answered' || this.questions[x]['status'] == 'Scored') {
        $("#q"+x).after("<div class=\"user\"><span class=\"labelEntryPoints\">Your Points</span> "+this.questions[x]['userPoints']+"<span class=\"pts\">pts</span></div>");
        if(this.questions[x]['type'] == 'Time') {var yourAnswer = convertToTime(this.questions[x]['userAnswer']);}
        else {var yourAnswer = this.questions[x]['userAnswer'];}
        $("#q"+x).after("<div class=\"user\"><span class=\"labelEntryAnswer\">Your Answer</span> "+yourAnswer+"</div>");
        
      }
      
	    // Option input
	    else if(this.questions[x]['type'] == 'Option') {
	      if($("select[name='q"+x+"']").exists()) {
	        $("select[name='q"+x+"']").val(this.questions[x]['userAnswer']);
	      }
	      else {
	        $("input[name='q"+x+"'][value='"+this.questions[x]['userAnswer']+"']").attr("checked","checked");
	      }
	    }
	  
	    // Time input
	    else if(this.questions[x]['type'] == 'Time') {
	      $("input[name='q"+x+"']").val(this.questions[x]['userAnswer']);
	      $("#s"+x).slider("option","value",this.questions[x]['userAnswer']);
	      $("#d"+x).val(convertToTime(this.questions[x]['userAnswer']));
	    }
	  
	    // Numeric input
	    else if(this.questions[x]['type'] == 'Numeric') {
	      $("input[name='q"+x+"']").val(this.questions[x]['userAnswer']);
	      $("#s"+x).slider("option","value",this.questions[x]['userAnswer']);
	    }
	  }
  } // End method: populate()
  
  
  
  /*** 
    Method: clear()
    Purpose: Clears the game of the user's answers
  ***/
  this.clear = function() {
    // Remove user divs
    $(".user").remove();
    
    // Loop through each input
    for (x in this.questions) {
      
      // Question answered
      if(this.questions[x]['status'] == 'Answered' || this.questions[x]['status'] == 'Scored') {
        continue;
      }
      
	    // Option input
	    else if(this.questions[x]['type'] == 'Option') {
	      if($("select[name='q"+x+"']").exists()) {
	        $("select[name='q"+x+"']").val('');
	      }
	      else {
	        $("input[name='q"+x+"']:checked").attr("checked","");
	      }
	    }
	  
	    // Time input
	    else if(this.questions[x]['type'] == 'Time') {
	      $("input[name='q"+x+"']").val('0');
	      $("#s"+x).slider("option","value",'0');
	      $("#d"+x).val(convertToTime('0'));
	    }
	  
	    // Numeric input
	    else if(this.questions[x]['type'] == 'Numeric') {
	      $("input[name='q"+x+"']").val('0');
	      $("#s"+x).slider("option","value",'0');
	    }
	  }
  } // End method: clear()
  
  
  
  /*** 
    Method: validate()
    Purpose: Checks the game to make sure all questions are answered 
  ***/
  this.validate = function() {
  
    // Initialization
    isValid=true;
 
    // loop through each user answer
    for (x in this.questions) {
	    
	    // Question already answered
	    if(this.questions[x]['status'] == 'Answered' || this.questions[x]['status'] == 'Scored') {
	      continue;
	    }
	    
	    // Option input
	    else if(this.questions[x]['type'] == 'Option') {
	      if($("select[name='q"+x+"']").exists()) {
	        if($("select[name='q"+x+"']").val() == '') {
	          isValid=false;
	        }
	      }
	      else {
	        if(!$("input[name='q"+x+"']:checked").exists()) {
	          isValid=false;
	        }
	      }
	    }
	  }
	
	  if(isValid == false) {
		  // Analytics
      try {pageTracker._trackPageview('/game/incomplete');}catch(err){}
		  alert("You haven't filled in all the questions yet!");
	  }
	  else {
		  // Analytics
      try {pageTracker._trackPageview('/game/submit');}catch(err){}
	  } 
    return isValid;
  } // End method: validate()
  
}


// getEntry:  Gets the entry for a username
function getEntry(username, gameID) {
  
  // Analytics
  try {pageTracker._trackPageview('/game/'+gameID+'/entry/'+username);}catch(err){}
  
  // Show popover
  $.blockUI({ message: $("#box") });
       
  // Get entry HTML
  $.ajax({
      url: "/game/"+gameID+"/entry/"+username,
      cache: false,
      success: function(html){
        // Replace html
        $("#entryHTML").html(html);
           
        // Fade out processing
        $("#boxProcessing").fadeOut(250, function() {$("#boxContent").fadeIn(250);});     
      }
  });

	return false;

}
