// code that is executed on every user profile
$(document).ready(function() {
var wl = window.location;
var newPathName = wl.pathname;
// userID is defined in profile.html
if (newPathName.split("/")[2] != userID) {
newPathName = "/u/" + userID;
}
// if there's no mode parameter in the querystring, add it
if (wl.search.indexOf("mode=") === -1)
window.history.replaceState('', document.title, newPathName + "?mode=" + favouriteMode + wl.hash);
else if (wl.pathname != newPathName)
window.history.replaceState('', document.title, newPathName + wl.search + wl.hash);
setDefaultScoreTable();
// when an item in the mode menu is clicked, it means we should change the mode.
$("#mode-menu>.item").click(function(e) {
e.preventDefault();
if ($(this).hasClass("active"))
return;
var m = $(this).data("mode");
$("[data-mode]:not(.item):not([hidden])").attr("hidden", "");
$("[data-mode=" + m + "]:not(.item)").removeAttr("hidden");
$("#mode-menu>.active.item").removeClass("active");
var needsLoad = $("#scores-zone>[data-mode=" + m + "][data-loaded=0]");
if (needsLoad.length > 0)
initialiseScores(needsLoad, m);
$(this).addClass("active");
window.history.replaceState('', document.title, wl.pathname + "?mode=" + m + wl.hash);
});
initialiseAchievements();
initialiseFriends();
// load scores page for the current favourite mode
var i = function(){initialiseScores($("#scores-zone>div[data-mode=" + favouriteMode + "]"), favouriteMode)};
if (i18nLoaded)
i();
else
i18next.on("loaded", function() {
i();
});
});
function initialiseAchievements() {
api('users/achievements' + (currentUserID == userID ? '?all' : ''),
{id: userID}, function (resp) {
var achievements = resp.achievements;
// no achievements -- show default message
if (achievements.length === 0) {
$("#achievements")
.append($("
")
.text(T("Nothing here. Yet.")));
$("#load-more-achievements").remove();
return;
}
var displayAchievements = function(limit, achievedOnly) {
var $ach = $("#achievements").empty();
limit = limit < 0 ? achievements.length : limit;
var shown = 0;
for (var i = 0; i < achievements.length; i++) {
var ach = achievements[i];
if (shown >= limit || (achievedOnly && !ach.achieved)) {
continue;
}
shown++;
$ach.append(
$("
").append(
$("").popup({
title: ach.name,
content: ach.description,
position: "bottom center",
distanceAway: 10
})
)
);
}
// if we've shown nothing, and achievedOnly is enabled, try again
// this time disabling it.
if (shown == 0 && achievedOnly) {
displayAchievements(limit, false);
}
};
// only 8 achievements - we can remove the button completely, because
// it won't be used (no more achievements).
// otherwise, we simply remove the disabled class and add the click handler
// to activate it.
if (achievements.length <= 8) {
$("#load-more-achievements").remove();
} else {
$("#load-more-achievements")
.removeClass("disabled")
.click(function() {
$(this).remove();
displayAchievements(-1, false);
});
}
displayAchievements(8, true);
});
}
function initialiseFriends() {
var b = $("#add-friend-button");
if (b.length == 0) return;
api('friends/with', {id: userID}, setFriendOnResponse);
b.click(friendClick);
}
function setFriendOnResponse(r) {
var x = 0;
if (r.friend) x++;
if (r.mutual) x++;
setFriend(x);
}
function setFriend(i) {
var b = $("#add-friend-button");
b.removeClass("loading green blue red");
switch (i) {
case 0:
b
.addClass("blue")
.attr("title", T("Add friend"))
.html("");
break;
case 1:
b
.addClass("green")
.attr("title", T("Remove friend"))
.html("");
break;
case 2:
b
.addClass("red")
.attr("title", T("Unmutual friend"))
.html("");
break;
}
b.attr("data-friends", i > 0 ? 1 : 0)
}
function friendClick() {
var t = $(this);
if (t.hasClass("loading")) return;
t.addClass("loading");
api("friends/" + (t.attr("data-friends") == 1 ? "del" : "add"), {user: userID}, setFriendOnResponse, true);
}
var defaultScoreTable;
function setDefaultScoreTable() {
defaultScoreTable = $("