$('document').ready( function () {
	var noCache = 0;
	$('#newQuotation').click(function () {
		$('#randomQuoteSpace').load("../htmlRandomBlogQuotation.php", "id=randomQuoteSpace&noCache=" + noCache);
		noCache++;
	});	
})

function postCommentDialogue(blogId)
{
	$('#grayout').width($(document).width());
	$('#grayout').height($(document).height());
	$('#grayout').show();
	var newFrame = document.createElement("IFRAME");
	newFrame.setAttribute("src", "postComment.php?blog_id=" + blogId);
	newFrame.style.width = "700px";
	newFrame.style.height = "540px";
	newFrame.id = "modalIFrame";
	$('#modal').css("top", calculateYPlacement($(window).height(), 
			   540, 
			   $(document).scrollTop()));
	$('#modal').css("left", calculateXPlacement($(window).width(), 
												700, 
												$(document).scrollLeft()));	
	$('#modal').append(newFrame);
	$('#modal').show();
	$('#modalCloseBtn').css("top", ($('#modal').position().top - 15));
	$('#modalCloseBtn').css("left", ($('#modal').position().left + $('#modal').width() - 15));
	$('#modalCloseBtn').click(function () {
		hideModals();
	})
	$('#modalCloseBtn').show();
}

function postCommentReplyDialogue(commentId)
{
	$('#grayout').width($(document).width());
	$('#grayout').height($(document).height());
	$('#grayout').show();
	var newFrame = document.createElement("IFRAME");
	newFrame.setAttribute("src", "postComment.php?comment_id=" + commentId);
	newFrame.style.width = "700px";
	newFrame.style.height = "540px";
	newFrame.id = "modalIFrame";
	$('#modal').css("top", calculateYPlacement($(window).height(), 
			   540, 
			   $(document).scrollTop()));
	$('#modal').css("left", calculateXPlacement($(window).width(), 
												700, 
												$(document).scrollLeft()));	
	$('#modal').append(newFrame);
	$('#modal').show();
	$('#modalCloseBtn').css("top", ($('#modal').position().top - 15));
	$('#modalCloseBtn').css("left", ($('#modal').position().left + $('#modal').width() - 15));
	$('#modalCloseBtn').click(function () {
		hideModals();
	})
	$('#modalCloseBtn').show();
}

function calculateYPlacement(outerFrameHeight, innerFrameHeight, offset)
{
	return (outerFrameHeight / 2) - (innerFrameHeight / 2) + offset;
}

function calculateXPlacement(outerFrameWidth, innerFrameWidth, offset)
{
	return (outerFrameWidth / 2) - (innerFrameWidth / 2) + offset;
}

function hideModals()
{
	$('#grayout').hide();
	$('#modalIFrame').remove();
	$('#modal').hide();
	$('#modalCloseBtn').hide();
}

function refreshBlogComments(blogId)
{
	$('#commentSection_' + blogId).load("_commentList.php?blog_id=" + blogId);
	hideModals();
}

function blockIPAndDeleteComment(commentId, blogId)
{
	if (confirm('Are you certain you wish to delete this comment and block the IP address of the author?')) {
		var deleteReturn = $.ajax({
			type: 'POST',
			url: '_deleteComment.php',
			data: 'comment_id=' + commentId + "&block=IP",
			async: false
		}).responseText;
		if (deleteReturn != 'success') {
			alert("Error deleting comment.");
			$('body').html(deleteReturn);
		} else {
			$('#commentSection_' + blogId).load("_commentList.php?blog_id=" + blogId);
		}
	}
}

function blockUserAndDeleteComment(commentId, blogId)
{
	if (confirm('Are you certain you wish to delete this comment and block the user by name?')) {	
		var deleteReturn = $.ajax({
			type: 'POST',
			url: '_deleteComment.php',
			data: 'comment_id=' + commentId + "&block=USER",
			async: false
		}).responseText;
		if (deleteReturn != 'success') {
			alert("Error deleting comment.");
			$('body').html(deleteReturn);
		} else {
			$('#commentSection_' + blogId).load("_commentList.php?blog_id=" + blogId);
		}
	}
}

function deleteComment(commentId, blogId)
{
	if (confirm('Are you certain you wish to delete this comment?')) {
		var deleteReturn = $.ajax({
			type: 'POST',
			url: '_deleteComment.php',
			data: 'comment_id=' + commentId,
			async: false
		}).responseText;
		if (deleteReturn != 'success') {
			alert("Error deleting comment.");
			$('body').html(deleteReturn);
		} else {
			$('#commentSection_' + blogId).load("_commentList.php?blog_id=" + blogId);
		}
	}
}
