var CFacebook = {

    init:function()
    {
        // alert('CFacebook.init');
        window.fbAsyncInit = function() 
        {
            FB.init({appId: strFacebookAppID, status: true, cookie: true, xfbml: true});

            // All the events registered 
            FB.Event.subscribe('auth.login', function(response) 
            {
                // do something with response
                CFacebook.showUser();  
                // TODO
                // send user id ans post messige on wall 
                FB.api('/me', function(response)
                {
                    //alert(strRootPath+'users/login/'+response.id+'.html');
                    $.ajax({
                        cache: false, 
                        url:strRootPath+'users/login/'+response.id+'.html', 
                        success: function(data) { if(data != '') { alert(data); }}
                    });
                }); 
            });
            
            FB.Event.subscribe('auth.logout', function(response) 
            {
                CFacebook.logout(); 
            });

            FB.getLoginStatus(function(response) 
            {
                if (response.session) 
                {
                    // logged in and connected user, someone you know
                    CFacebook.showUser();
                }
                else
                {
                    CFacebook.showLoginButton();
                }
            }); 
        };
    
        (function() {
            var e = document.createElement('script');
            e.type = 'text/javascript';
            e.src = document.location.protocol + '//connect.facebook.net/bg_BG/all.js';
            e.async = true;
            document.getElementById('fb-root').appendChild(e);
        }());    
    },
    
    logout:function()
    {
        FB.logout(function(response) {
            // user is now logged out
            CFacebook.showLoginButton(); 
            window.location.reload();
        });
    },
    
    login:function(bReload)
    {
        FB.login(function(response) {
          if (response.session) {
            if (response.perms) {
              // user is logged in and granted some permissions.
              // perms is a comma separated list of granted permissions
              if(bReload)
              {
                  window.location.reload();     
              }
            } else {
              // user is logged in, but did not grant any permissions
            }
          } else {
            // user is not logged in
          }
        }, {perms:'email,user_birthday,status_update,publish_stream,user_about_me'}); 
    },
        
    showUser:function()
    {
        FB.api('/me', function(response)
        {
            $.ajax({
                cache: false,
                type: "POST",
                url: strRootPath+'users/getuser/'+response.id+'.html',
                success: function(data) {
                    if(data != '')
                    {
                        document.getElementById('fblogin').innerHTML = data;   
                    }
                }
            });
            /*
            var query = FB.Data.query('select name, username, hometown_location, sex, pic_square from user where uid={0}', response.id);
            query.wait(function(rows) 
            { 
                document.getElementById('fblogin').innerHTML = '<div style="float:left; width:50px;"><a href="' + strRootPath + 'users/' + response.id + '.html"><img src="' + rows[0].pic_square + '" alt="" /></a></div><div style="float:left; width:110px; padding:5px 0 0 10px;"><p class="logoutbtn"><a href="' + strRootPath + 'users/' + response.id + '.html">' + rows[0].name + '</a></p><p class="logoutbtn"><a href="javascript:CFacebook.logout();">logout</a></p></div><div class="clear"></div>';
            });
            */
        }); 
    },
    
    showLoginButton:function()
    {
         document.getElementById('fblogin').innerHTML = '<a class="button_fblogin" href="javascript:CFacebook.login();"></a>';  
    }       
}

$(document).ready(function()
{ 
    CFacebook.init();
});

