var isLogged = false;
var gid = '-212368';
var count = '5';
var commentos = [] ;
var users =[];
var refreshTime=30000;
function refreshPage(checkLogged)
{
if((checkLogged && !isLogged) || !checkLogged)
{
location.href = 'http://astana.en.cx';
}
}
function getComments(){
commentos = [] ;
VK.Api.call('wall.get', {owner_id: gid,count:count}, function(data) {
if(!data.error)
{
if(data.response.length > 0)
{
$('#comments').empty();
for(i = 1; i < data.response.length; i++)
{
commentos.push(data.response[i]);
/*var comment=data.response[i];
VK.Api.call('getProfiles', {uids:comment.from_id,fields:'photo_rec'}, function(profile_data) {
if(!profile_data.error)
{
if(profile_data.response.length > 0)
{
for(var j = 0; j < profile_data.response.length; j++)
{
var user=profile_data.response[j];
$('#comments').append('
');
if(comment.comments.count>0){
getComments(comment.id);
}
}
}
}else
{
alert(profile_data.error.error_msg);
}
});*/
}
}
if(commentos.length > 0)
{
var users_id = [];
for(var i = 0; i < commentos.length; i++)
{
users_id.push(commentos[i].from_id);
/*VK.Api.call('getProfiles', {uids:commentos[i].from_id,fields:'photo_rec'}, function(profile_data) {
if(!profile_data.error)
{
if(profile_data.response.length > 0)
{
for(var j = 0; j < profile_data.response.length; j++)
{
var user=profile_data.response[j];
$('#comments').append('');
if(commentos[i].comments.count>0){
getComments(commentos[i].id);
}
}
}
}else
{
alert(profile_data.error.error_msg);
}
});*/
}
VK.Api.call('getProfiles', {uids:users_id,fields:'photo_rec'}, function(profile_data) {
users =[];
if(!profile_data.error)
{
if(profile_data.response.length > 0)
{
for(j = 0; j < profile_data.response.length; j++)
{
var user=profile_data.response[j];
users.push(user);
}
for(i = 0; i < commentos.length; i++)
{
for(j = 0; j < users.length; j++)
{
if(commentos[i].from_id==users[j].uid){
$('#comments').append('');
/*if(commentos[i].comments.count>0){
getComments(commentos[i].id);
}*/
}
}
}
}
}else
{
alert(profile_data.error.error_msg);
}
});
}
}else
{
alert(data.error.error_msg);
}
} );
}
function getAnswers(postid){
VK.Api.call('wall.getComments', {owner_id:gid,post_id:postid}, function(data) {
if(!data.error)
{
if(data.response.length > 0)
{
for(i = 1; i < data.response.length; i++)
{
comments.push(data.response[i]);
}
}
if(comments.length > 0)
{
var users_id = [];
for(i = 0; i < comments.length; i++)
{
var comment=comments[i];
users_id.push(comments[i].from_id);
}
var users =[];
VK.Api.call('getProfiles', {uids:users_id,fields:'photo_rec'}, function(profile_data) {
if(!profile_data.error)
{
if(profile_data.response.length > 0)
{
for(j = 0; j < profile_data.response.length; j++)
{
var user=profile_data.response[j];
users.push(user);
}
}
}else
{
alert(profile_data.error.error_msg);
}
});
for(i = 0; i < comments.length; i++)
{
for(j = 0; j < users.length; j++)
{
if(comments[i].from_id==users[j].uid){
$('tr#c'+postid).after('');
if(comments[i].reply_to_cid && comments[i].reply_to_cid!=comments[i].cid){
getComments(comments[i].id);
}
break;
}
}
}
}
}else
{
alert(data.error.error_msg);
}
});
}
$(document).ready(function(){
VK.init({
apiId: 2746304
});
getComments(0);
VK.Auth.getLoginStatus(function(response){
if(response.session)
{
isLogged = true;
$('div#login-bar').hide();
$('div#logout-bar').show();
$('div#current-user-bar').show();
$('div#vk-chat-body').show();
$('div#vk-chat-footer').show();
VK.Api.call('getProfiles', {uids: response.session.mid,fields:'photo_rec'}, function(data) {
if(!data.error)
{
if(data.response.length > 0)
{
for(i = 0; i < data.response.length; i++)
{
var user = data.response[i];
if(user.photo_rec)
{
$('div#current-user-bar').html(''+''+user.first_name+' '+user.last_name+'
');
}
}
}
}
else
{
if(data.error.error_code == 7)
{
isLogged = false;
$('div#login-bar').show();
$('div#logout-bar').hide();
$('div#current-user-bar').hide();
$('div#vk-chat-body').hide();
$('div#vk-chat-footer').hide();
}
else
{
alert(data.error.error_msg);
}
}
});
} else
{
$('div#login-bar').show();
$('div#logout-bar').hide();
$('div#current-user-bar').hide();
$('div#vk-chat-body').hide();
$('div#vk-chat-footer').hide();
}
});
VK.Observer.subscribe('auth.login', function(response){
refreshPage(true);
});
VK.Observer.subscribe('auth.sessionChange', function(response){
refreshPage(true);
});
$('a#vk-login').click(function(event){
event.preventDefault();
VK.Auth.login(null, 8194);
});
$('input#comment-publish').click(function(event){
event.preventDefault();
if(isLogged){
var message=$('#comment-text-editor').val();
message=$.trim(message);
if(message==''){
alert('Введите текст сообщения!');
}else{
VK.Api.call('wall.post', {owner_id: gid,message:message}, function(data) {
if(!data.error)
{
if(data.response.post_id > 0)
{
$('textarea#comment-text-editor').val('');
getComments();
}
}
});
}
}
});
$('a#vk-refresh').click(function(event){
getComments();
});
$('a#vk-refresh').everyTime(refreshTime, function() {
if(isLogged){
getComments();
}
});
$('a#vk-logout').click(function(event){
event.preventDefault();
VK.Auth.logout(function(){
refreshPage(false);
});
});
});