function postComment(comment)
{
	$('img#feedbackPreloader').show();
	$('div#feedback1').hide();
	$('div.feedback2').removeClass('feedbackError');
	$.post(	'/blog/wp-comments-ajaxpost.php',
			$('form#blogCommentForm').serializeArray(),
			function (response)
			{
				if (response.error || response.errorFields)
				{
					if (response.error)
					{
						alert(response.error);
					}
					if (response.errorFields)
					{
						var f = response.errorFields;
						for (var i = 0; i < f.length; i++)
							$('div#feedbackField' + f[i]).addClass('feedbackError');
					}
					$('div#feedback1').show();					
				}
				else
				{
					$('div#feedback2').show();
					setTimeout("location.reload()", 2000);
				}
				$('img#feedbackPreloader').hide();
			},
			'json'
			);
}

function isMouseOnBlog(e)
{
	var div = $('div#posts');
	var xy1 = getOffset(div[0]);
	var x2 = xy1.left + div.width() + 100;
	var y2 = xy1.top + div.height();
	return (e.clientX >= xy1.left && e.clientX <= x2 && e.clientY >= xy1.top && e.clientY <= y2);
}

$(document).ready( function() {
	//open comments form
	$('a.postCommentLink').click( function (e) {
		//e.preventDefault();
		$('div.feedback2').removeClass('feedbackError');
		var offset = $(this).offset();
		var visibility = null;
		var curId = $('div#blogComment').find('input[name="comment_parent"]').attr('value');
		var newId = $(this).attr('id').substr(8);
		if (curId == newId)
			visibility = $('div#blogComment').css('visibility') == 'hidden' ? 'visible' : 'hidden';
		else
			visibility = 'visible';
		$('div#blogComment').css('left', offset.left)
						    .css('top', offset.top + $(this).height() + 5)
							.css('visibility', visibility)
							.find('input, textarea').attr('value', '')
								.end()							
							.find('input[name="comment_post_ID"]')
								.attr('value', $('div.post').attr('id').substr(5))
								.end()
							.find('input[name="comment_parent"]')
								.attr('value', $(this).attr('id').substr(8));
	});
	//post comment
	$('a#feedbackSend').click( function (e) {
		e.preventDefault();
		postComment($('div#blogComment'));
	});
	$('div#geisha').css('height', $(document).height());
	$('div#osoboCopyright').css('bottom', 'auto')
						   .css('top', $(document).height() - 75);
	$('div#osoboPhone2').css('bottom', 'auto')
						   .css('top', $(document).height() - 75);
	$('a#email').css('bottom', 'auto')
						   .css('top', $(document).height() - 78);

	osoboMenu.addSkipFunction(isMouseOnBlog);
});