function show_add_comment_form (announce) {
	id = announce.attr("id");
	announce.find("ul.comments").after('' +
		'<form class="add-comment" method="post">' +
			'<input type="hidden" value=' + id + ' name="slug"/>' +
			'<textarea class="wide short" name="body" />' +
			'<input type="button" value="Gönder" class="add-comment" />' +
			'<input type="button" value="İptal" class="cancel-add-comment"/>' +
		'</form>'
	);
}

function show_update_comment_form (comment) {
	var id = comment.attr("id");
	$.ajax({
		type : "get",
		url : "/announces/get_comment/" + id + "/",
		data : {'only_context' : true},
		dataType : "html",
			beforeSend:function() {
				comment.parent().parent().addClass("loading");
			},
			complete:function() {
				comment.parent().parent().removeClass("loading");
			},
			success:function(data) {
				comment.html('' +
				'<form class="add-comment" method="post">' +
					'<input type="hidden" value=' + id + ' name="id"/>' +
					'<textarea class="wide short" name="body">' + data +
					'</textarea>' +
					'<input type="button" value="Gönder" class="update-comment" />' +
					'<input type="button" value="İptal" class="cancel-update-comment"/>' +
				'</form>'
				)
			},
			error : function (data) {
				alert(data);
			}
		});
}

$().ready(function(){
	$('a.delete-announce').livequery('click', function(){
		var confirmed = confirm("Silelim mi?");
		var announce = $(this).parent().parent().parent();
		var slug = announce.attr("id");
		if (confirmed) {
			$.ajax({
				method : "GET",
				url: "/announces/delete/"+ slug +"/",
				success: function (AjaxMsg) { $(announce).fadeOut(500); }
			});
		};
	});

	$("a.add-comment").livequery('click', function() {
		var announce = $(this).parent().parent().parent();
		if (announce.find("form.add-comment").html() == null) {
			show_add_comment_form(announce);
		} else {
			announce.find("form.add-comment").val("");
		}
	});

	$("a.delete-comment").livequery('click', function() {
		var comment = $(this).parent();
		var confirmed = confirm("Silelim mi?");
		if (confirmed) {
			$.ajax({
				method : "GET",
				url: "/announces/delete_comment/"+ comment.attr("id") +"/",
				success: function (AjaxMsg) { $(comment).fadeOut(500); }
			});
		}
	});

	$("a.show-full").livequery('click', function() {
        container = $(this).parent().parent();
		container.load("/announces/get/"+container.attr("id")+"/");
	});

	$("a.update-comment").livequery('click', function() {
		var comment = $(this).parent();
		show_update_comment_form(comment);
	});

	$("input.cancel-update-comment").livequery('click', function() {
		var comment = $(this).parent().parent();
		comment.load("/announces/get_comment/"+comment.attr("id")+"/")
	})

	$("input.update-comment").livequery('click', function() {
		announce = $(this).parent().parent().parent().parent();
		form = announce.find("form.add-comment");

		$.ajax({
			type : "post",
			url : "/announces/update_comment/",
			data : form.serialize(),
			dataType : "json",
			beforeSend:function() {
				announce.addClass("loading");
				form.find("input").attr("disabled", "disabled");
			},
			complete : function() {
				announce.removeClass("loading");
				form.remove();
			},
			success :function(data) {
				if (data["success"] == true) {
					announce.find("ul.comments").load("/announces/get_comments/"+announce.attr("id")+"/");
				} else {
					alert(data["errmsg"])
				}
			},
			error : function (data) {
				alert(data);
			}
		});
	})

	$("input.cancel-add-comment").livequery('click', function() {
		var announce = $(this).parent().parent();
		announce.find("form.add-comment").remove();
	})

	$("input.add-comment").livequery('click', function() {
		var announce = $(this).parent().parent();
		$.ajax({
			type : "post",
			url : "/announces/add_comment/",
			data : announce.find("form.add-comment").serialize(),
			dataType : "json",
			beforeSend:function() {
				announce.find("div.comments").addClass("loading");
				announce.find("form.add-comment > input").attr("disabled", "disabled");
			},
			complete : function() {
				announce.find("form.add-comment").remove();
				announce.find("div.comments").removeClass("loading");
			},
			success :function(data) {
				if (data["success"] == true) {
					announce.find("ul.comments").load("/announces/get_comments/"+announce.attr("id")+"/");
				} else {
					alert(data["errmsg"])
				}
			},
			error : function (data) {
				alert(data);
			}
		});
	})

	$("a.expand-comments").livequery('click', function() {
		var announce = $(this).parent().parent().parent();
		announce.find("ul.comments").load("/announces/get_comments/"+announce.attr("id")+"/?expanded=True");
	})
});


