    //Try to wrap FB-stuff into dedicated object:
    //http://developers.facebook.com/docs/reference/javascript/
    var ytFB = window.ytFB || {};

    //Ultimately logs out from yt.
    ytFB.logout = function () {

       if( ytFB.logged ){
           //Redundant: page will be refreshed:
           //but.style.visibility = 'visible';
           //yt_login_button.setAttribute( 'class', 'login');
           //yt_login_button.setAttribute( 'title', 'Login');
           //yt_login_button.innerHTML = 'Login';
           FB.logout( function(response) {
               //Redundant: page will be refreshed:
               //tj$.digs( 'now, fb user is not logged out' );
               //tj$.digs( response );
               //ytFB.logged = false;
               //Enable login buttons:
               window.location = 'logout.redirect.php?called_after_fb_logout';
           });
           return false;

       }else{
           return true; //Call logout.redirect.php normal way.
       }

    };
 
    ytFB.login = function() { 

       var where_to_goto = '';
       if( arguments.length > 0 ) where_to_goto = arguments[0]; //{/literal}{$post_goto}{literal}

       var facebook_button = document.getElementById('facebook_login_menu_button');
       var yt_login_button = document.getElementById('yt_login_button');

           FB.login( 
              function(response) {
                 if (response.authResponse) {
                    ytFB.logged = true;
                    ytFB.user_id = response.authResponse.userID;
										FB.api('/me', function(response) {
                   		ytFB.name = response.name;
                   		ytFB.email = response.email;
                           var query_tail = 'fb_id=' + encodeURI(ytFB.user_id) + '&shake=' + encodeURI(new Date()) + '&fb_email=' + encodeURI(ytFB.email) + '&fb_name=' + encodeURI(ytFB.name);
                           if( where_to_goto == '' ){
                               var w = 'bind_to_fb.redirect.php?' + query_tail;
                           }else{
                               var w = 'bind_to_fb.redirect.php' + where_to_goto + '&' + query_tail;
                           }
                           window.location = w;
										});
										ytFBgetData();
											

                    //TODO Bad impression. Provide Progress indicator.
                    //facebook_button.style.visibility = 'hidden';
                    //yt_login_button.setAttribute( 'class', 'logout');
                    //yt_login_button.setAttribute( 'title', 'logout');
                    //yt_login_button.href="logout.redirect.php";
                    //yt_login_button.innerHTML = 'Logout';

                    //if( !yt.logged ){
                           //var pth = window.location.pathname;
                           //if( pth.substring(0,1) != '/' ) pth = '/'+pth;
                           /*var query_tail = 'fb_id=' + encodeURI(ytFB.user_id) + '&shake=' + encodeURI(new Date()) + '&fb_email=' + encodeURI(ytFB.email) + '&fb_name=' + encodeURI(ytFB.name);
                           if( where_to_goto == '' ){
                               var w = 'bind_to_fb.redirect.php?' + query_tail;
                           }else{
                               var w = 'bind_to_fb.redirect.php' + where_to_goto + '&' + query_tail;
                           }
                           window.location = w;
														*/
                           return false; //Need?
                    //}else{
                    //       ytFB.getData(); //Slow?
                    //}


                    //if (response.perms) {
                     //  tj$.digs( response );
                       // user is logged in and granted some permissions.
                       // perms is a comma separated list of granted permissions
                    //} else {
                       // user is logged in, but did not grant any permissions
                      // tj$.digs( 'no perm' );     
                    //}
                 } else {
                    // user is not logged in
                    //tj$.digs( 'not logged in' );
                    ytFB.user_id = null;
                    facebook_button.style.visibility = 'visible';
                 }
              },
              {scope:'email,photo_upload,video_upload,read_stream,publish_stream'},
							{oauth:true}
              //read_stream,status_update,photo_upload,video_upload,offline_access,email,create_note,share_item,publish_stream
           );
    };

    ytFB.getData = function () {
        tj$.digs( "getData began" );
        var query = FB.Data.query('select name, email, uid from user where uid={0}', ytFB.user_id);
        query.wait(
            function(rows) {
                   tj$.digs( rows );
                   ytFB.name = rows[0].name;
                   ytFB.email = rows[0].email;
                   tj$.digs( 'fb-query arrived: Email=' + ytFB.email );
            } //function(rows)
        ); //query.wait(
    }; //ytFB.getData

    //TODO:
    ytFB.getLoginStatus = function (){
       FB.getLoginStatus(
          function(response) {
             if (response.status === 'connected') {
                tj$.digs( response );
                // logged in and connected user, someone you know
             } else {
                // no user session available, someone you dont know
             }
          });
    };

