﻿function htmlDecode(value) {
	return $('<div/>').html(value).text();
}

function afterShowComments() {
	$('#comments_buttonsend').click(function() {
		var sendUrl = $('#comments_sendurl').val();
		var message = htmlDecode($('#comments_txtmessage').val());
		var fullname = htmlDecode($('#comments_txtfullname').val());
		var valid = 1;

		if (message == "") {
			$('#comments_txtmessage').focus();
			$('#comments_errors').html('Zpráva musí být vyplněna.').fadeIn();
			valid = 0;
		} else if (fullname == "") {
			$('#comments_txtfullname').focus();
			$('#comments_errors').html('Jméno musí být vyplněno.').fadeIn();
			valid = 0;
		}

		if (valid == 1) {
			$.ajax({
				url: sendUrl,
				data: "message=" + encodeURI(message) + "&fullName=" + encodeURI(fullname) + "",
				success: function(data, textStatus, jqXHR) {
					$('#comments_holder').html(data).fadeIn();
					afterShowComments(sendUrl);
				},
				error: function(jqXHR, textStatus, errorThrown) {
					$('#comments_buttonsend').show();
					$('#comments_errors').html(jqXHR.responseText).fadeIn();
				}
			});
			$('#comments_buttonsend').hide();
		}
		return false;
	});
	$('.comments_ajax').hide();
	$('html,body').scrollTop($("#comments_create").offset().top - 100 );
}

function afterShowResponse(commentId) {
	$('#comments_buttonsend' + commentId).click(function() {
		var sendUrl = $('#comments_sendurl').val();
		var message = htmlDecode($('#comments_txtmessage' + commentId).val());
		var fullname = htmlDecode($('#comments_txtfullname' + commentId).val());

		if (message == "") {
			$('#comments_txtmessage' + commentId).focus();
			$('#comments_errors' + commentId).html('Zpráva musí být vyplněna.').fadeIn();
		} else if (fullname == "") {
			$('#comments_txtfullname' + commentId).focus();
			$('#comments_errors' + commentId).html('Jméno musí být vyplněno.').fadeIn();
		} else {
			$.ajax({
				url: sendUrl,
				data: "commentId=" + commentId + "&message=" + encodeURI(message) + "&fullName=" + encodeURI(fullname) + "",
				success: function(data) {
					$('#comments_holder').html(data).fadeIn();
					afterShowComments(sendUrl);
				},
				error: function(jqXHR, textStatus, errorThrown) {
					$('#comments_buttonsend' + commentId).show();
					$('#comments_errors' + commentId).html(jqXHR.responseText).fadeIn();
				}
			});
			$('#comments_buttonsend' + commentId).hide();
		}
		return false;
	});
	$('html,body').scrollTop($("#comments_create" + commentId).offset().top - 100);
	$('#comments_txtmessage' + commentId).focus();
}

function showComments(commentId) {
	var showUrl = $('#comments_showcommentsurl').val();
	$.ajax({
		url: showUrl,
		success: function(data) {
			$('#comments_holder').html(data).fadeIn();
			afterShowComments();
			if (commentId) {
				$('html,body').scrollTop($("#comments_comment" + commentId).offset().top);
			}
		},
		error: function() { $('.comments_buttonshow').show(); }
	});
	$('.comments_buttonshow').hide();
	$('.comments_ajax').show();
	return false;
}

function showResponse(commentId) {
	var showUrl = $('#comments_showresponseurl').val();
	$.ajax({
		url: showUrl,
		data: "commentId=" + commentId,
		success: function(data) {
			$('#comments_responseholder' + commentId).html(data).fadeIn();
			afterShowResponse(commentId);
		},
		error: function() { $('#comments_buttonshow' + commentId).show(); }
	});
	$('#comments_buttonshow' + commentId).hide();
	return false;
}

function rateComment(commentId, rating) {
	var url = $('#comments_rateurl').val();
	$.ajax({
		url: url,
		data: "commentId=" + commentId + "&rating=" + rating,
		success: function(data) {
			$('#comments_holder').html(data).fadeIn();
		},
		error: function(jqXHR, textStatus, errorThrown) {
			alert(jqXHR.responseText);
		}
	});
	return false;
}

function censoreComment(commentId) {
	var url = $('#comments_censoreurl').val();
	jConfirm("Opravdu chcete smazat komentář?", "Potvrzení smazání", function(confirm) {
		if (confirm) {
			jPrompt("Uveďte prosím Váš důvod (není povinné, bude vidět pro uživatele!!):", "", "Důvod smazání", function(remark) {
				$.ajax({
					url: url,
					data: "commentId=" + commentId + "&remark=" + remark,
					success: function(data) {
						$('#comments_holder').html(data).fadeIn();
					},
					error: function(jqXHR, textStatus, errorThrown) {
						alert(jqXHR.responseText);
					}
				});
			});
		}
	});
	return false;
}

function banUserOrIp(commentId, user, ip) {
	var url = $('#comments_banurl').val();
	jConfirm("Opravdu chcete zablokovat uzivatele nebo IP adresu?", "Potvrzení zablokování", function(confirm) {
		if (confirm) {
			jPrompt("Uveďte prosím Váš důvod (není povinné, bude vidět pro uživatele!!):", "", "Důvod zablokování", function(remark) {
				$.ajax({
					url: url,
					data: "user=" + user + "&ip=" + ip + "&remark=" + remark,
					success: function(data) {
						$('#comments_holder').html(data).fadeIn();
					},
					error: function(jqXHR, textStatus, errorThrown) {
						alert(jqXHR.responseText);
					}
				});
			});
		}
	});
	return false;
}

