
$(document).ready(function() {
  button();
  rollover();
  question();
  UIPaymentInfos();
});



function clearFeedback()
{
  $("#feedbackWrapper").empty();
}

function addFeedback(msg)
{
  if ($("#feedback").length == 0) {
    $("#feedbackWrapper").prepend('<ul id="feedback"></ul>');
  }
  $('#feedback').prepend("<li>" + msg + "</li>");
  $('#feedback li:first').hide();
  $('#feedback li:first').fadeIn("200");
}

/*******************************************************************************
 *  INPUT BUTTON
 ******************************************************************************/
function button()
{
  $("input.button").hover(function () {
    $(this).addClass("buttonover");
   },
   function () {
    $(this).removeClass("buttonover");
   });
}

/*******************************************************************************
 *  QUESTION
 ******************************************************************************/
function question()
{
  $('#answerForm').submit(function(){
    return false;
  });
  $('#answerForm #continue').click(nextQuestion);
  $('#answerForm #back').click(previousQuestion);
}

function fadeInContent(callback)
{
  $(".left-column").fadeIn('slow');
  $(".TQright").fadeIn('slow', callback);
}

function fadeOutContent(callback)
{
  $(".left-column").fadeOut('slow');
  $(".TQright").fadeOut('slow', callback);
}

function updateQuestion(response)
{
  $(".questionNumber").html(response.data.order);
  $(".questionP").html(response.data.text);
  $("#answerForm").html(response.data.form);
  question();
}

function nextQuestion()
{
  clearFeedback();
  if (saveAnswer() == false) return false;

  var test_id = $('#test_id').val();
  var question_number = parseInt($('#question_number').val());
  question_number = question_number + 1;
  fadeOutContent(function(){
    $.ajax({
      url: "/api/test/question",
      global: false,
      type: "GET",
      data: ({'test_id' : test_id,
              'question_number' : question_number}),
      dataType: "json",
      success: function(msg){
        /* last question */
        if (msg.code == "finish") {
          window.location = "/test/results/" + test_id;
          return;
        }
        updateQuestion(msg);
        fadeInContent();
        }
      });
    });
  return false;
}

function previousQuestion()
{
  clearFeedback();
  var test_id = $('#test_id').val();
  var question_number = parseInt($('#question_number').val());
  question_number = question_number - 1;
  fadeOutContent(function(){
    $.ajax({
      url: "/api/test/question",
      global: false,
      type: "GET",
      data: ({'test_id' : test_id,
              'question_number' : question_number}),
      dataType: "json",
      success: function(msg){
        updateQuestion(msg);
        fadeInContent();
        }
      });
    });
  return false;
}

function saveAnswer()
{
  var answer = $('#answerForm input:checked').val();
  var test_id = $('#test_id').val();
  var question_id = parseInt($('#question_id').val());
  if (answer == undefined)
  {
    addFeedback("All questions require an answer");
    return false;
  }

  $.ajax({
    url: "/api/test/answer",
    global: false,
    type: "GET",
    data: ({'test_id' : test_id,
            'question_id' : question_id,
            'choice' : answer}),
    dataType: "json",
    success: function(msg){
      return true;
    }
  });

}

/*******************************************************************************
 *  ROLLOVER   
 ******************************************************************************/

function over()
{
  var src = $(this).attr('src');
  var chunk = src.split(".");
  chunk = chunk.reverse();
  var over_src = src + ".over." + chunk[0];
  $(this).attr('src', over_src);
}

function out()
{
  var over_src = $(this).attr('src');
  var chunk = over_src.split(".");
  chunk = chunk.reverse();
  var ext = chunk[0];
  var end = over_src.length - ext.length - 5 - 1; /* .over. */
  var src = over_src.substring(0, end);
  $(this).attr('src', src);
}

function rollover()
{
  $(".rollover").each(function()
  {
    $(this).hover(over, out);
  });
}

/*******************************************************************************
 *  PAYMENT   
 ******************************************************************************/

function UIPaymentInfos()
{
  $("#countrycode").change(function(){
   var countryCode = $(this).val();

   var provinceField = $("#state");
   var postalcodeField = $("#zip");
   switch(countryCode)
   {
     case "US":
       provinceField.prev().text("State: ");
       postalcodeField.prev().text("Zip Code: ");
       break;
     case "CA":
       provinceField.prev().text("Province: ");
       postalcodeField.prev().text("Postal Code: ");
       break;
     default:
       provinceField.prev().text("State / Province: ");
       postalcodeField.prev().text("Postal Code: ");
       break;
  }
   $.ajax({
     url: "/api/state/listing",
     global: false,
     type: "GET",
     data: ({'countryCode' : countryCode}),
     dataType: "json",
     success: function(response){
       var options = "";
       $.each(response.data, function(i,d){
        options += '<option value="' + i + '">' + d + '<\/option>';
       });
       provinceField.html(options);
       }
     });
  });

  $("#ordcountry").change(function(){
   var countryCode = $(this).val();

   var provinceField = $("#ordprovince");
   var postalcodeField = $("#ordpostalcode");
   switch(countryCode)
   {
     case "US":
       provinceField.prev().text("State: ");
       postalcodeField.prev().text("Zip Code: ");
       break;
     case "CA":
       provinceField.prev().text("Province: ");
       postalcodeField.prev().text("Postal Code: ");
       break;
     default:
       provinceField.prev().text("State / Province: ");
       postalcodeField.prev().text("Postal Code: ");
       break;
   }

   $.ajax({
     url: "/api/state/listing",
     global: false,
     type: "GET",
     data: ({'countryCode' : countryCode}),
     dataType: "json",
     success: function(response){
       var options = "";
       $.each(response.data, function(i,d){
        options += '<option value="' + i + '">' + d + '<\/option>';
       });
       provinceField.html(options);
       }
     });

  });
}
