
// source: http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

// create cookie to check if cookies are enabled
createCookie('cookies_enabled', 'yes', 30);

var all_the_points = $("#points\-remaining\-count");

$(document).ready(function(){
  
  var all_the_points = $("#points\-remaining\-count");
  
  //  Fix total points if input values are cached
  var all_the_points_temp = parseInt(all_the_points.text());
  $(".issue\-point").each(function() {
    if ($(this).val() > 0) {
      $(this).parents("li").addClass('notnull');
      all_the_points_temp = all_the_points_temp - $(this).val();
    }
  });
  if (all_the_points_temp == 0) { all_the_points_temp = "0"; }
  all_the_points.html(all_the_points_temp);
  
  
  // disable submit button
  checkSubmitAvailibility(parseInt(all_the_points.text()));
  
  
  // "+" button clicked, add one point
  $("a").filter(".issue\-add\-points").bind("click", this, function() {
    var current_node = $(this).parents("li");
    addPoints(current_node, 1, current_node.find(".issue\-point").val());
    return false;
  });
  
  
  // "-" button clicked, subract one point
  $("a").filter(".issue\-subtract\-points").click(function() {
    var current_node = $(this).parents("li");
    addPoints(current_node, -1, current_node.find(".issue\-point").val());
    return false;
  });
  
  
  // Manipulate points if entered in input field
  var original_value;
  $(".issue\-point").focus(function() {
    original_value = $(this).val();
  });

  $(".issue\-point").blur(function() {
    var current_input = $(this);
    var current_node = $(this).parents("li");
    // make sure it's only digits
    if (current_input.val().match(/^\d+$/) && original_value != current_input.val()) {
      if ((!addPoints(current_node, ((original_value - current_input.val()) * -1), original_value))) {
        current_input.val(original_value);
      }       
    } else {
     current_input.val(original_value);
    }
  });

  $("#quiz-selector").click(function() {
    $("#quiz-selection ul").toggle();
    return false;
  });


});


function checkSubmitAvailibility(point_num)
{
  // enable if 0 points left
  if (point_num == 0) {
    $("#screen1\-submit").attr("disabled","");
    $("#screen1\-submit").removeClass("disabled");
    $("#submit\-button\-help").hide();
    
  // disable button
  } else {
    $("#screen1\-submit").attr("disabled","disabled");
    $("#screen1\-submit").addClass("disabled");
    
  }
}


function addPoints(node, value, original_value) 
{
  var all_the_points = $("#points\-remaining\-count");
  var total_points = parseInt(original_value) + value;
  var all_total_points = parseInt(all_the_points.text()) - value;
    
  if ((all_total_points >= 0 && all_total_points <= 20) && (total_points >= 0)) {
    if (all_total_points == 0) {
      all_total_points = "0"; // change to string
    }
    if (total_points == 0) {
      total_points = "0";
      node.removeClass('notnull');
    } else {
      node.addClass('notnull');
    }
    all_the_points.html(all_total_points);
    node.find(".issue\-point").val(total_points);
    checkSubmitAvailibility(all_total_points);
    return true;
  } else {
    return false;
  }
  
}


