$(document).ready(function(){

  if ($('.answer_selected').length) {

    // hide content to be shown when an answer is selected,
    //  unless that answer is selected
    $('.answer_selected').each(function(){

      if (!$(this).parent().find('input:checked').length) {
        $(this).addClass('hidden');
      }

    });

    // show additional content if a checkbox is checked
    $('form#quiz_main_form input:checkbox').each(function(){

      if ($(this).parent().find('.answer_selected').length) {

        $(this).change(function(){
          if ($(this).is(':checked')) {
            $(this).parent().find('.answer_selected').removeClass('hidden');
          } else {
            $(this).parent().find('.answer_selected').addClass('hidden');
          }
        });

      }

    });

    // show additional content if a radio button is checked
    //  and make sure content for other radio buttons is hidden
    $('form#quiz_main_form ol li[id^=q_]').each(function(){

      if ($(this).find('input:radio').length &&
          $(this).find('.answer_selected').length) {

        $(this).find('input:radio').change(function(){

          $(this).parents('ul').find('.answer_selected').addClass('hidden');
          $(this).parent().find('.answer_selected').removeClass('hidden');

        });

      }

    });

  }

  $('#quiz_navigation form').submit(function(){ return false; });

  $('a.fave_link').click(function() {
    var target = this;
    $.ajax({
      url: $(this).attr('href'),
      data: 'ajax=1',
      dataType: 'json',
      type: 'post',
      success: function(response_data) {
        var other;
        if (response_data['add']) {
          other = $(target).siblings(".remove_fave")[0];
        } else if (response_data['remove']) {
          other = $(target).siblings(".add_fave")[0];
        }
        $(target).addClass('hidden');
        $(other).removeClass('hidden');
      }
    });
    return false;
  });

  $('form.rating').rating();

  $('div.share form').submit(function(){ return false; });
  $('div.share input[type=text]').each(function(){

    $(this).keydown(function(){ return false; });
    $(this).keypress(function(){ return false; });
    $(this).keyup(function(){ return false; });
    $(this).focus(function(){
      this.select();
    });

  });

  $('#tag_modify_form').hide();
  $('#add_tags_ajax').show();
  $('a.add_tag').click(function(){
    $(this).hide();
    $('#tag_modify_form').show();
    $("#tag_modify_tags").focus();
    return false;
  });
  $('#tag_modify_form').submit(function(){

    var tags = $("#tag_modify_tags").val();

    tags = tags.replace(/^[ \t,]+/, '').replace(/[ \t,]+$/, '');

    $.ajax({
      url: document.location.toString(),
      data: {
        tag_modify_ajax: 1,
        tags: tags
      },
      dataType: 'json',
      type: 'post',
      success: function(response) {
        add_tag_items(response);
      },
      error: function(request, error, exception) {
        $("#tag_modify_tags").val('');
        $("ul#tag_list").html('');
        $("div#tag_modify_error").text(request.responseText);
        $.ajax({
          url: document.location.toString(),
          data: { retrieve_tags_ajax: 1 },
          dataType: 'json',
          type: 'post',
          success: function(response) {
            add_tag_items(response);
          }
        });
      },
      complete: function(request, type) {
        $("img#ajax-loader").hide();
      }
    });

    return false;
  });

});

function add_tag_items(response) {
  $("#tag_modify_tags").val('');
  $("ul#tag_list").html('');
  for (var i = 0; i < response.length; i++) {
    var tag = response[i];
    var filteredText = tag.text.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace('"', '&quot;');
    if (tag.count > 0) {
      $("ul#tag_list").append('<li><a style="font-size: ' + tag.font_size + '%" href="tag/' + encodeURIComponent(tag.text) + '">' + filteredText + '</a></li>\n');
    } else {
      $("ul#tag_list").append('<li><span class="tag0">' + filteredText + '</span></li>\n');
    }
  }
}
