
function sendLikes(postId) {
	$.post("/ajax/", { action: 'like', post_id: postId }, function(data){
		var likes = parseInt(data);
		if (likes != NaN && likes > 0) {
			$('#like-counter-' + postId).text(likes);
			$('#like-counter-link-' + postId).replaceWith($('#like-counter-link-' + postId).children());
			$('#like-link-' + postId).replaceWith($('<span class="symbol symbol-like"/>'));
		}
	});
}

function toggleComments(postId) {
	$("#post-drawer-tip-" + postId + ", #post-drawer-report-" + postId).slideUp("fast");
	$("#post-drawer-comment-" + postId).slideToggle("fast");
	reloadReCaptcha("recaptcha-comment-" + postId);
}

function toggleTip(postId) {
	$("#post-drawer-comment-" + postId + ", #post-drawer-report-" + postId).slideUp("fast");
	$("#post-drawer-tip-" + postId).slideToggle("fast");
	reloadReCaptcha("recaptcha-tip-" + postId);
}

function toggleReport(postId) {
	$("#post-drawer-comment-" + postId + ", #post-drawer-tip-" + postId).slideUp("fast");
	$("#post-drawer-report-" + postId).slideToggle("fast");
	reloadReCaptcha("recaptcha-report-" + postId);
}

function reloadReCaptcha(elementId) {
	Recaptcha.destroy();
	Recaptcha.create("6LeC-goAAAAAAEj53sPdq1ZshjNuy-AT5p6v4Tw5", elementId, { theme: 'white', tabindex: 0, callback: null });
}

$(document).ready(function(){
	
	$(".commentForm").submit(function(){
		
		var $this = $(this);
		var target = $this.attr("action");
		var postIdIndex = target.indexOf("post_id") + 8;
		var postId = parseInt(target.substr(postIdIndex));
		
		if (postId != NaN) {
		
			var values = new Object();
			
			values['name'] = $this.find(".comment_name").val();
			values['email'] = $this.find(".comment_email").val();
			values['blog'] = $this.find(".comment_blog").val();
			values['text'] = $this.find(".comment_text").val();
			values['post_id'] = postId;
			values['action'] = 'comment';
			values['recaptcha_challenge_field'] = Recaptcha.get_challenge();
			values['recaptcha_response_field'] = Recaptcha.get_response();
			
			try {
				
				$this.find(".sendButton").css("display", "none").after($('<img src="images/ajax-loader.gif" class="loader" />'));
				
				$.ajax({
					type: "post",
					url: "/ajax/",
					dataType: "json",
					data: values,
					timeout: 10000,
					error: function(request, error) {
						alert("Något gick fel och din kommentar har inte skickats.");
						$this.find(".sendButton").css("display", "block")
						$this.find(".loader").remove();
						Recaptcha.reload();
					},
					success: function(data) {
						
						var result = eval(data);
						
						if (result && typeof(result) == 'object' && (result instanceof Array)) {
						
							var commentList = $('<div/>');
							
							for (var i = 0; i < result.length; i++) {
								var comment = $(
									'<div class="span-16 last">' + 
									'	<div class="name"><strong>' + result[i]['name'] + '</strong></div>' +
									'	<div class="date"><em> - ' + result[i]['date'] + '</em></div>' +
									'</div>' +
									'<div class="span-14 last text">' +
									'	<p>' + result[i]['comment'] + '</p>' +
									'	<div class="separator2"></div><br/>' +
									'</div>'
								);
								commentList.append(comment);
							}
							
							$('#post-drawer-comment-list-' + postId).empty().append(commentList.children());
							$('#comment-count-' + postId).text("(" + result.length + ")");
							
							$this.find(".comment_name").val("");
							$this.find(".comment_email").val("");
							$this.find(".comment_blog").val("");
							$this.find(".comment_text").val("");
						}
						else {
							
							alert("Något gick fel och din kommentar har inte skickats. Kontrollera att du skrev rätt ord i spamskyddet.");
						}
						
						$this.find(".sendButton").css("display", "block")
						$this.find(".loader").remove();
						
						Recaptcha.reload();
					}
				});
				
			} catch (e) { console.log(e); return; }
		}
		return false;
	});
	
	$(".tipForm").submit(function(){
		
		var $this = $(this);
		var target = $this.attr("action");
		var postIdIndex = target.indexOf("post_id") + 8;
		var postId = parseInt(target.substr(postIdIndex));
		
		if (postId != NaN) {
		
			var values = new Object();
			
			values['name'] = $this.find(".tip_name").val();
			values['email'] = $this.find(".tip_email").val();
			values['message'] = $this.find(".tip_message").val();
			values['url'] = $this.find(".tip_url").val();
			values['post_id'] = postId;
			values['action'] = 'tip';
			values['recaptcha_challenge_field'] = Recaptcha.get_challenge();
			values['recaptcha_response_field'] = Recaptcha.get_response();
			
			try {
				
				$this.find(".sendButton").css("display", "none").after($('<img src="images/ajax-loader.gif" class="loader" />'));
				
				$.ajax({
					type: "post",
					url: "/ajax/",
					dataType: "json",
					data: values,
					timeout: 10000,
					error: function(request, error) {
						alert("Något gick fel och ditt tips har inte skickats.");
						$this.find(".sendButton").css("display", "block")
						$this.find(".loader").remove();
						Recaptcha.reload();
					},
					success: function(data) {
						
						$('#post-drawer-tip-' + postId + ' .ajax-result').empty().append($('<div class="span-16 last text ajax-result"><p>' + data + '</p></div>'));
			
						$this.find(".tip_name").val("");
						$this.find(".tip_email").val("");
						$this.find(".tip_message").val("");
						$this.find(".sendButton").css("display", "block")
						$this.find(".loader").remove();
						Recaptcha.reload();
					}
				});
				
			} catch (e) { console.log(e); return; }
		}
		return false;
	});
	
	$(".reportForm").submit(function(){
		
		var $this = $(this);
		var target = $this.attr("action");
		var postIdIndex = target.indexOf("post_id") + 8;
		var postId = parseInt(target.substr(postIdIndex));
		
		if (postId != NaN) {
		
			var values = new Object();
			
			values['name'] = $this.find(".report_name").val();
			values['email'] = $this.find(".report_email").val();
			values['message'] = $this.find(".report_message").val();
			values['url'] = $this.find(".report_url").val();
			values['post_id'] = postId;
			values['action'] = 'report';
			values['recaptcha_challenge_field'] = Recaptcha.get_challenge();
			values['recaptcha_response_field'] = Recaptcha.get_response();
			
			try {
				
				$this.find(".sendButton").css("display", "none").after($('<img src="images/ajax-loader.gif" class="loader" />'));
				
				$.ajax({
					type: "post",
					url: "/ajax/",
					dataType: "json",
					data: values,
					timeout: 10000,
					error: function(request, error) {
						alert("Något gick fel och din rapportering har inte skickats.");
						$this.find(".sendButton").css("display", "block")
						$this.find(".loader").remove();
						Recaptcha.reload();
					},
					success: function(data) {
						
						$('#post-drawer-report-' + postId + ' .ajax-result').empty().append($('<div class="span-16 last text ajax-result"><p>' + data + '</p></div>'));
			
						$this.find(".report_name").val("");
						$this.find(".report_email").val("");
						$this.find(".report_message").val("");
						$this.find(".sendButton").css("display", "block")
						$this.find(".loader").remove();
						Recaptcha.reload();
					}
				});
				
			} catch (e) { console.log(e); return; }
		}
		return false;
	});
});