function authInfo(response) {
    if (response.session) {
        Log('Auth OK');
        window.location.reload();
    } else {
        Log(response);
        Log('Auth Failed');
    } 
}


function fillForm() {
    VK.Auth.getLoginStatus(function(response){
        
        if (response.session) {
            Log('Session OK');
            
            var code;
            code = 'return {';
            code += 'me: API.getProfiles({uids: API.getVariable({key: 1280}), fields: "photo,nickname,first_name,last_name,domain"})[0]';            
            code += '};';
                        
            VK.Api.call('execute', {'code': code}, function(r){
                var me = r.response.me;
                
                var name = (me.nickname != '') ? me.nickname : me.first_name + ' '+me.last_name;
                Log(me); Log(name);
                
                $('#username').html(name + '<br /><a href="" onclick="logOut(); return false;">Выйти</a>');                
                $('#photo').html('<img src="'+me.photo+'" />');
                
                // inputs
                $('input[name=name]').val(name);
                $('input[name=page]').val('http://vkontakte.ru/id'+me.uid);
                $('input[name=photo]').val(me.photo);
                
                
            });            

        } else {
            Log('session is NULL');
            $('#comment_form').html('<tr><td>Вы не вошли в систему, комментарии могут оставлять только авторизованные пользователи. <div id="login_button" onclick="VK.Auth.login(authInfo);"></div></td></tr>');
            VK.UI.button('login_button');
        }
    });
}

function logOut() {
    VK.Auth.logout(function(r){
       window.location.reload(); 
    });
}


function Log(str) {
    if (window.console) {
        //console.log(str);
    }
}


