$(function() {
	$('.stop_following, .follow').click(function(e) {
		$this = $(this);
		var opts = {};
		if ($this.hasClass('follow'))
		{
			opts.action   = 'follow';
			opts.disabled = null;
			if (!$this.hasClass('nostop'))
			{
				opts.next_action = 'stop_following';
				opts.next_label  = 'Stop Following';
			}
			else
			{
				opts.next_action = '';
				opts.next_label  = 'Following';
				opts.disabled    = 'disabled';
			}
		}
		else if ($this.hasClass('stop_following'))
		{
			opts.action      = 'stop_following';
			opts.disabled    = null;
			opts.next_action = 'follow';
			opts.next_label  = 'Follow';
		}
		opts.user = $this.attr('id').split('_')[1];
		$this.attr('disabled', 'disabled').addClass('btn-disabled').html('Please Wait...');
		$.getJSON('/user/'+opts.action+'/'+opts.user, {}, function(data) {
			if (data == null || data.rsp != 'ok')
			{
				$this.replaceWith('<span class="error">Failed!</span"');
			}
			else
			{
				$this.fadeOut('fast', function() { $this.removeClass(opts.action).addClass(opts.next_action).html(opts.next_label).attr('disabled', opts.disabled).fadeIn('fast'); });
				if (opts.action == 'stop_following')
				{
					$('.only-following #following_'+opts.user).fadeOut('fast', function() { $(this).remove(); });
				}
				if (data.translate != null)
				{
					translate(data.translate);
				}
			}
		});
		e.stopPropagation();
		return false;
	});
});
