(function(context,$) {
"use strict";
context.JK = context.JK || {};
context.JK.BandProfileScreen = function(app) {
var logger = context.JK.logger;
var rest = context.JK.Rest();
var bandId;
var isMember = false;
var band = {};
var instrument_logo_map = context.JK.getInstrumentIconMap24();
function beforeShow(data) {
bandId = data.id;
}
function afterShow(data) {
// hide until we know if 'isMember'
$("#btn-follow-band").hide();
$("#btn-edit-band-profile").hide();
resetForm();
events();
determineMembership()
.done(function() {
renderActive();
})
.fail(function(jqXHR) {
app.notifyServerError(jqXHR, "Unable to talk to server")
})
}
function resetForm() {
$('#band-profile-instruments').empty();
$('#band-profile-about').show();
$('#band-profile-history').hide();
$('#band-profile-members').hide();
$('#band-profile-social').hide();
$('.band-profile-nav a.active').removeClass('active');
$('.band-profile-nav a.#band-profile-about-link').addClass('active');
}
/****************** MAIN PORTION OF SCREEN *****************/
function addFollowing(isBand, id) {
var newFollowing = {};
if (!isBand) {
newFollowing.user_id = id;
}
else {
newFollowing.band_id = id;
}
rest.addFollowing(newFollowing)
.done(function() {
if (isBand) {
var newCount = parseInt($("#band-profile-follower-stats").text()) + 1;
var text = newCount > 1 || newCount == 0 ? " Followers" : " Follower";
$('#band-profile-follower-stats').html(newCount + text);
configureBandFollowingButton(true);
}
else {
configureMemberFollowingButton(true, id);
}
renderActive();
})
.fail(app.ajaxError);
}
function removeFollowing(isBand, id) {
rest.removeFollowing(id)
.done(function() {
if (isBand) {
var newCount = parseInt($("#band-profile-follower-stats").text()) - 1;
var text = newCount > 1 || newCount == 0 ? " Followers" : " Follower";
$('#band-profile-follower-stats').html(newCount + text);
configureBandFollowingButton(false);
}
else {
configureMemberFollowingButton(false, id);
}
renderActive();
})
.fail(app.ajaxError);
}
function configureBandFollowingButton(following) {
$('#btn-follow-band').unbind("click");
if (following) {
$('#btn-follow-band').text('UNFOLLOW');
$('#btn-follow-band').click(function() {
removeFollowing(true, bandId);
return false;
});
}
else {
$('#btn-follow-band').text('FOLLOW');
$('#btn-follow-band').click(function() {
addFollowing(true, bandId);
return false;
});
}
}
function configureMemberFollowingButton(following, userId) {
var $btnFollowMember = $('div[user-id=' + userId + ']', '#band-profile-members').find('#btn-follow-member');
if (context.JK.currentUserId === userId) {
$btnFollowMember.hide();
}
else {
$btnFollowMember.unbind("click");
if (following) {
$btnFollowMember.text('UNFOLLOW');
$btnFollowMember.click(function() {
removeFollowing(false, userId);
return false;
});
}
else {
$btnFollowMember.text('FOLLOW');
$btnFollowMember.click(function() {
addFollowing(false, userId);
return false;
});
}
}
}
// refreshes the currently active tab
function renderActive() {
if (isMember) {
$("#btn-follow-band").hide();
$("#btn-edit-band-profile").show();
}
else {
$("#btn-follow-band").show();
$("#btn-edit-band-profile").hide();
}
if ($('#band-profile-about-link').hasClass('active')) {
renderAbout();
}
else if ($('#band-profile-history-link').hasClass('active')) {
renderHistory();
}
else if ($('#band-profile-members-link').hasClass('active')) {
renderMembers();
}
else if ($('#band-profile-social-link').hasClass('active')) {
renderSocial();
}
}
/****************** ABOUT TAB *****************/
function renderAbout() {
$('#band-profile-about').show();
$('#band-profile-history').hide();
$('#band-profile-members').hide();
$('#band-profile-social').hide();
$('.band-profile-nav a.active').removeClass('active');
$('.band-profile-nav a.#band-profile-about-link').addClass('active');
bindAbout();
}
function bindAbout() {
rest.getBand(bandId)
.done(function(response) {
band = response;
if (band) {
// name
$('#band-profile-name').html(band.name);
// avatar
$('#band-profile-avatar').attr('src', context.JK.resolveAvatarUrl(band.photo_url));
// location
$('#band-profile-location').html(band.location);
// stats
var text = band.follower_count > 1 || band.follower_count == 0 ? " Followers" : " Follower";
$('#band-profile-follower-stats').html(band.follower_count + text);
text = band.session_count > 1 || band.session_count == 0 ? " Sessions" : " Session";
$('#band-profile-session-stats').html(band.session_count + text);
text = band.recording_count > 1 || band.recording_count == 0 ? " Recordings" : " Recording";
$('#band-profile-recording-stats').html(band.recording_count + text);
$('#band-profile-biography').html(band.biography);
// wire up Follow click
configureBandFollowingButton(band.is_following);
}
else {
logger.debug("No band found with bandId = " + bandId);
}
})
.fail(function(xhr) {
if(xhr.status >= 500) {
context.JK.fetchUserNetworkOrServerFailure();
}
else if(xhr.status == 404) {
context.JK.entityNotFound("Band");
}
else {
context.JK.app.ajaxError(arguments);
}
});
}
/****************** SOCIAL TAB *****************/
function renderSocial() {
$('#band-profile-social-followers').empty();
$('#band-profile-about').hide();
$('#band-profile-history').hide();
$('#band-profile-members').hide();
$('#band-profile-social').show();
$('.band-profile-nav a.active').removeClass('active');
$('.band-profile-nav a.#band-profile-social-link').addClass('active');
bindSocial();
}
function bindSocial() {
// FOLLOWERS
var url = "/api/bands/" + bandId + "/followers";
$.ajax({
type: "GET",
dataType: "json",
url: url,
async: false,
processData:false,
success: function(response) {
$.each(response, function(index, val) {
var template = $('#template-band-profile-social').html();
var followerHtml = context.JK.fillTemplate(template, {
userId: val.id,
hoverAction: val.musician ? "musician" : "fan",
avatar_url: context.JK.resolveAvatarUrl(val.photo_url),
userName: val.name,
location: val.location
});
$('#band-profile-social-followers').append(followerHtml);
});
},
error: app.ajaxError
});
context.JK.bindHoverEvents();
}
/****************** HISTORY TAB *****************/
function renderHistory() {
$('#band-profile-about').hide();
$('#band-profile-history').show();
$('#band-profile-members').hide();
$('#band-profile-social').hide();
$('.band-profile-nav a.active').removeClass('active');
$('.band-profile-nav a.#band-profile-history-link').addClass('active');
bindHistory();
}
function bindHistory() {
}
/****************** BANDS TAB *****************/
function renderMembers() {
$('#band-profile-members').empty();
$('#band-profile-about').hide();
$('#band-profile-history').hide();
$('#band-profile-members').show();
$('#band-profile-social').hide();
$('.band-profile-nav a.active').removeClass('active');
$('.band-profile-nav a.#band-profile-members-link').addClass('active');
bindMembers();
}
function bindMembers() {
var url = "/api/bands/" + bandId + "/musicians";
$.ajax({
type: "GET",
dataType: "json",
url: url,
async: false,
processData:false,
success: function(response) {
bindMusicians(response);
},
error: app.ajaxError
});
if (isMember) {
bindPendingMembers();
}
}
function bindPendingMembers() {
var url = "/api/bands/" + bandId + "/musicians?pending=true";
$.ajax({
type: "GET",
dataType: "json",
url: url,
async: false,
processData:false,
success: function(response) {
if (response && response.length > 0) {
$("#band-profile-members").append("