﻿$(function() {
	var addCommentEl = $(".comments .add-comment");
	var addCommentTextEl = addCommentEl.find(".comment-text");
	var addCommentButtonEl = addCommentEl.find(".button");

	bindAddCommentElEvents(addCommentEl);

	var replyCommentEl = $(".comments .reply-comment");
	var replyCommentTextEl = replyCommentEl.find(".comment-text");
	bindAddCommentElEvents(replyCommentEl);

	addCommentButtonEl.click(function() {
		if (addCommentTextEl.val() != "" && addCommentTextEl.val() != addCommentTextEl.attr("title")) {
			$.ajax({
				type: "POST",
				url: commentsSettings.addCommentUrl,
				dataType: "html",
				data: { "targetContentUUID": commentsSettings.targetContentUUID, "text": addCommentTextEl.val(), "pageSize": commentsSettings.pageSize, "reverseSort": commentsSettings.reverseSort },
				beforeSend: function() {
					addCommentEl.children().attr("disabled", "disabled");
				},
				success: function(data) {
					hideReply();

					$(".comments .comments-list").html(data);

					addCommentTextEl.val("");
					addCommentTextEl.blur();
					addCommentEl.children().removeAttr("disabled");
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					alert(textStatus);
				}
			});
		}
	});
});

function bindAddCommentElEvents(addCommentEl) {
	var addCommentTextEl = addCommentEl.find(".comment-text");
	var addCommentButtonEl = addCommentEl.find(".button");

	addCommentTextEl.val(addCommentTextEl.attr("title"));

	addCommentTextEl.focus(function() {
		if ($(this).val() === $(this).attr("title")) {
			$(this).val("");
			$(this).animate({ height: "70px" }, 500);
			addCommentButtonEl.fadeIn(500);
			$(this).toggleClass("active");
		}
	}).blur(function() {
		if ($(this).val() === "" && $(this).hasClass("active")) {
			$(this).val($(this).attr("title"));
			$(this).animate({ height: "30px" }, 500);
			addCommentButtonEl.fadeOut(500);
			$(this).toggleClass("active");
		}
	});
}

function showComments(offset) {
	$.ajax({
		type: "POST",
		url: commentsSettings.commentsListUrl,
		dataType: "html",
		data: { "targetContentUUID": commentsSettings.targetContentUUID, "pageIndex": offset, "pageSize": commentsSettings.pageSize, "reverseSort": commentsSettings.reverseSort },
		success: function(data) {
			$(".comments .comments-list").html(data);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert(textStatus);
		}
	});
}

function replyComment(commentUUID, commentEl, offset) {
	var replyCommentEl = $(".comments .reply-comment");
	$(commentEl).append(replyCommentEl);

	var replyCommentTextEl = replyCommentEl.find(".comment-text");
	var replyCommentButtonEl = replyCommentEl.find(".button");

	replyCommentButtonEl.unbind("click").click(function() {
		if (replyCommentTextEl.val() != "") {
			$.ajax({
				type: "POST",
				url: commentsSettings.replyCommentUrl,
				dataType: "html",
				data: { "targetContentUUID": commentsSettings.targetContentUUID, "commentUUID": commentUUID, "text": replyCommentTextEl.val(), "pageSize": commentsSettings.pageSize, "pageIndex": offset, "reverseSort": commentsSettings.reverseSort },
				beforeSend: function() {
					replyCommentEl.children().attr("disabled", "disabled");
				},
				success: function(data) {
					replyCommentTextEl.val("");
					hideReply();

					$(".comments .comments-list").html(data);
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					alert(textStatus);
				}
			});
		}
	});

	replyCommentEl.children().removeAttr("disabled");
	replyCommentEl.show();
}

function editComment(commentTargetContentUUID, commentUUID, commentEl, offset) {
	commentEl.find(".actions").hide();
	var textEl = $(commentEl).find(".text");
	$("#button-more-" + commentUUID).remove();

	var previewText = $("#preview-" + commentUUID).html();
	var restText = $("#rest-" + commentUUID).html();

	var text = null;

	if (previewText != null && restText != null) {
		text = (previewText + restText);
	} else {
		text = textEl.html();
	}
	text = text.replace(commentsSettings.wbr, "").replace(commentsSettings.brRegex, '\n').replace(commentsSettings.brRegexIE, '\n');

	var editEl = $("<div class='text-edit'><textarea class='edit' cols='20' rows='7'></textarea><input type='button' class='button' value='Done' /></div>")
	var doneEl = editEl.find(".button");
	var textEditEl = editEl.find(".edit");

	textEditEl.val(text);

	doneEl.click(function() {
		if (jQuery.trim(textEditEl.val()) != "") {
			$.ajax({
				type: "POST",
				url: commentsSettings.editCommentUrl,
				dataType: "json",
				data: { "commentTargetContentUUID": commentTargetContentUUID, "commentUUID": commentUUID, "text": jQuery.trim(textEditEl.val()), "pageSize": commentsSettings.pageSize, "pageIndex": offset },
				success: function(data) {
					editEl.remove();
					textEl.html(data);
					textEl.show();
					commentEl.find(".actions").show();
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					alert(textStatus);
				}
			});
		} else {
			showNotification("Comment cannot be empty!");
		}
	});

	textEditEl.width(textEl.width());
	textEl.hide();
	textEl.before(editEl);
}

function flagComment(commentTargetContentUUID, commentUUID, flagEl) {
	$.ajax({
		type: "POST",
		url: commentsSettings.flagCommentUrl,
		dataType: "html",
		data: { "commentTargetContentUUID": commentTargetContentUUID, "commentUUID": commentUUID },
		success: function(data) {
			flagEl.html("Flagged");
			flagEl.addClass("active");
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert(textStatus);
		}
	});
}

function deleteComment(commentTargetContentUUID, commentUUID, offset) {
	$.ajax({
		type: "POST",
		url: commentsSettings.deleteCommentUrl,
		dataType: "html",
		data: { "targetContentUUID": commentsSettings.targetContentUUID, "commentTargetContentUUID": commentTargetContentUUID, "commentUUID": commentUUID, "pageSize": commentsSettings.pageSize, "pageIndex": offset, "reverseSort": commentsSettings.reverseSort },
		success: function(data) {
			$(".comments .comments-list").html(data);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert(textStatus);
		}
	});
}

function hideReply() {
	var replyCommentEl = $(".comments .comments-list .reply-comment");

	if (replyCommentEl.length > 0) {
		var replyCommentTextEl = replyCommentEl.find(".comment-text");

		replyCommentTextEl.blur();

		replyCommentEl.hide();
		replyCommentEl.children().removeAttr("disabled");

		$(".comments .body").append(replyCommentEl);
	}
}
