
$(document).ready( function () {
	initComments();
} );

function initComments() {
	$('.commentApprove,.commentReject,.commentRestore').bind('click', { }, function (e) {
		var cId = this.id.replace(/\D/g, '');
		var cAction = '';
		var oId = $('.comments:first').get(0).id.replace(/\D/g, '');
		if ($(this).hasClass('commentReject')) {
			//delete
			cAction = 'reject';
		} else if ($(this).hasClass('commentApprove')) {
			cAction = 'approve';
		} else if ($(this).hasClass('commentRestore')) {
			cAction = 'restore';
		} else {
			return false;
		}
		$.get(
				'/moderator/' + oId + '/comments/' + cId + '/' + cAction + '/',
				{},
				function (d) {
					parseCommentModeration(d, cId);
				}
		);
		
	});
}

function parseCommentModeration(d, cId) {
	var cStatus = $(d).find('status').text();
	var cCommentContainer = $('#commentContainer' + cId);
	if (cStatus) {
		switch (cStatus) {
			case '1':
				cCommentContainer.removeClass('deletedComment');
				cCommentContainer.removeClass('newComment');
				cCommentContainer.addClass('normalComment');
			break;
			
			case '2':
				cCommentContainer.addClass('deletedComment');
				cCommentContainer.removeClass('normalComment');
				cCommentContainer.removeClass('newComment');
			break;
		}
	}
}

function submitReview(oId) {
	var cContent = $('#review .review_content:first').val();
	if (cContent) {
		$.post(
			'/objects/' + oId + '/review/',
			{ content : cContent },
			function (e) {
				
				var op = $(e).find('operation:first');
				if (op && op.attr('status') == 'success') {
					// parse
					var content = $(e).find('review content:first').text();
					var added = $(e).find('review added:first').text();
					if (content && added) {
						$('#ownReview .content').html( content );
						$('#ownReview .date').html( added );
						$('#ownReview').removeClass('invisible');
						$('#ownReviewLink').addClass('invisible');
					}
				}
				
				getMagicPopup('review').close();
			}
		);
	}
	
}

