function authInfo(response) { if (typeof response != 'undefined' && response.session) { Session.setVkParams(response.session); $('#vk_login, .auth_notice').hide(); $('#vk_logout').css('display', 'block'); if ($('#search_field').length > 0) { $('#search_field').focus(); } Session.query('/user/auth', {}, function(ar){ if (!ar.username) { VK.Api.call('getVariable', {key: 1281}, function(r) { Session.query('/user/update', {'name': r.response}, function(ar2) { $('#header-container .hello .greating').text('Hi there, '+ (ar2.username ? ar2.username : '%username%') +'!'); }); }); } $('#header-container .hello .greating').text('Hi there, '+ (ar.username ? ar.username : '%username%') +'!'); Session.user_id = ar.id; }); } else { $('#vk_login, .auth_notice').css('display', 'block'); $('#vk_logout').hide(); } } $(function(){ VK.init({ apiId: (document.location.host == 'beathaven.org' ? 2335068 : 2383163), nameTransportPath: "/xd_receiver.html" }); VK.Auth.getLoginStatus(authInfo); $('#vk_login').click(function(){ VK.Auth.login(authInfo, 8); }); $('#vk_logout').click(function(){ VK.Auth.logout(authInfo); }); }) function loadTracksData(artist, track, duration, callback) { track_prepared = track.replace(/\(.*\)/i, '').split('/')[0]; VK.Api.call('audio.search', {q: artist +' '+ track_prepared}, function(r){ callback(matchPerfectResult(r.response, artist, track, duration)); }) } function matchPerfectResult(data, artist, track, duration) { var duration = duration.split(':'); if (duration[1].charAt(0) === '0') { duration[1] = duration[1].substring(1); } duration = parseInt(duration[0]) * 60 + parseInt(duration[1]); var best_score = 0; var best_result = null; for (var i = 1; i < data.length; i++) { var score = 0; if (data[i].artist === artist) { score += 10; } else if (data[i].artist.split(artist).length === 2) { score += 5; } else if (data[i].title.split(artist).length === 2) { score += 4; } if (data[i].title === track) { score += 10; } else if (data[i].title.split(track).length === 2) { score += 5; } if (parseInt(data[i].duration) === duration) { score += 15; } else { var delta = Math.abs(parseInt(data[i].duration) - duration); if (delta < 10) { score += (10 - delta); } } if (score > best_score) { best_score = score; best_result = data[i]; } if (score === 35) { return best_result.url; } } return best_result.url; }