diff -upbN fbconnect/changes.txt fbconnect/changes.txt
--- fbconnect/changes.txt	1970-01-01 03:00:00.000000000 +0300
+++ fbconnect/changes.txt	2009-11-26 05:41:13.000000000 +0300
@@ -0,0 +1,22 @@
+2009/11/26 - vectoroc
+  - Extend users.mail field upto 320 chars to support proxied_mail in fast registration mode (monkey patch)
+  - Some bugfixes
+  
+2009/11/25 - vectoroc
+  - Ability to link/unlink Facebook acount from user profile
+  - Some minor changes
+
+2009/11/18 - vectoroc
+  - Supports SSL, http://wiki.developers.facebook.com/index.php/Facebook_Connect_Via_SSL.
+  - User profile pictures now are loaded through FBML (from DFF module - drupal.org/project/fb) 
+  - Some addition settings (check at admin/settings/fbconnect)
+  - Most of js code is moved to fbconnect.js
+  - New hooks: hook_fbconnect_config_alter, hook_fbconnect_login_button_alter 
+  
+  Patches from community:
+   - http://drupal.org/node/483282#comment-2118418
+   - http://drupal.org/node/367244#comment-2117946
+   - http://drupal.org/node/574210
+   - http://drupal.org/node/605174
+   
+Patch set can be found at http://drupal.org/node/635568
\ No newline at end of file
Common subdirectories: fbconnect/facebook-client and fbconnect/facebook-client
diff -upbN fbconnect/fbconnect.admin.inc fbconnect/fbconnect.admin.inc
--- fbconnect/fbconnect.admin.inc	2009-06-03 17:51:06.000000000 +0400
+++ fbconnect/fbconnect.admin.inc	2009-11-26 06:06:29.000000000 +0300
@@ -50,6 +50,13 @@ function fbconnect_admin_settings() {
     '#description' => t('Unable this mode will bypass drupal\'s standard registration form.'),
   );
   
+  $form['fbconnect_auto_login_logout'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Auto login/logout'),
+    '#default_value' => variable_get('fbconnect_auto_login_logout', false),
+    '#description' => t('Auto login to drupal/logout from facebook. Enable this if you don\'t want to logout automatically from facebook.'),
+  );
+  
   $form['fbconnect_invite_msg'] = array(
     '#type' => 'textfield',
     '#title' => t('Invite message'),      
@@ -70,11 +77,11 @@ function fbconnect_admin_settings() {
 
   if (fbconnect_get_config()) {  
     $button = array(
-      'small_short'  => fbconnect_render_button('small', 'short'),
-      'medium_short' => fbconnect_render_button('medium', 'short'),
-      'medium_long'  => fbconnect_render_button('medium', 'long'),
-      'large_short'  => fbconnect_render_button('large', 'short'),
-      'large_long'   => fbconnect_render_button('large', 'long'),
+      'small_short'  => fbconnect_render_button(array('size' => 'small' , 'length' => 'short', 'onclick' => 'return false;')),
+      'medium_short' => fbconnect_render_button(array('size' => 'medium', 'length' => 'short', 'onclick' => 'return false;')),
+      'medium_long'  => fbconnect_render_button(array('size' => 'medium', 'length' => 'long', 'onclick' => 'return false;')),
+      'large_short'  => fbconnect_render_button(array('size' => 'large' , 'length' => 'short', 'onclick' => 'return false;')),
+      'large_long'   => fbconnect_render_button(array('size' => 'large' , 'length' => 'long', 'onclick' => 'return false;')),
     );
 
     $form['fbconnect_button'] = array(
@@ -95,6 +102,12 @@ function fbconnect_admin_settings() {
     '#default_value' => variable_get('fbconnect_com_feed_id', FBCONNECT_COMMENT_FEED_BUNDLEID),
   );
 
+  $form['fbconnect_debug'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Debug mode'),      
+    '#default_value' => variable_get('fbconnect_debug', false),
+  );
+
   $form = system_settings_form($form);
   $form['#submit'][] = 'fbconnect_set_appproperties';
   return $form;
@@ -113,6 +126,7 @@ function fbconnect_set_appproperties($fo
   $properties = array(
     'connect_url' => $form_state['values']['fbconnect_connect_url'],
     'base_domain' => $form_state['values']['fbconnect_base_domaine'],
+    'uninstall_url' => url('fbconnect/post-remove', array('absolute' => true)),
   );
   try {
     $res = facebook_client()->api_client->admin_setAppProperties($properties);
diff -upbN fbconnect/fbconnect.install fbconnect/fbconnect.install
--- fbconnect/fbconnect.install	2009-06-03 17:51:06.000000000 +0400
+++ fbconnect/fbconnect.install	2009-11-26 06:05:14.000000000 +0300
@@ -38,7 +38,7 @@ function fbconnect_schema() { 
   $schema['fbconnect_users'] = array( 
     'fields' => array( 
       'uid'            => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), 
-      'fbuid'          => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
+      'fbuid'          => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'size' => 'big'),
       'timestamp'      => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
     ), 
     'primary key' => array('fbuid'), 
@@ -57,3 +57,32 @@ function fbconnect_update_6002() {
   $ret[] = update_sql("UPDATE {system} SET schema_version = 6002 WHERE name = 'fbconnect'");
   return $ret;
 }
\ No newline at end of file
+
+/**
+ * Alter fbuid field to allow for 64-bit facebook ids
+ */
+function fbconnect_update_6003() {
+  $ret = array();
+  db_drop_primary_key($ret, 'fbconnect_users');
+  db_change_field($ret, 'fbconnect_users', 'fbuid', 'fbuid',
+    array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'size' => 'big'),
+    array('primary key' => array('fbuid'))
+  );
+  return $ret;
+}
+
+
+/**
+ * Extend maximum email length to 320 chars
+ */
+function fbconnect_update_6004() {
+  $res = array();
+  $scheme = user_schema();
+  $scheme['users']['fields']['mail']['length'] = 320;
+  db_change_field($res, 'users', 'mail', 'mail', $scheme['users']['fields']['mail']);
+  return $res;
+}
+
+function fbconnect_schema_alter(&$schema) {
+  $schema['users']['field']['mail']['length'] = 320;
+}
\ No newline at end of file
diff -upbN fbconnect/fbconnect.js fbconnect/fbconnect.js
--- fbconnect/fbconnect.js	1970-01-01 03:00:00.000000000 +0300
+++ fbconnect/fbconnect.js	2009-11-25 23:54:46.000000000 +0300
@@ -0,0 +1,18 @@
+Drupal.behaviors.fbconnect = function(context) {
+	if (Drupal.settings.fbconnect && Drupal.settings.fbconnect.api_key) {
+		FB.Bootstrap.requireFeatures(["Connect","XFBML"], function() {
+			FB.Facebook.init(
+				Drupal.settings.fbconnect.api_key, 
+				Drupal.settings.basePath + Drupal.settings.fbconnect.xd_path
+			);
+			$(context).each(function() {
+				var elem = $(this).get(0);
+				FB.XFBML.Host.parseDomElement(elem);
+			});
+		});
+	}
+};
+
+function facebook_onlogin_ready() {
+	$("#fbconnect-autoconnect-form").submit();
+}
\ No newline at end of file
diff -upbN fbconnect/fbconnect.module fbconnect/fbconnect.module
--- fbconnect/fbconnect.module	2009-06-03 17:51:06.000000000 +0400
+++ fbconnect/fbconnect.module	2009-11-26 05:38:22.000000000 +0300
@@ -52,10 +52,25 @@ function fbconnect_menu() {
     'file' => 'fbconnect.pages.inc',
   );
   
+  $items['fbconnect/post-remove'] = array(
+    'page callback' => 'fbconnect_post_remove_callback',
+    'access callback' => 'user_access',
+    'access arguments' => array('access content'),
+    'type' => MENU_CALLBACK,
+  );
+  
+  // Cross-domain receiver.
+  $items['fbconnect/receiver'] = array(
+    'page callback' => 'fbconnect_receiver',
+    'access callback' => TRUE,
+    'type' => MENU_CALLBACK,
+    'file' => 'xd_receiver.inc',
+  );
+
+
   return $items;
 }
 
-
 /**
  * Implementation of hook_block().
  */
@@ -160,6 +175,11 @@ function fbconnect_redirect_submit($form
  * Impletementation of hook_form_alter.
  */
 function fbconnect_form_alter(&$form, $form_state, $form_id) {  
+//  if (in_array($form_id, array('user_profile_form', 'user_edit_form', 'user_register'))) {
+  if (isset($form['account'])) {
+    $form['account']['mail']['#maxlength'] = 320;
+  }
+
   if ($form_id == 'user_login' && $_GET['destination'] == 'fbconnect') {
     unset($_REQUEST['destination']);
     
@@ -170,8 +190,8 @@ function fbconnect_form_alter(&$form, $f
   // Render fbconnect on login block.
   if ($form_id == 'user_login_block' || $form_id == 'user_login') {
     if ($_GET['destination'] != 'fbconnect') {
-      list($size, $length) = explode('_', variable_get('fbconnect_button', NULL));    
-      $items[] = array('data' => t('Sign in using Facebook ').fbconnect_render_button($size, $length), 'class' => 'fbconnect-button');
+      
+      $items[] = array('data' => t('Sign in using Facebook ').fbconnect_render_button(), 'class' => 'fbconnect-button');
       $form['fbconnect_button'] = array(
         '#value' => theme('item_list', $items),
         '#weight' => 1,
@@ -236,9 +256,19 @@ function fbconnect_user_edit_submit($for
 /**
  * Render a custom button to log in via Facebook.
  */
-function fbconnect_render_button($size = 'medium', $length = 'short') {
-  return '<fb:login-button onlogin="facebook_onlogin_ready();" size="'. $size .'" background="dark" length="'.$length.'">
-          </fb:login-button>';
+function fbconnect_render_button($attr = array()) {
+  list($size, $length) = explode('_', variable_get('fbconnect_button', NULL));
+  $defaulr_attr = array(
+    'size' => $size, 
+    'length' => $length,
+    'onlogin' => 'facebook_onlogin_ready();',
+    'background' => 'dark',
+  ); 
+  
+  $attr = array_merge($defaulr_attr, $attr);
+  drupal_alter('fbconnect_login_button', $attr);
+
+  return '<fb:login-button '.drupal_attributes($attr).'></fb:login-button>';
 }
 
 /**
@@ -282,8 +312,18 @@ function facebook_client() {
  * @return array
  */
 function fbconnect_get_config() {
+  static $config;
+  
+  if (!$config) {
   $config['api_key'] = variable_get('fbconnect_key', NULL);
   $config['secret_api_key'] = variable_get('fbconnect_skey', NULL);
+	  $config['xd_path'] = 'fbconnect/receiver';
+	  $config['debug'] = variable_get('fbconnect_debug', false);
+	  $config['auto_login_logout'] = variable_get('fbconnect_auto_login_logout', false);
+	
+	  // allow third party modules to change settings
+	  drupal_alter('fbconnect_config', $config);	
+  }
   
   if ($config['api_key'] && $config['secret_api_key']) {
     return $config;
@@ -320,24 +360,22 @@ function fbconnect_render_js() {
   global $base_url;
   $module_path = drupal_get_path('module', 'fbconnect');
   $escaped_urL = drupal_to_js($base_url);
+  $config = fbconnect_get_config();
    
-  drupal_add_js('function facebook_onlogin_ready() {$("#fbconnect-autoconnect-form").submit();}', 'inline', 'footer');
-
-  if ($param = fbconnect_get_config()) {
-    $xd_path = drupal_to_js(base_path().$module_path .'/xd_receiver.html');
-    drupal_add_js('FB.Bootstrap.requireFeatures(["Connect","XFBML"], function() {
-      FB.Facebook.init("'. $param['api_key'] .'", '. $xd_path .'); 
-    });','inline', 'footer');
+  if ($param = $config) {
+    unset($param['secret_api_key']);
+    drupal_add_js(array('fbconnect'=> $param), 'setting');
+    drupal_add_js($module_path. '/fbconnect.js');
   }
 
 
-  if (fbconnect_get_fbuid() && $_GET['fblogout']) {
-    drupal_add_js('$(document).ready(function() {
-      FB.Bootstrap.requireFeatures(["Connect"], function() {
-        FB.Connect.logoutAndRedirect('. $escaped_urL .');
-      });
-    });', 'inline', 'footer');
-  }
+  //  if ($config['auto_login_logout'] && fbconnect_get_fbuid() && $_GET['fblogout']) {
+  //    drupal_add_js('$(document).ready(function() {
+  //      FB.Bootstrap.requireFeatures(["Connect"], function() {
+  //        FB.Connect.logoutAndRedirect('. $escaped_urL .');
+  //      });
+  //    });', 'inline', 'footer');
+  //  }
   
   if ($_SESSION['fbconnect_feed']) {
     $site = drupal_to_js(variable_get('site_name', $base_url));
@@ -364,11 +402,13 @@ function fbconnect_render_js() {
       break;
     }
     
+    if ($config['auto_login_logout']) {
     drupal_add_js('$(document).ready(function() {
       FB.Bootstrap.requireFeatures(["Connect"], function() { ' .$feed_js. '
           FB.Connect.showFeedDialog('. $bundle_id .', template_data, null, body);
       });
     });', 'inline', 'footer');
+    }
     
     unset($_SESSION['fbconnect_feed']);
   }
@@ -378,7 +418,10 @@ function fbconnect_render_js() {
  * Implementation of hook_footer().
  */
 function fbconnect_footer() {
-  $url = 'http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php';
+  $url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')
+  ? "https://ssl.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php"
+  : "http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" ;
+
   $footer = '<script type="text/javascript" src="'. $url .'"></script>';
   // Display the autoconnect form.
   $footer .= drupal_get_form('fbconnect_autoconnect_form');
@@ -389,27 +432,72 @@ function fbconnect_footer() {
  * This form is submitted by javascript when facebook session is detected.
  */
 function fbconnect_autoconnect_form() {
-  $form['#validate'] = array('fbconnect_autoconnect_form_validate');
-  $form['fbconnect'] = array('#type' => 'hidden');
+  $form['connect'] = array(
+    '#type' => 'submit',
+    '#value' => 'Connect',
+    '#prefix' => '<!--',
+    '#suffix' => '-->',
+  );
+
   return $form;
 }
 
-function fbconnect_autoconnect_form_validate($form, &$form_state) {
+function fbconnect_autoconnect_form_submit($form, &$form_state) {
   if (!user_is_logged_in()) {   
     $fbuid = fbconnect_get_fbuid();
     if ($fbuid) {  
       $uid = _is_fbconnect_user($fbuid);
+      $form_state['values']['uid']   = $uid;
+      $form_state['values']['fbuid'] = $fbuid;
     
       if (!$uid) {
-        drupal_goto('fbconnect/register/prompt');
+        $form_state['redirect'] = 'fbconnect/register/prompt';
       }
       elseif ($uid) {
        $user = user_load($uid);
        user_external_login($user);
-       drupal_goto($_GET['q']);
+        $form_state['redirect'] = ($_GET['q'] == 'user/login' || $_GET['q'] == 'user/register' || $_GET['q'] == 'user/password') ? 'user' : $_GET['q'];
       }
     }
   }  
+  else {
+    fbconnect_redirect_submit($form, $form_state);
+  }
+}
+
+/**
+ * Implementation of hook_exit();
+ */
+function fbconnect_exit($destination = null) {
+  global $fbconnect_logout_on_exit;
+  if (!$fbconnect_logout_on_exit) return;
+  
+  // off trigger to prevent repeated hook_exit calling
+  $fbconnect_logout_on_exit = false;
+  
+  if (fbconnect_get_fbuid()) {
+    $conf = fbconnect_get_config();
+    $session_key = facebook_client()->api_client->session_key;
+     
+    if ($conf && $conf['auto_login_logout'] && $session_key) {
+      // Figure out where to send the user.
+      if ($destination) {
+        $next = url($destination, array('absolute' => TRUE));
+      }
+      elseif ($_REQUEST['destination']) {
+        $next = url($_REQUEST['destination'], array('absolute' => TRUE));
+        // It pains me to muck with $_REQUEST['destination'], but facebook's weak-ass API leaves us little choice.
+        unset($_REQUEST['destination']);
+      }
+      else {
+        $next = url('<front>', array('absolute' => TRUE));
+      }
+
+      // http://forum.developers.facebook.com/viewtopic.php?id=21879
+      // Use next parameter to expire session.
+      drupal_goto("http://www.facebook.com/logout.php?app_key={$conf[api_key]}&session_key={$session_key}&next=" . $next);
+    }
+  }
 }
 
 /**
@@ -417,12 +505,24 @@ function fbconnect_autoconnect_form_vali
  */
 function fbconnect_user($op, &$edit, &$user, $category = NULL) {
   switch ($op) {
+//    case 'load':
+//      $info = db_fetch_object(db_query("SELECT * FROM {fbconnect_users} WHERE uid = %d", $user->uid));
+//      if ($info) {
+//        $user->fbuid         = $info->fbuid;
+//        $user->proxied_email = $info->proxied_email;
+//        if (!$user->mail) {
+//          $user->mail = $user->proxied_email;
+//        }
+//      }
+//      break;
     case 'delete':
       db_query('DELETE FROM {fbconnect_users} WHERE uid = %d', $user->uid);
     break;
     case 'logout':
-       if (fbconnect_get_fbuid()) {
-         drupal_goto(NULL, 'fblogout=1');
+      $conf = fbconnect_get_config();
+      if ($conf['auto_login_logout']) {
+        global $fbconnect_logout_on_exit;
+        $fbconnect_logout_on_exit = true;
        }
      break;
   }
@@ -453,54 +553,16 @@ function fbconnect_register($uid, $fbuid
 }
 
 /**
- * Get user picture from Facebook.
+ * Remove user from table fbconnect
+ * 
  * @param Int $fbuid
+ *   Facebook user id
+ * @return A
  */
-function fbconnect_get_fb_avatar($fbuid) {
-  $size = 'pic_with_logo';
-  // Get facebook user picture's url.
-  $pic_url = fbconnect_get_info_from_fb($fbuid, $size);
-  if ($pic_url) {
-    $result = drupal_http_request($pic_url[$size]);
-  
-    if ($result->code != 200) {
-      watchdog('fbconnect', 'Failed importing facebook user avatar for @fbuid, code : @code', 
-      array('@fbuid' => $fbuid, '@code' => $result->code));
-      return FALSE;
-    }  
-    
-    $filename = 'picture-fb_'. $fbuid .'.jpg';
-    $dest = file_directory_path();  
-    if (variable_get('user_picture_path', '')) {
-      $dest .= '/'. variable_get('user_picture_path', '');
-    }
-    $dest .= '/'. $filename;
-    
-    $file = file_save_data($result->data, $dest, FILE_EXISTS_REPLACE);
-    // Resizing
-    list($width, $height) = sscanf(variable_get('user_picture_dimensions', '85x85'), '%dx%d');
-    image_scale_and_crop($file, $dest, $width, $height);
-
-    return $file;
-  }
-}
-
-/**
- * Impletementation of hook_cron
- */
-function fbconnect_cron() {
-  if (variable_get('user_pictures', 0)) {
-    $expire = time() - (24 * 3600);  
-    $result = db_query('SELECT fbuid FROM {fbconnect_users} WHERE timestamp < %d', $expire);
-    
-    while ($tab = db_fetch_array($result)) {
-      fbconnect_get_fb_avatar($tab['fbuid']);
-      db_query('UPDATE {fbconnect_users} SET timestamp = %d', time());
-    }
-  }
+function fbconnect_unregister($fbuid) {
+  return db_query('DELETE FROM {fbconnect_users} WHERE fbuid = %d', $fbuid);
 }
 
-
 /**
  * Query information from facebook user table.
  *
@@ -516,3 +578,69 @@ function fbconnect_get_info_from_fb($fbu
     }
   }
 }
\ No newline at end of file
+
+/**
+ * Implementation of hook_theme_registry_alter().
+ *
+ * Override theme functions for things that can be displayed using
+ * XFBML.  Currently overriding user_picture.  We rename
+ * the original entries, as we will use them for users without
+ * javascript enabled.
+ *
+ */
+function fbconnect_theme_registry_alter(&$theme_registry) {
+  //  // Re-register the original theme function under a new name.
+  //  $theme_registry['fbconnect_username_orig'] = $theme_registry['username'];
+  //  // Override theme username
+  //  $theme_registry['username'] = array(
+  //    'arguments' => array('object' => NULL),
+  //    'function' => 'fbconnect_theme_username_override',
+  //  );
+
+  // Re-register the original theme function under a new name.
+  $theme_registry['fbconnect_user_picture_orig'] = $theme_registry['user_picture'];
+  // Override theme username
+  $theme_registry['user_picture'] = array(
+    'arguments' => array('account' => NULL),
+    'function' => 'fbconnect_theme_user_picture_override',
+  );
+
+}
+
+/**
+ * Our replacement for theme('user_picture', ...)
+ */
+function fbconnect_theme_user_picture_override($account) {
+  // First learn the Facebook id
+  $fbuid = db_result(db_query("SELECT fbuid FROM {fbconnect_users} WHERE uid = %d", $account->uid));
+  $user_url = url('user/' . $account->uid);
+
+  if ($fbuid && $account->fb_avatar) {
+    $output = <<<PIC
+      <div class="picture">
+        <a href="$user_url">
+         <fb:profile-pic uid='$fbuid' linked=false></fb:profile-pic>
+        </a>
+      </div>
+PIC;
+  } else {  
+    $output = theme('fbconnect_user_picture_orig', $account); // Markup without fb_connect.
+  }
+
+  return $output;
+
+}
+
+/**
+ * The post-remove callback for facebook. Unlinks facebook account
+ * from the linked drupal one.
+ */
+function fbconnect_post_remove_callback() {
+  if ($fb = facebook_client()) {
+    $fbuid = $fb->get_loggedin_user();
+    if ($fbuid && $fb->fb_params['uninstall'] == 1) {
+      //The user has removed your app
+      fbconnect_unregister($fbuid);
+    }
+  }
+}
\ No newline at end of file
diff -upbN fbconnect/fbconnect.pages.inc fbconnect/fbconnect.pages.inc
--- fbconnect/fbconnect.pages.inc	2009-06-03 17:51:06.000000000 +0400
+++ fbconnect/fbconnect.pages.inc	2009-11-26 05:31:51.000000000 +0300
@@ -6,20 +6,29 @@
  * Called when user perform facebook registration
  */
 function fbconnect_prompt_page() {
+  $links = fbconnect_prompt_page_links();
+  $output = array();
+  foreach ($links as $link) {
+    array_push($output, theme('box', $link));
+  }
+
+  return join("\n", $output);   
+}
+
+function fbconnect_prompt_page_links() {
   $reg_msg  = t('Click here to create a new account with Facebook connect');
   $link_msg = t('Click here to connect your existing account with Facebook');  
   
   $reg_choice  = l($reg_msg, 'fbconnect/register/create');
   $link_choice = l($link_msg, 'user', array('query' => 'destination=fbconnect'));
-
-  return theme('box', $reg_choice).theme('box', $link_choice);   
+  return array($reg_choice, $link_choice);
 }
 
 /**
  * Menu callback.
  * Called when user perform facebook registration
  */
-function fbconnect_register_page() {
+function fbconnect_register_page($form_state = array()) {
   $site  = variable_get('site_name', t('this website'));
   $fbuid = fbconnect_get_fbuid();
   $data  = fbconnect_get_info_from_fb($fbuid, 'name, proxied_email');    
@@ -38,7 +47,7 @@ function fbconnect_register_page() {
     $form['fb_avatar'] = array(
       '#type' => 'checkbox',
       '#title' => t('Use my Facebook picture as user picture'),
-      '#description' => t('Your picture will be updated every 24 hours.'),
+      '#description' => t('Your picture will be loaded from facebook.'),
       '#default_value' => 1,
     );  
   }
@@ -84,13 +93,7 @@ function fbconnect_register_form_submit(
    
    fbconnect_register($user->uid, $fbuid);
    
-   // Save facebook picture as avatar.
-   if ($form_state['values']['fb_avatar'] && variable_get('user_pictures', 0)) {     
-     if ($picture = fbconnect_get_fb_avatar($fbuid)) {
-       $data['picture']   = $picture;
-       $data['fb_avatar'] = 1;
-     }
-   }
+   $data['fb_avatar']  = $form_state['values']['fb_avatar'];
    $data['fb_visible'] = $form_state['values']['fb_visible'];
    $data['fbuid']      = $fbuid;
    
@@ -109,8 +112,16 @@ function fbconnect_user_identities($user
   if ($user->uid == $uid) { 
     return drupal_get_form('fbconnect_user_settings_form');
   }
+  elseif ($uid && $user->uid != $uid) {
+    return t(
+      "Actual Facebook account is already connected with another account on your site.\n"
+     ."Before connect your new account to Facebook account you should unconnect old account."
+    );
+  }
   else {
-    return t('Your account is not linked with a Facebook account or you are not authorized to edit the Facebook connect settings of this account.');
+    return '<div> Click here to connect your Facebook account </div><div class="fbconnect-button">'
+         . fbconnect_render_button()
+         . '</div>';
   } 
 }
 
@@ -137,11 +148,24 @@ function fbconnect_user_settings_form() 
     $form['fb_avatar'] = array(
       '#type' => 'checkbox',
       '#title' => t('Use my Facebook picture as user picture'),
-      '#description' => t('Your picture will be updated every 24 hours.'),
       '#default_value' => $user->fb_avatar,
     );
   
   }
+  
+  $form['operations'] = array(
+    '#title' => t('Unconnect Facebook account'),
+    '#description' => 'Click here if you want unconnect Facebook account',
+    '#type' => 'fieldset',
+    '#collapsed' => true,
+    '#collapsible' => true,
+  );
+  
+  $form['operations']['unlink'] = array(
+    '#type' => 'submit',
+    '#value' => t('Unconnect'),
+  ); 
+  
   $form['submit'] = array(
     '#type' => 'submit',
     '#value' => t('Save settings'),
@@ -157,12 +181,15 @@ function fbconnect_user_settings_form_su
   $fbuid   = fbconnect_get_fbuid(); 
   $visible = $form_state['values']['visibility'];
   $avatar  = $form_state['values']['fb_avatar'];
+  $op      = $form_state['values']['op'];
 
   $data['fb_visible'] = $form_state['values']['fb_visible'];
   $data['fb_avatar']  = $form_state['values']['fb_avatar'];
   
-  if ($data['fb_avatar'] && ($user->fbuid == $fbuid)) {
-    db_query("UPDATE {users} SET picture = '%s' WHERE uid = %d", fbconnect_get_fb_avatar($fbuid), $user->uid);    
+  if ($op == t('Unconnect')) {
+    fbconnect_unregister($fbuid);
+    $data['fb_visible'] = null;
+    $data['fb_avatar'] = null;
   }
   
   if ($data) user_save($user, $data);
diff -upbN fbconnect/xd_receiver.html fbconnect/xd_receiver.html
--- fbconnect/xd_receiver.html	2009-01-09 12:19:18.000000000 +0300
+++ fbconnect/xd_receiver.html	1970-01-01 03:00:00.000000000 +0300
@@ -1,6 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" > 
- <head> <title>Cross-Domain Receiver Page</title> </head> 
- <body> 
- <script src="http://static.ak.facebook.com/js/api_lib/v0.4/XdCommReceiver.debug.js" type="text/javascript"></script> 
- </body> </html>
\ No newline at end of file
diff -upbN fbconnect/xd_receiver.inc fbconnect/xd_receiver.inc
--- fbconnect/xd_receiver.inc	1970-01-01 03:00:00.000000000 +0300
+++ fbconnect/xd_receiver.inc	2009-11-20 17:06:23.000000000 +0300
@@ -0,0 +1,35 @@
+<?php 
+
+/**
+ * Without a receiver file, cross-domain javascript will not work.
+ *
+ * In their infinite wisdom, facebook has decreed that the URL for
+ * this static page be in the same place as the app's callback URL.
+ * So we have to make a Drupal callback for what would otherwise be a
+ * simple file.
+ *
+ * Supports SSL, http://wiki.developers.facebook.com/index.php/Facebook_Connect_Via_SSL.
+ */
+function fbconnect_receiver() {
+	$conf = fbconnect_get_config();
+  $src_suffix = $conf['debug']
+					    ? '.debug'
+					    : '' ;
+  
+  $src = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') 
+    ? "https://ssl.connect.facebook.com/js/api_lib/v0.4/XdCommReceiver$src_suffix.js" 
+    : "http://static.ak.connect.facebook.com/js/api_lib/v0.4/XdCommReceiver$src_suffix.js" ;
+  
+  $output = <<<HTML
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" >
+<body>
+  <!-- Drupal for Facebook cross-domain receiver. -->
+  <!-- http://wiki.developers.facebook.com/index.php/Cross_Domain_Communication_Channel -->
+  <script src="$src" type="text/javascript"></script>
+</body>
+</html>
+HTML;
+  print $output;
+  die(); // prevent Drupal from writing anything else.
+}
\ No newline at end of file
