$(document).ready(init);

function init() {
    saved_posts = {};
    comment_form = $("#comment-form");
    $("#comment-preview", comment_form).hide();
    input_reply = $("#id_reply_to", comment_form);
    comment_link = $("input.comment-link");
    // actions
    
    var id = input_reply.val();
    
    if ($(".errorlist", comment_form).length == 0 && document.location.href.indexOf("#add-comment") == -1 && expand_comment_form != 1 && !id) {
        comment_form.hide();
        var show_comment_link = true;
    }
    else {
        var show_comment_link = id;
        move_form(id);
        $("#reply-link-" + id).hide();
        $('html').animate({scrollTop: comment_form.offset().top}, 1);
    }
    if (show_comment_link) {
        comment_link.show();
    } else {
    	comment_link.hide();
    }
    $("a.edit-comment").css("display", "inline");
    $("a.cancel-edit").hide();
    $("input.edit-submit").hide();

    // spinner
    $('#ajaxload').ajaxStart(function(){$(this).show()}).ajaxStop(function(){$(this).hide()});
};

function get_json(data) {
    return eval('(' + data + ')');
};

function move_form(id) {
    if (id) {
        $("#c" + id).append(comment_form);
    }
    else {
        $("#add-comment-containter").append(comment_form);
    }
}

function replyto(id) {
    cancel_comment();
    input_reply.val(id);
    move_form(id);
    comment_form.show();
    $("#reply-link-" + id).hide();
    return false;
};

function comment() {
    cancel_comment();
    input_reply.val('');
    move_form(null);
    comment_form.show();
    comment_link.hide();
    $('html').animate({scrollTop: comment_form.offset().top}, 0);
    return false;
};

function cancel_comment() {
    id = input_reply.val();
    if (id) {
        $("#reply-link-" + id).show();
    }
    else {
        comment_link.show();
    }
    comment_form.hide();
    return false;
};

function edit_comment(id, url) {
    comment_div = $("#c" + id + " .text");
    saved_posts[id] = comment_div.html()
    $.post(url,
           {'get_body': ''},
           function (data) {
               json = get_json(data);
               comment_div.html('<textarea class="comment-edit-area">' + json.body + '</textarea>');
               $("#edit-comment-" + id).hide();
               $("#cancel-edit-" + id).css("display", "inline");
               $("#edit-submit-" + id).css("display", "inline");
           });
    return false;
};

function cancel_edit(id) {
    comment_div = $("#c" + id + " .text");
    comment_div.html(saved_posts[id]);
    $("#cancel-edit-" + id).hide();
    $("#edit-submit-" + id).hide();
    $("#edit-comment-" + id).css("display", "inline");
    return false;
};

function submit_edit(id, url) {
    comment_div = $("#c" + id + " .text");
    text = $("#c" + id + " .text textarea.comment-edit-area").val();
    $.post(url,
           {'body': text},
           function (data) {
               json = get_json(data);
               comment_div.html(json.body_html);
               $("#cancel-edit-" + id).hide();
               $("#edit-submit-" + id).hide();
               $("#edit-comment-" + id).css("display", "inline");
           });
    return false;
};

function post_comment(url) {
	if ($("#id_body").val() != "") {
	
		$(":input").attr("disabled", "disabled");
		
		$.post(url, {body: $("#id_body").val(),
			  		 reply_to: $("#id_reply_to").val()},
				function(data) {
					id = input_reply.val();
				    if (id) {
				    	$("#c" + id).after(data);
				    } else {
				    	$("#id_comments_containter").append(data);
				    }
				    add_highlight_to_user_comments(highlight_user_id);
				    
				    $(":input").removeAttr("disabled");
					
					cancel_comment();
					$("#id_body").val("");
					$("#id_reply_to").val("");
				}, "text");
	}		
};

function delete_comment(id, url) {
    if (confirm("Are you sure to delete this comment?"))
        {
            $.post(url,
                   {'delete': true},
                   function (data) {
                       json = get_json(data);
                       $('#c' + json.id).remove();
                   });
        };
    return false;
};

function preview_comment(url) {
    text = $("#id_body", comment_form).val();
    preview_div = $("#comment-preview", comment_form);
    preview_div.text("Loading...");
    preview_div.show();
    $.post(url,
          {'body': text},
          function (data) {
              json = get_json(data);
              preview_div.html(json.body_preview);
          });
    return false;
};

function highlight_comments(comment_id, parent_comment_id) {
	var highlight = true;
	if ($(".comment-arrow-" + comment_id).hasClass("highlight-comments-arrow"))
		highlight = false;
	
	$(".highlight-comments-arrow").removeClass("highlight-comments-arrow");
	$(".highlight-comment").removeClass("highlight-comment");
	
	if (highlight) {
		$(".comment-arrow-" + comment_id).addClass("highlight-comments-arrow");
		$("#c" + comment_id).addClass("highlight-comment");
		$("#c" + parent_comment_id).addClass("highlight-comment");
		
		$('html').animate({scrollTop: $("#c" + parent_comment_id).offset().top}, 200);
	}
	
	return false;
};

var highlight_user_id = false;

function highlight_user_comments(user_id) {
	highlight_user_id = user_id;
	if ($(".user-" + user_id).hasClass("highlight-user-dot"))
		highlight_user_id = false;
		
	$(".select-user-dot,.highlight-link").removeClass("highlight-user-dot");
	$(".comment-of-user").removeClass("comment-of-user");
	$(".comment-to-user").removeClass("comment-to-user");
	
	add_highlight_to_user_comments(highlight_user_id);
	
	return false;
};

function add_highlight_to_user_comments(highlight_user_id) {
	if (highlight_user_id != false) {
		$(".user-" + highlight_user_id).addClass("highlight-user-dot");
		$(".comment-of-user-" + highlight_user_id).addClass("comment-of-user");
		$(".comment-to-user-" + highlight_user_id).addClass("comment-to-user");
	}
}