$(document).ready(function(){

  $('a.delete_quiz').click(function(){
    if (confirm('Are you sure?')) {
      if ($(this).attr('href').match('favorite')) {
        var obfu_id = $(this).attr('href').match('favorite/quiz/([^/]+)/')[1];
        profile_remove_favorite('quiz', obfu_id);
      } else if ($(this).attr('href').match('delete_quiz')) {
        var match = $(this).attr('href').match(
          'user/([^\?]+)\\?delete_quiz=([^\&]+)'
        );
        var username = match[1];
        var obfu_id  = decodeURIComponent(match[2]);
        profile_remove_quiz(username, obfu_id);
      }
    }
    return false;
  });
  enable_ajax_comment_delete();

});

function profile_remove_favorite(type, id) {
  $.ajax({
    url: "favorite/" + type + "/" + id + "/remove",
    data: "ajax=1",
    dataType: 'json',
    type: 'post',
    success: function(response_data) {
      if (response_data['remove'] == 1) {
        $("li#fave_" + id).fadeOut("fast");
      }
    }
  });
  return false;
}

function profile_remove_quiz(username, obfu_id) {
  $.ajax({
    url: "user/" + username + "?delete_quiz=" + obfu_id,
    data: "ajax=1",
    dataType: "json",
    type: "post",
    success: function(response_data) {
      if (response_data['quiz_deleted'] == obfu_id) {
        $("li#created_" + obfu_id).fadeOut("fast");
      }
    }
  });
  return false;
}

function profile_remove_comment(username,comment_id) {
  $.ajax({
    url: "user/" + username + "?delete_comment=" + comment_id,
    data: "ajax=1",
    dataType: "json",
    type: "post",
    success: function(response_data) {
      if (response_data['comment_deleted'] == comment_id) {
        //Leave space, in case it has children.
        var comment = $("li#comment" + comment_id);
        comment.fadeTo("fast", 0.10);
        var selected = $("div#comments div.paginator li.selected");
        var page_selected = selected.html();
        if(!page_selected || page_selected.length<1) {
          page_selected = '1';
        }
        var url = $('div#comments div.paginator a[href]').attr("href");
        if (!url || !(url.length)) {
          url = "user/" + username + "?comments_page=1";
        } else {
          url = url.split("?")[0] + "?comments_page=" + page_selected;
        }
         load_comment_page(url);
         update_comment_count(response_data['comment_count']);

        // Catch rare cases when one or two comments are deleted at the
        // same time, and are the last comments on the page.
        var html = $("li.disabled:first").html();
        if(html && html.match(/next/i) && 
($("li.comment:not(.deleted_parent)")).length<=1){
          page_selected = parseInt(page_selected) - 1;
          url = url.split("?")[0] + "?comments_page=" + page_selected;
          load_comment_page(url);
        }

      }
    }
  });
  return false;
}

function load_comment_page(url) {
  $('#comments .body').load(url, {}, function(){
    document.location.hash = 'comments';
    enable_ajax_comment_delete();
    enable_comment_reply();
    enable_ajax_comment_nav();
    enable_ajax_comment_adult();
    enable_ajax_flagging();
  });
}

function update_comment_count(count) {
  var comments = $("div#comments div.title h2.icon").text();
  //comments.match(/(\d+) Comment/i);
  //var new_comments = (parseInt(RegExp.$1)-1) + " Comment";
  if (count == null) {
    count = "0";
  }
  var new_comments = count + " Comment";
  if (parseInt(count) != 1) {
    new_comments += "s";
  }
  $("div#comments div.title h2.icon").text(new_comments);
}

function enable_ajax_comment_delete() {
  $('div#comments a.delete_comment').click(function(){
    var comment_poster = $('a.comment_username').attr('href').match('user/([^\?]+)')[1];
    if (confirm('Are you sure you want to delete this comment by ' + comment_poster + '? If you delete it, it will be permanently removed from your profile.')) {
      var match = $(this).attr('href').match(
        'user/([^\?]+)\\?delete_comment=([^\&]+)'
      );
      var username = match[1];
      var comment_id = decodeURIComponent(match[2]);
      profile_remove_comment(username, comment_id);
    }
    return false;
  });

}

