function numericalId(elem) {
	var attr = $(elem).attr("id");
	return attr.substr(attr.lastIndexOf('-') + 1);
}

function incrementElementBody(elem) {
	var cnt = $(elem).html();
	if (!cnt || isNaN(parseInt(cnt))) {
		cnt = 0;
	} else {
		cnt = parseInt(cnt);
	}
	$(elem).html(++cnt);
}

function setupStreamLinks(selector, stream) {
	if (!selector) {
		selector = '';
	}
	if (!stream) {
		stream = $('#stream');
	}
	$(stream).find('li.streamEntry' + selector + ' a.comment-button').click(function() {
		var ecElem = $(this).parents('.post').find('.comment-entry');
		$(ecElem).toggle();
		$(ecElem).find('textarea').focus();
		return false;
	});

	$(stream).find('li.streamEntry' + selector + ' .content .body').truncate({max_length: 500, more: 'Read more &raquo;', less: '&laquo;' });
	$(stream).find('li.streamEntry' + selector + ' button').button();
	$(stream).find('li.streamEntry' + selector + ' a.button').button();
	$(stream).find('li.streamEntry' + selector + ' .responses').each(function(index, elem) {
		var responses = $(elem).children('.response');
		var responsesCnt = responses.size();
		if (responsesCnt > 3) {
			$(elem).prepend($('<li/>')
				.addClass('show-all-notes')
				.append('<a data-ajax="false" href="#">View all ' + responsesCnt + ' notes</a>')
				.click(function() {
					$(elem).toggleClass('truncate');
					$(this).hide();
					return false;
				})
			);
			responses.slice(0, responsesCnt - 3).toggleClass('truncate');
		}
	});
}

function syncActivityValues(obj, activitySelector, validateUnsignedInt) {
	var val = $(obj).val();
	if (val == "") {
		hideMessage();
		return false;
	}
	$(obj).addClass("updated");
	val = parseFloat(val);
	activityFormInError = false;
	$('input.progressInput' + activitySelector).val(val);
	$('input.progressInput' + activitySelector).removeClass('placeheld');
	if (val >= 0) {
		$('input.progressInput' + activitySelector).siblings('.units').hide();
		$('.stepper' + activitySelector + ' a.stepDown').removeClass('disabled');
	} else {
		$('input.progressInput' + activitySelector).siblings('.units').show();
		$('.stepper' + activitySelector + ' a.stepDown').addClass('disabled');
	}
	return true;
}

function validateActivityForm(form, button) {
	$(button).button('disable');
	if ($(form).find('input#postfb:checked').length) {
		var continueSubmission = false;
		FB.login(function(r) {
			if (r.authResponse) {
				form.submit();
				continueSubmission = true;
			} else {
				$(button).button('enable');
				displayPageError('You need to be logged in to Facebook in order to cross post.');
			}
		}, {'perms': 'publish_stream'});
		return continueSubmission;
	} else {
		return true;
	}
}

function appendResponse(streamEntryId, response) {
	var r = 'responses-' + streamEntryId;
	if ($('#' + r).length == 0) {
		$('#entryComment-' + streamEntryId).before("<div id='" + r + "' class='responses'></div>");
	}
	$('#' + r).append(response);
}

function annotateLoading(streamEntryId, buttonClass) {
	$('#streamEntry-' + streamEntryId + ' .action-button.' + buttonClass + ' a').unbind('click');
	$('#streamEntry-' + streamEntryId + ' .action-button.' + buttonClass + ' a').attr('onclick', '');
	$('#streamEntry-' + streamEntryId + ' .action-button.' + buttonClass + ' a').attr('href', '#');
	$('#streamEntry-' + streamEntryId + ' .action-button.' + buttonClass).toggleClass('loading');
}

function annotateSuccess(data, textStatus, streamEntryId, buttonClass) {
	$('#streamEntry-' + streamEntryId + ' .action-button.' + buttonClass).addClass('user-selected');
	incrementElementBody($('#streamEntry-' + streamEntryId + ' .action-button.' + buttonClass + ' span.count'));
	appendResponse(streamEntryId, data);
}

function annotateFailure(request, textStatus, error, streamEntryId, buttonClass) {
	annotateLoading(streamEntryId, buttonClass);
	if (request.status == 401) {
		displayLoginForm(request,
			function(data, ts) {
				annotateSuccess(data, ts, streamEntryId, buttonClass);
			},
			function(request, ts) {
				annotateFailure(request, ts, streamEntryId, buttonClass);
			}
		);
	} else {
		displayPageError(request.responseText);
	}
}

function commentLoading(streamEntryId) {
	$('#postCommentForm-' + streamEntryId + ' button').button('disable');
}

function commentLoaded(streamEntryId) {
	$('#postCommentForm-' + streamEntryId + ' button').button('enable');
}

function commentSuccess(data, textStatus, streamEntryId) {
	commentLoaded(streamEntryId);
	appendResponse(streamEntryId, data);
	$('#entryComment-' + streamEntryId + ' textarea').val('');
	$('#entryComment-' + streamEntryId).toggle();
	$('#streamEntry-' + streamEntryId + ' .comment-button').addClass('user-selected');
	incrementElementBody($('#streamEntry-' + streamEntryId + ' .comment-button .comment-count'));
}

function commentFailure(request, textStatus, error, streamEntryId) {
	commentLoaded(streamEntryId);
	if (request.status == 401) {
		displayLoginForm(request,
			function(data, ts) {
				commentSuccess(data, ts, streamEntryId);
			},
			function(request, ts) {
				commentFailure(request, ts, streamEntryId);
			}
		);
	} else {
		displayPageError(request.responseText);
	}
}

function notificationActionLoading(id) {
	$('#notification-' + id).toggleClass('loading');
}

function notificationActionSuccess(data, textStatus, id) {
	$('#notification-' + id).hide('slow');
}

function notificationActionFailure(request, textStatus, error, id) {
	displayPageError(request.responseText);
	notificationActionLoading(id);
}

function leaderboardLoadingSuccess(data, textStatus, formSelector) {
	var lbc = $(formSelector).parent('.leaderboard-paging-container');
	lbc.before(data);
	lbc.remove();
	$('.leaderboard-paging-container button').button();
	if (sw_facebookPage) {
		resizeFacebookPage();
	}
}

function leaderboardLoading(form) {
	$(form).parent('.leaderboard-paging-container').hide();
}


