$(document).ready(function() {
	//setupRecaptcha();
	$('#leave_comment').bind('click', function() {
		$('#lte_content').slideUp();
		if($('#comment_form').is(':hidden') || !$('#comments .title').next().is('#comment_form, #comment_error')) {
			showForm($('#comments .title'));
		}
		return false;
	});
	$('.reply a').live('click', function() {
		var parent = $(this);
		do {
			parent = parent.parent();
		} while(!parent.hasClass('comment'));
		var replyId = $(this).attr('href').match(/[0-9]+/);
		if(!parent.next().is("#comment_form, #comment_error"))
			showForm(parent, replyId);
		return false;
	});
	$('#show_terms').bind('click', function() {
		var terms = $('#comment_terms');
		var html = $(this).html();
		if(terms.is(':hidden')) {
			terms.show();
			$(this).html(html.replace('Show', 'Hide'));
		} else {
			terms.hide();
			$(this).html(html.replace('Hide', 'Show'));
		}
		return false;
	});
	$('.scroll').bind('click', function() {
		var id = $(this).attr('href');
		scrollTo(id);
		return false;
	});
	$('#font_bigger').bind('click', function() {
		var size = $('#story').css('font-size');
		var unit = size.match(/[^0-9\.]+/, '');
		size = size.replace(unit, '');
		$('#story').css('font-size', size * 1.1 + unit);
		return false;
	});
	$('#font_smaller').bind('click', function() {
		var size = $('#story').css('font-size');
		var unit = size.match(/[^0-9\.]+/, '');
		size = size.replace(unit, '');
		$('#story').css('font-size', size * .9 + unit);
		return false;
	});
	$('#comment_form').bind('submit', function() {
		var string = $('#comment_the_form').serialize() + '&ajax=1';
		
		
		
		
		var response = $.ajax({
			'url': '/comment.php',
			'data': string,
			'type': 'POST',
			'dataType': 'json',
			'beforeSend': function(request) {
				$('#comment_form :input:visible').attr("disabled", "disabled");
				$('#comment_form :submit').attr('value', 'Posting...');				
			},
			'error': function(request, text, error) {
				window.location = "/comment.php?" + string.replace('&ajax=1', '');
			},
			'success': function(data, text) {
				if(data.success) {
					window.location.reload(true);
				} else {
					showError(data);
					$('#comment_form :input:disabled').removeAttr("disabled");
					$('#comment_form :submit').attr('value', 'Comment');
					Recaptcha.reload();
				}
			}
		});
		return false;
	});
	$('#comments .flag, #comments .delete').live('click', function() {
		var href = $(this).attr('rel');
		var string = href.match(/\?(.*)/)[1];
		var id = $(this).attr('id').match(/[0-9]+/);
		$.ajax({
			'url': '/comment.php',
			'data': string + '&ajax=1',
			'dataType': 'json',
			'error': function(response, text, error) {
				window.location = href;
			},
			'success': function(data, text) {
				if(data.flagged)
					$('#flag-' + id).replaceWith('<span>Flagged!</span>');
				else if(data.removed)
					$('#comment-' + id).html('<p>This comment has been removed by an administrator.</p>').addClass('deleted');
			}
		});
		return false;
	});

	var comment_offset = 0;
	$('#more_comments').click(function()
	{
		comment_offset = comment_offset+20;
		$('#more_comments .throbber').toggle();
		$.post(window.location.href, {ajaxComments: comment_offset},
			function(data)
			{
				$('#comments').append(data);
				if (alpha_comment_count < comment_offset+20)
				{
					$('#more_comments').hide();
				}
				else if ((alpha_comment_count-(comment_offset+20)) < 20)
				{
					$('#more_comments span').html('Show the remaining comments.');
				}

				$('#more_comments .throbber').toggle();
				//$('#more_comments span').html('Show '+(comment_offset+21)+'-'+(comment_offset+40)+' of '+comment_count+' comments');
			}, "html"
		);
	});
	$('.show_comment').live('click', function()
	{
		var id = $(this).attr('id').replace('comment_show_', '');
		$('#comment_body_'+id).slideToggle('fast');
		return false;
	});

});

function showForm(parent, reply)
{
	$('#comment_error').slideUp('slow', function() { $(this).remove(); });
	$('#comment_form').slideUp('slow', function() {
		parent.after($('#comment_form').slideDown('normal', function() {
			$('#commenter').focus();
		}));
		$('#comment_text').val('');
		
		scrollTo('#comment_form');
		setupRecaptcha();
	});
	
	$('#parent_comment').attr('value', (reply != null ? reply : ''));
}

function setupRecaptcha ()
{

		if ( $('#recaptcha_show') && typeof(FB) != 'undefined' )
		{
			FB.Connect.get_status().waitUntilReady(function(status){
				
				switch ( status )
				{
					case FB.ConnectState.connected:
						$('#comment_text').focus();
					break;
					case FB.ConnectState.appNotAuthorized:
					case FB.ConnectState.userNotLoggedIn:
						Recaptcha.create(RECAPTCHA_PUBLIC_KEY, 'recaptcha_show', {
						   theme: 'red',
						   tabindex: 0,
						   callback: Recaptcha.focus_response_field
						});
					break;
				}
			});
		}	
}

function showError(json)
{
	var error = $("<p></p>").html('Error: ' + json.error);
	var div = $('#comment_error');
	if(div.size() > 0)
		div.html(error);
	else
		$('#comment_errors').html($('<div id="comment_error"></div>').html(error));
}

function scrollTo(id)
{
	var targetOffset = $(id).offset().top;
	$('html,body').animate({scrollTop: targetOffset}, 1000);
}

