(function(context,$) {
"use strict";
context.JK = context.JK || {};
context.JK.BandProfileScreen = function(app) {
var NOT_SPECIFIED_TEXT = 'Not specified';
var logger = context.JK.logger;
var rest = context.JK.Rest();
var bandId;
var isMember = false;
var isAdmin = false;
var band = {};
var instrument_logo_map = context.JK.getInstrumentIconMap24();
var profileUtils = context.JK.ProfileUtils;
var feed = new context.JK.Feed(app)
var $root = $("#band-profile")
var $history = $root.find("#band-profile-history")
function beforeShow(data) {
bandId = data.id;
feed.setBand(bandId);
}
function beforeHide(data) {
feed.setBand(null);
}
function afterShow(data) {
// hide until we know if 'isMember'
$("#btn-follow-band").hide();
$("#btn-edit-band-profile").hide();
$("#btn-edit-band-bio").hide();
$("#btn-edit-band-info").hide();
$("#btn-edit-band-members").hide();
$("#btn-edit-band-delete").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();
$("#btn-edit-band-bio").show();
$("#btn-edit-band-info").show();
$("#btn-edit-band-members").show();
if (isAdmin) {
$("#btn-edit-band-delete").show();
}
} else {
$("#btn-follow-band").show();
$("#btn-edit-band-bio").hide();
$("#btn-edit-band-profile").hide();
$("#btn-edit-band-info").hide();
$("#btn-edit-band-members").hide();
$("#btn-edit-band-delete").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').text(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').text(band.biography);
renderMusicalExperience()
profileUtils.renderPerformanceSamples(band, $root, isAdmin)
profileUtils.renderOnlinePresence(band, $root, isAdmin)
renderCurrentInterests()
// 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);
}
});
}
function renderMusicalExperience() {
var genres = buildGenreList(band.genres)
var gigs = (band.concert_count > 0) ? 'Has played ' + profileUtils.gigMap[band.concert_count] + ' live concert gigs' : NOT_SPECIFIED_TEXT;
var bandType ;
if (!band.band_type || typeof(band.band_type)=="undefined" || band.band_type==="") {
bandType = "Not specified";
} else if (band.band_type.toLowerCase()==="physical") {
bandType = "Physical";
} else if (band.band_type.toLowerCase()==="virtual") {
bandType = "Virtual";
} else {
bandType = "Not specified";
}
var bandStatus ;
if (!band.band_status || typeof(band.band_status)=="undefined" || band.band_status==="") {
bandStatus = "Not specified";
} else if (band.band_status.toLowerCase()==="amateur") {
bandStatus = "Amateur Band";
} else if (band.band_status.toLowerCase()==="professional") {
bandStatus = "Professional Band";
} else {
bandStatus = "Not specified";
}
$root.find(".experience-genres").html(genres)
$root.find(".experience-gigs").html(gigs)
$root.find(".experience-status").html(bandStatus)
$root.find(".experience-type").html(bandType)
}
function renderCurrentInterests() {
if (band.add_new_members) {
$root.find(".new-member-details").html(profileUtils.renderBandInstruments(band))
$root.find(".interests-new-members").removeClass("hidden")
} else {
$root.find(".interests-new-members").addClass("hidden")
}
if (band.paid_gigs) {
$root.find(".paid-gig-rate").html(band.hourly_rate)
$root.find(".paid-gig-minimum").html(band.gig_minimum)
$root.find(".interests-paid-gigs").removeClass("hidden")
} else {
$root.find(".interests-paid-gigs").addClass("hidden")
}
if (band.free_gigs) {
$root.find(".interests-free-gigs").removeClass("hidden")
} else {
$root.find(".interests-free-gigs").addClass("hidden")
}
}
/****************** 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() {
rest.getBandFollowers(bandId)
.done(function(response) {
$.each(response, function(index, val) {
// var template = $('#template-profile-social').html();
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);
if (index === response.length-1) {
context.JK.bindHoverEvents();
}
})
})
.fail(function(xhr) {
if(xhr.status >= 500) {
context.JK.fetchUserNetworkOrServerFailure();
}
else {
context.JK.app.ajaxError(arguments);
}
});
}
/****************** 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() {
feed.refresh();
}
function buildGenreList(genres) {
var list = '';
for (var i=0; i < genres.length; i++) {
list = list.concat(genres[i].id);
if (i !== genres.length - 1) {
list = list.concat(', ');
}
}
return list;
}
/****************** 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() {
rest.getBandMembers(bandId, false)
.done(function(response) {
bindMusicians(response, false);
})
.fail(function(xhr) {
if(xhr.status >= 500) {
context.JK.fetchUserNetworkOrServerFailure();
}
else {
context.JK.app.ajaxError(arguments);
}
});
}
function bindPendingMembers() {
rest.getBandMembers(bandId, true)
.done(function(response) {
if (response && response.length > 0) {
$("#band-profile-members").append("