(function ($) { var TAB_COUNT = 0; var topArtistsLoaded = false; var topAlbumsLoaded = false; var topTracksLoaded = false; var tasteometerLoaded = false; var username; $(document).ready(function () { init(); }); function init() { username = $('#lastfm_username').text(); $('#tabs').children('a').each(function () { var i = TAB_COUNT; $(this).click(function () { switchTo(i); return false; }); TAB_COUNT++; }); } function getTopArtists(div) { if (!topArtistsLoaded) { div.html('Loading...'); ajax.get('user.php?action=lastfm_top_artists&username=' + username, function (response) { var json = JSON.parse(response); if (json != null && json['error']) { div.html(json['message']); } else if (json == null) { div.html("Error"); } else { var j = json['topartists']['artist']; var html = 'Top Artists'; div.html(html); topArtistsLoaded = true; } }); } } function getTopAlbums(div) { if (!topAlbumsLoaded) { div.html('Loading...'); ajax.get('user.php?action=lastfm_top_albums&username=' + username, function (response) { var json = JSON.parse(response); if (json != null && json['error']) { div.html(json['message']); } else if (json == null) { div.html("Error"); } else { var j = json['topalbums']['album']; var html = 'Top Albums'; div.html(html); topAlbumsLoaded = true; } }); } } function getTopTracks(div) { if (!topTracksLoaded) { div.html('Loading...'); if (json != null && json['error']) { div.html(json['message']); } else if (json == null) { div.html("Error"); } else { ajax.get('user.php?action=lastfm_top_tracks&username=' + username, function (response) { var json = JSON.parse(response); var j = json['toptracks']['track']; if (j != null) { var html = 'Top Tracks'; div.html(html); } else { div.html('Error'); } topTracksLoaded = true; }); } } } function getTasteometer(div) { if (!tasteometerLoaded) { div.html('Loading...'); ajax.get('user.php?action=lastfm_compare_users&username=' + username, function (response) { var json = JSON.parse(response); if (json != null && json['error']) { div.html(json['message']); } else if (json == null) { div.html("Error, do you have your Last.FM username set in settings?"); } else { var j = json['comparison']['result']; var a = j['artists']['artist']; console.log(a); var compatibility = Math.round(j['score'] * 100); var html = 'Tasteometer
Compatibility: ' + compatibility + '% '; div.html(html); tasteometerLoaded = true; } }); } } function switchTo(tab) { var i = 0; $('#tabs').children('a').each(function () { if (i != tab) { $(this).css('font-weight', ''); } else { $(this).css('font-weight', 'bold'); } i++; }); i = 0; $('#contents_div').children('div').each(function () { if (i != tab) { $(this).hide(); } else { $(this).show(); switch (tab) { case 1: getTopArtists($(this)); break; case 2: getTopAlbums($(this)); break; case 3: getTopTracks($(this)); break; case 4: getTasteometer($(this)); break; default: break; } } i++; }); } })(jQuery);