$(function() {
	$("form.rate_wine").ajaxForm({
		loading: function() {
			this.addClass("processing");
		},
		success: function(response) {
			var rating = response.rating,
				currentRating = this
					.removeClass("processing")
					.find(".current-rating");

			$.each([ 0, 1, 2, 3, 4, 5 ], function(i) {
				currentRating[(i == rating ? "add" : "remove") + "Class"]("r" + i);
			});
		},
		error: function() {}
	})
		.find("a").click(function(e) {
			e.preventDefault();
			var $this = $(this),
				value = $this.text();
			
			if (!parseInt(value, 10)) {
				value = 0;
			}
			
			$this.closest("form")
				.find("input[name=rating]")
					.val(value)
				.end()
				.submit();
		});
	
	$("#tasting-notes")
		.find("form")
			.ajaxForm({
				loading: function() {
					this.addClass("processing");
				},
				success: function(response) {
					this
						.removeClass("processing")
						.closest(".notes")
							[(response.notes.length == 0 ? "add" : "remove") + "Class"]("new edit")
							.find("blockquote p:first-child")
								.text(response.notes);
				},
				error: function() {}
			})
		.end()

		.find("a.note-edit, a.note-cancel").click(function(e) {
			e.preventDefault();
			($this = $(this))
				.closest(".notes")[($this.is(".note-edit") ? "add" : "remove") + "Class"]("edit");
		})
		.end()

		.find("a.note-delete").click(function(e) {
			e.preventDefault();
			$(this).closest(".notes")
				.find("input[name=notes]")
					.val("")
					.closest("form")
					.submit();
		});
});