1
0
Fork 0
oldhaven/public/javascripts/beathaven/vkontakte.js

75 lines
2.0 KiB
JavaScript

function authInfo(response) {
if (typeof response != 'undefined' && response.session) {
$('#vk_login, .auth_notice').hide();
$('#vk_logout').css('display', 'block');
if ($('#search_field').length > 0) {
$('#search_field').focus();
}
VK.Api.call('getVariable', {key: 1281}, function(r) {
$('#header-container .hello').text('Hi there, '+ r.response +'!');
});
} else {
$('#vk_login, .auth_notice').css('display', 'block');
$('#vk_logout').hide();
}
}
$(function(){
VK.init({
apiId: (document.location.host == 'beathaven.org' ? 2335068 : 2383163),
nameTransportPath: "/demo/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) {
VK.Api.call('audio.search', {q: artist +' '+ track}, 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;
}