function enableComments() {
	jQuery("div.comment-add a").unbind('click');
	jQuery("div.comment-add a").click(APP.addComment);
}

function getComments() {
	jQuery.getJSON(
			web_path + 'ajax/comment/paginateComment', {
				pageNoComments: pageNumber,
				typeComment: commentType,
				idElement: elementId
			},
			function(response) {
				setComments(response);
			}
	);
}

function postComment() {
	var text = jQuery('textarea.comment').val();
	var button = jQuery('div.comment-reply a');
	if (text.length > 0 && text.length < 500) {
		button.unbind('click').attr('onclick', '').html('sending...');
		jQuery.post(
				web_path + 'ajax/comment/postComment', {
					idComment: commentEditId,
					idReplyComment: commentReplyId,
					typeComment: commentType,
					idElement: elementId,
					content: text
				},
				function(response) {
					if (response.err) {
						button.bind('click', APP.postComment).html('Send');
						jQuery('div.note-comment').html(response.msg);
					} else {
						closeAllMessageBoxes();
						if (response['data'] != '') {
							commentAction(response['data']);
						}
					}
				}, 'json'
		);
	} else {
		jQuery('div.note-comment').html('You must enter a comment!');
	}
}

function deleteComment(commentId, replyId) {
	if (confirm('Are you sure you want to Delete this comment?')) {
		jQuery.post(
				web_path + 'ajax/comment/deleteComment', {
					idComment: commentId,
					idReplyComment: replyId,
					typeComment: commentType,
					idElement: elementId
				},
				function() {
					jQuery('#comment_container_' + commentId).hide();
					jQuery('#comment_children_' + commentId).hide();
					jQuery('#comment_area_' + commentId).hide();
					totalComments--;
					updateCounter();
				}
		);
	}
}

function reportSpam(commentId) {
	if (confirm('Are you sure you want to Report this comment?')) {
		jQuery('#spam_' + commentId).attr('onclick', '').text('Sending...');
		jQuery.post(
				web_path + 'ajax/comment/reportSpam', {
					idComment: commentId,
					typeComment: commentType,
					idElement: elementId
				},
				function() {
					jQuery('#spam_' + commentId).text('Reported').css('color', 'gray');
				}
		);
	}
}

function loadMoreComments() {
	jQuery('div.more-comment a').attr('onclick', '').html('Loading...');
	pageNumber++;
	getComments();
}

function addComment() {
	commentEditId = 0;
	commentReplyId = 0;
	commentAction = onAddComment;

	createComment('#comment_area_0', '', 'Post');

	setCaretPosition(0);
}

function editComment(commentId, replyId) {
	commentEditId = commentId;
	commentReplyId = replyId;
	commentAction = onEditComment;

	createComment('#comment_content_' + commentId, jQuery('#comment_content_' + commentId).html().replace(/(<([^>]+)>)/ig,""), 'Update');
}

function replyComment(replyId, replyName) {
	commentEditId = 0;
	commentReplyId = replyId;
	commentAction = onReplyComment;

	replyName = replyName != '' ? '@' + replyName + ': ' : '';

	createComment('#comment_area_' + replyId, replyName, 'Reply');

	scrollToElement('#comment_area_' + replyId);

	setCaretPosition(replyId);
}

function onAddComment(comment) {
	if (jQuery('#comments_list_area').text().length < 50) {
		jQuery('#comments_list_area').empty();
	}
	jQuery('#comments_list_area').prepend(comment);
	totalComments++;
	updateCounter();
}

function onEditComment(comment) {
	jQuery('#comment_container_' + commentEditId).replaceWith(comment);
}

function onReplyComment(comment) {
	jQuery('#comment_children_' + commentReplyId).append(comment);
}

function setComments(response) {
	jQuery('div.more-comment').detach();
	if (response['data'] != '') {
		jQuery('#comments_list_area').append(response['data']);
	}
	totalComments = response['total'];
	updateCounter();
}

function updateCounter() {
	if (totalComments >= 0) {
		jQuery('#comments_counter').html('(' + totalComments + ')');
		jQuery('#itemCommentsCounter').html(totalComments);
	}
}

function closeAllMessageBoxes() {
	jQuery('div.reply-boxes').hide();
	jQuery('div.reply-boxes').empty();
}

function createComment(container, replyName, buttonName) {
	closeAllMessageBoxes();
	jQuery(container).html(buildCommentBox(replyName, buttonName))
	jQuery(container).show();
}

function limitText(limitField, limitNum) {
	var text = limitField.value;
	if (text.length > limitNum) {
		limitField.value = text.substring(0, limitNum);
	}
}

function buildCommentBox(commentText, buttonText) {
	return	'<textarea class="comment" onKeyUp="limitText(this, 500);">' + commentText + '</textarea>' +
			'<div class="group">' +
			'<div class="note-comment note"></div>' +
			'<div class="button-comment comment-reply"><a href="javascript:;" onclick="APP.postComment();">' + buttonText + '</a></div>' +
			'</div>';
}

function setCaretPosition(commentId) {
	var textField = jQuery('#comment_textarea_' + commentId);
	if (commentId > 0 && textField.val() != '') {
		if (textField.get(0).createTextRange) {
			var range = textField.get(0).createTextRange();
			range.move('character', textField.val().length);
			range.select();
		}
		else {
			textField.focus();
			textField.get(0).setSelectionRange(textField.val().length, textField.val().length);
		}
	}
}

function scrollToElement(anchor) {
	if ((jQuery(document).scrollTop() + jQuery(window).height() - 160) < jQuery(anchor).offset().top) {
		jQuery(document).scrollTop(jQuery(anchor).offset().top - jQuery(window).height() + 160);
	}
}
