diff --git a/CHANGELOG.txt b/CHANGELOG.txt
old mode 100644
new mode 100755
diff --git a/LICENSE.txt b/LICENSE.txt
old mode 100644
new mode 100755
diff --git a/README.txt b/README.txt
old mode 100644
new mode 100755
index 01cde4a..a076f62
--- a/README.txt
+++ b/README.txt
@@ -24,18 +24,19 @@ Also this tab allows to unlink your Facebook account.
 
 -- REQUIREMENTS --
 PHP 5.2 or higher versions. 
-Drupal 6.x. 
-Facebook PHP Library: http://github.com/facebook/php-sdk/
+Drupal 7.x. 
+Facebook PHP Library: http://github.com/facebook/php-sdk/  use version 2 as version 3 is not supported.
 Facebook API key: http://www.facebook.com/developers/
 
 
 -- INSTALLATION --
   1. Upload the 'fbconnect' folder into your module directory and activate the module,
   
-  2. Upload facebook-php-sdk (http://github.com/facebook/php-sdk/) into fbconnect module folder (or into libraries folder, 
+  2. Upload facebook-php-sdk/src/facebook.php (http://github.com/facebook/php-sdk/) into fbconnect module folder (or into libraries folder, 
     if you want to use it with the libraries module, http://drupal.org/project/libraries)
     
-     To ensure that you installed correctly check file : <fbconnect folder>/facebook-php-sdk/src/facebook.php must be accessible
+     To ensure that you installed correctly check file : <fbconnect folder>/facebook.php must be accessible
+     OR /sites/all/libraries/facebook/facebook.php must be accessible
      
  -- OR -- 
   Use drush_make script instead of steps 1 and 2:
diff --git a/fbconnect.admin.inc b/fbconnect.admin.inc
old mode 100644
new mode 100755
index 91f39ee..38f43ac
--- a/fbconnect.admin.inc
+++ b/fbconnect.admin.inc
@@ -12,7 +12,7 @@ function fbconnect_api_keys_settings($form, &$form_state) {
     '#required' => TRUE,
     '#title' => t('Application ID'),
     '#default_value' => variable_get('fbconnect_appid', NULL),
-    '#description' => t('Your Application ID, <strong> not API Key </strong>'),
+    '#description' => t('Also called the <em>OAuth client_id</em> value on Facebook App settings pages. <a href="https://www.facebook.com/developers/createapp.php">Facebook Apps must first be created</a> before they can be added here.'),
   );
 
   $form['fbconnect_skey'] = array(
@@ -20,7 +20,7 @@ function fbconnect_api_keys_settings($form, &$form_state) {
     '#required' => TRUE,
     '#title' => t('Application Secret'),
     '#default_value' => variable_get('fbconnect_skey', NULL),
-    '#description' => t('Do not share your secret key with anyone'),
+    '#description' => t('Also called the <em>OAuth client_secret</em> value on Facebook App settings pages.'),
   );
 
   $form['fbconnect_language_code'] = array(
@@ -56,7 +56,7 @@ function fbconnect_api_keys_settings($form, &$form_state) {
   );
 
   $form['fbconnect_connect_url'] = array(
-    '#type' => 'item',
+    '#type' => 'textfield',
     '#title' => t('Connect url'),
     '#description' => t('Copy this value into Facebook Applications on Connect settings tab'),
     '#value' => variable_get('fbconnect_connect_url', $GLOBALS['base_url'] . '/'),
@@ -220,18 +220,17 @@ function fbconnect_appearance_settings($form, &$form_state) {
 function fbconnect_fbapp_settings($form, &$form_state) {
   if (!facebook_client()) {
     drupal_set_message(t('Ensure that you entered valid api keys.'), 'error');
-    drupal_goto('admin/config/fbconnect');
+    drupal_goto('admin/config/people/fbconnect');
   }
-
+  $fbuid = fbconnect_get_fbuid();
   if (!fbconnect_get_fbuid()) {
     $button_attr = array('text' => t('Connect with your Facebook account'));
     $form['fbconnect'] = array(
-      '#value' => fbconnect_render_button($button_attr),
+      '#prefix' => "<div>". t('You are not connected to the facebook account that this application is registered to') . "</div>",      
+      '#markup' => fbconnect_render_button($button_attr),
     );
-
     return $form;
   }
-
   $form['fbconnect_base_domaine'] = array(
     '#type' => 'textfield',
     '#title' => t('Base Domain Url'),
diff --git a/fbconnect.css b/fbconnect.css
old mode 100644
new mode 100755
diff --git a/fbconnect.info b/fbconnect.info
old mode 100644
new mode 100755
index 1e3a2f6..4d44837
--- a/fbconnect.info
+++ b/fbconnect.info
@@ -3,3 +3,10 @@ name = Fbconnect
 description = Allows users to connect with Facebook account
 package = "Authentication"
 core = 7.x
+
+; Information added by drupal.org packaging script on 2011-03-17
+version = "7.x-2.x-dev"
+core = "7.x"
+project = "fbconnect"
+datestamp = "1300320371"
+
diff --git a/fbconnect.install b/fbconnect.install
old mode 100644
new mode 100755
index b62fb83..1f4e442
--- a/fbconnect.install
+++ b/fbconnect.install
@@ -60,7 +60,7 @@ function fbconnect_requirements($phase) {
         'title' => $t('FBConnect'),
         'value' => $t('Not configured'),
         'severity' => REQUIREMENT_WARNING,
-        'description' => $t('Please ensure that you entered Application ID and Secret Key. !link', array('!link' => l($t('Settings page'), 'admin/config/fbconnect'))),
+        'description' => $t('Please ensure that you entered Application ID and Secret Key. !link', array('!link' => l($t('Settings page'), 'admin/config/people/fbconnect'))),
       );
     }
   }
@@ -209,5 +209,5 @@ function _fbconnect_change_user_mail_field($action = 'install') {
     fbconnect_schema_alter($schema);
   }
   db_change_field('users', 'mail', 'mail', $schema['users']['fields']['mail']);
-  return $res;
+  //return $res;
 }
diff --git a/fbconnect.js b/fbconnect.js
old mode 100644
new mode 100755
index 4067954..b441f2c
--- a/fbconnect.js
+++ b/fbconnect.js
@@ -9,7 +9,7 @@ Drupal.fbconnect.init = function () {
     Drupal.fbconnect.initLogoutLinks(context);
   }
   
-  if (Drupal.settings.fbconnect.loginout_mode == 'auto') {
+  if (Drupal.settings.fbconnect.loginout_mode == 'auto') { 
     FB.Event.subscribe('auth.sessionChange', Drupal.fbconnect.reload_ifUserConnected);
 //    FB.Event.subscribe('auth.login', function(response) {
 //      console.log('event auth.login');
@@ -20,10 +20,9 @@ Drupal.fbconnect.init = function () {
 }
 
 Drupal.fbconnect.logout = function(keep_fbaccount_logged) {
-  var logout_url = Drupal.settings.basePath + 'logout'; 
-  
-  if (!keep_fbaccount_logged) {
-    FB.logout(function(response) { 
+  var logout_url = Drupal.settings.basePath + 'user/logout'; 
+  if (!keep_fbaccount_logged) {   
+    FB.logout(function(response) {
       window.location.href = logout_url; 
     });
   }
@@ -45,15 +44,16 @@ Drupal.fbconnect.initLogoutLinks = function(context) {
   var loginout_mode = Drupal.settings.fbconnect.loginout_mode;
   var user          = Drupal.settings.fbconnect.user;
   var basePath      = Drupal.settings.basePath;
-  var logout_url    = basePath + 'logout'; 
-  var links         = $('a[href='+ logout_url +']', context).not('.logout_link_inited');
-  
+  var logout_url    = basePath + 'user/logout'; 
+  var links         = jQuery('a[href='+ logout_url +']', context).not('.logout_link_inited');
+
   if (loginout_mode == 'manual') return;
   
-  links.addClass('logout_link_inited').click(function() {
+  links.addClass('logout_link_inited').bind('click',function() {
     var fbuid = FB.getSession() && FB.getSession().uid;
+ 
     if (!user.fbuid || user.fbuid != fbuid) return;
-    if (loginout_mode == 'auto') { 
+    if (loginout_mode == 'auto') {
       Drupal.fbconnect.logout();
     }
     else if (loginout_mode == 'ask') {    
@@ -100,7 +100,7 @@ function facebook_onlogin_ready() {
   if (!FB.getSession()) {
     return;
   }
-  $("#fbconnect-autoconnect-form").submit();
+  jQuery("#fbconnect-autoconnect-form").submit();
 }
 
 /**
@@ -151,8 +151,9 @@ Drupal.fbconnect.PopupDialog.prototype.callback = function(event, data) {
     break;
     
   case 'close':
-    var btn = this.findButton('cancel'); 
-    if (btn) this.callback('click', btn);
+    this.close();
+    /*var btn = this.findButton('cancel'); 
+    if (btn) this.callback('click', btn); */
     break;
     
   case 'load':
@@ -169,7 +170,7 @@ Drupal.fbconnect.PopupDialog.prototype.prepareDefaults = function(options) {
     'showLoading'     : false,
     'hideUntilLoaded' : false
   };  
-  $.extend(this.options, defaults, options);
+  jQuery.extend(this.options, defaults, options);
   
   this.__close_handler = this.createHandler('close', {});
   this.options.dialog = this; 
@@ -204,10 +205,10 @@ Drupal.fbconnect.PopupDialog.prototype.findButton = function(name) {
 
 Drupal.theme.prototype.fb_popup_dialog_buttons = function(buttons, dialog) {
   buttons = buttons || {};
-  var container = $('<div class="dialog_buttons"></div>');
-  
+  var container = jQuery('<div class="dialog_buttons"></div>');
+
   jQuery.each(buttons, function(i, btn) {
-    var button = $('<input type="button" class="dialog_inputbutton">');
+    var button = jQuery('<input type="button" class="dialog_inputbutton">');
     if (!btn['name']) btn['name'] = i;
     if (btn.attr) button.attr(btn.attr);
     if (btn['class']) button.addClass(btn['class']);
@@ -234,7 +235,7 @@ Drupal.theme.prototype.fb_popup_dialog = function(options) {
     '</div>'
   ];
   
-  $(elements.join("\n")).each(function() {
+  jQuery(elements.join("\n")).each(function() {
     container.appendChild(this);
   });
   if (options.buttons) {
@@ -254,7 +255,7 @@ Drupal.theme.prototype.fbml_name = function(fbuid, options) {
     'linked' : false
   };
   
-  options = $.extend({}, defaults, options);
+  options = jQuery.extend({}, defaults, options);
   
   output.push('" useyou="'+ (!!options.useyou ? 'true' : 'false') +'"');
   output.push('" linked="'+ (!!options.linked ? 'true' : 'false') +'"');
diff --git a/fbconnect.module b/fbconnect.module
old mode 100644
new mode 100755
index 467b9cf..1e80712
--- a/fbconnect.module
+++ b/fbconnect.module
@@ -5,21 +5,22 @@
  * Implements hook_menu().
  */
 function fbconnect_menu() {
-  $items['admin/config/fbconnect'] = array(
+  $items['admin/config/people/fbconnect'] = array(
     'title' => 'Facebook Connect',
+    'description' => 'Configure required settings for Facebook integration',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('fbconnect_api_keys_settings'),
     'access arguments' => array('administer site configuration'),
     'file' => 'fbconnect.admin.inc',
   );
 
-  $items['admin/config/fbconnect/api-keys'] = array(
+  $items['admin/config/people/fbconnect/api-keys'] = array(
     'title' => 'Api Keys',
     'type' => MENU_DEFAULT_LOCAL_TASK,
     'file' => 'fbconnect.admin.inc',
   );
 
-  $items['admin/config/fbconnect/apperance'] = array(
+  $items['admin/config/people/fbconnect/apperance'] = array(
     'title' => 'Appearance',
     'type' => MENU_LOCAL_TASK,
     'page callback' => 'drupal_get_form',
@@ -28,7 +29,7 @@ function fbconnect_menu() {
     'file' => 'fbconnect.admin.inc',
   );
 
-  $items['admin/config/fbconnect/fb-app'] = array(
+  $items['admin/config/people/fbconnect/fb-app'] = array(
     'title' => 'App Settings',
     'type' => MENU_LOCAL_TASK,
     'page callback' => 'drupal_get_form',
@@ -50,10 +51,10 @@ function fbconnect_menu() {
 //  );
 
   $items['fbconnect/register/prompt'] = array(
-    'title' => 'Facebook connect',
+    'title' => t('Facebook connect'),
     'page callback' => 'fbconnect_prompt_page',
     'access callback' => 'user_is_anonymous',
-    'type' => MENU_CALLBACK,
+    'type' => MENU_NORMAL_ITEM,
     'file' => 'fbconnect.pages.inc',
   );
 
@@ -149,10 +150,17 @@ function fbconnect_init() {
  */
 function fbconnect_user_cancel($edit, $account, $method) {
   db_delete('fbconnect_users')
-    ->condition('uid', $user->uid)
+    ->condition('uid', $account->uid)
+    ->execute();
+}
+/**
+ * Implements hook_user_delete().
+ */
+function fbconnect_user_delete($account) {
+  db_delete('fbconnect_users')
+    ->condition('uid', $account->uid)
     ->execute();
 }
-
 /**
  * Implements hook_user_categories().
  */
@@ -199,7 +207,6 @@ function fbconnect_redirect_submit($form, &$form_state) {
  * Implements hook_form_alter().
  */
 function fbconnect_form_alter(&$form, $form_state, $form_id) {
-
   if (isset($form['account'])) {
     $form['account']['mail']['#maxlength'] = 320;
   }
@@ -210,6 +217,12 @@ function fbconnect_form_alter(&$form, $form_state, $form_id) {
         drupal_set_message(t('Please log in, in order to link your account with Facebook Connect'));
         $form['#submit'][] = 'fbconnect_redirect_submit';
       }
+      //check if there is a get paramater set(fbconnect=true)
+      if(isset($_GET['fbconnect']) && $_GET['fbconnect'] == 'true'){
+          //add a function callback to call after user is loged in
+          $form['#submit'][] = 'fbconnect_add_user_afterlogin_submit';
+      }
+      
       // don't break here since we want to run the next user_login_block case too
 
       // Render fbconnect on login block.
@@ -235,7 +248,7 @@ function fbconnect_form_alter(&$form, $form_state, $form_id) {
     case 'user_profile_form':
       if (variable_get('user_pictures', 0) && isset($form['_account'])) {
         $account = $form['_account']['#value'];
-        if ($account->fb_avatar && isset($form['picture'])) {
+        if ($account->data['fb_avatar'] && isset($form['picture'])) {
           $form['picture']['fb_avatar'] = array(
             '#value' => t('You are currently using your Facebook picture, if you delete or load a new picture, your facebook picture will no longer be updated.'),
             '#weight' => 0,
@@ -247,7 +260,26 @@ function fbconnect_form_alter(&$form, $form_state, $form_id) {
 
   endswitch;
 }
-
+/**
+* add the user to the fbconnect_users table if they logged in with facebook
+* @param mixed $form
+* @param mixed $form_state
+*/
+function fbconnect_add_user_afterlogin_submit($form, &$form_state){    
+    global $user;
+    $data  = fbconnect_get_user_info(array('name', 'email'));
+    //user not logged into facebook we dont have any information for them, let them log in again
+    if(empty($data)){
+        return facebook_login_page($form,$form_state);
+    }
+    $uid = _email_allready_exist($data['email']);
+    if($uid == $user->uid){
+        //add this user to the fbconnect_users table
+        fbconnect_register($user->uid, $data['id']);    
+    }else{
+        drupal_set_message('You have loged into the website with an account that does not have the same email as your facebook account');
+    }    
+}
 /**
  * If the user deletes the photo or load a new picture,
  * we deactivate the automatic import of the Facebook avatar.
@@ -294,21 +326,22 @@ function _facebook_client_search_paths() {
 }
 
 function _facebook_client_load_include() {
+  // TODO: add full Libraries API support.
   if (!class_exists('Facebook')) {
-    $lib_paths = _facebook_client_search_paths();
-
-    foreach ($lib_paths as $dir) {
-      $lib_paths[] = $dir . '/php';
-      $lib_paths[] = $dir . '/src';
-    }
-
-    foreach ($lib_paths as $path) {
-      $client_include = $path . '/facebook.php';
-      if (file_exists($client_include)) {
-        include_once DRUPAL_ROOT . '/' . $client_include;
+    $paths = array(
+      DRUPAL_ROOT . '/sites/all/libraries/facebook',
+      dirname(__FILE__),
+    );
+    foreach ($paths as $path) {
+      $path .= '/facebook.php';
+      if (file_exists($path)) {
+        include $path;
         break;
       }
     }
+    if (!class_exists('Facebook')) {
+      watchdog('fbconnect', t("Unable to load the required Facebook library, please check the README.txt for instructions on how to resolve this."));
+    }
   }
 
   return class_exists('Facebook') && defined('Facebook::VERSION');
@@ -392,14 +425,14 @@ function fbconnect_get_config() {
  * @return integer
  *   facebook user id
  */
-function fbconnect_get_fbuid($check_connected = FALSE) {
+function fbconnect_get_fbuid($check_connected = FALSE) {    
   global $user;
 
-  $client = facebook_client();
-
-  if ($client && $session = $client->getSession()) {
+  $client = facebook_client();  
+  $session = $client->getSession();
+  if ($client && $session != NULL) {
     $fbuid = $session['uid'];
-
+    
     if ($check_connected && $fbuid) {
       if (_get_user_fbuid($user->uid) != $fbuid) {
         $fbuid = NULL;
@@ -407,6 +440,8 @@ function fbconnect_get_fbuid($check_connected = FALSE) {
     }
 
     return $fbuid;
+  }else{
+    return 0;
   }
 }
 
@@ -416,7 +451,7 @@ function fbconnect_get_fbuid($check_connected = FALSE) {
 function fbconnect_render_js() {
   global $base_url;
   global $user;
-
+  $user->fbuid = _get_user_fbuid($user->uid);
   $module_path = drupal_get_path('module', 'fbconnect');
 
   if ($config = fbconnect_get_config()) {
@@ -433,36 +468,35 @@ function fbconnect_render_js() {
 }
 
 /**
- * Implements hook_footer().
+ * Implements hook_page_alter ().
  */
-function fbconnect_footer() {
+function fbconnect_page_alter(&$page) {
   if (facebook_client() && !_fbconnect_is_excluded_page($_GET['q'])) {
     $config = fbconnect_get_config();
-    $footer = array();
-    $footer['fb-init-code'] = <<<CODE
-      <div id="fb-root"></div>
-      <script type="text/javascript">
+    $page['page_bottom']['fb-init-code']= array(
+        '#type' => 'markup',
+        '#markup' => '<div id="fb-root"></div>
+        <script type=""text/javascript"">
         window.fbAsyncInit = function() {
           FB.init({
-            appId  : '$config[app_id]',
+            appId  : \'' . $config['app_id'] . '\',
             status : true, // check login status
             cookie : true, // enable cookies to allow the server to access the session
             xfbml  : true,
-            logging: '$config[debug]'
+            logging: \'' . $config['debug'] . '\'
           });
 
-          jQuery(document).trigger('fb:init');
+          jQuery(document).trigger(\'fb:init\');
         };
 
         (function() {
-          var e = document.createElement('script');
-          e.src = $config[connect_js];
+          var e = document.createElement(\'script\');
+          e.src = ' . $config['connect_js'] . ';
           e.async = true;
-          document.getElementById('fb-root').appendChild(e);
+          document.getElementById(\'fb-root\').appendChild(e);
         }());
-      </script>
-CODE;
-
+      </script>'
+    );
     // ugly hack to integrate with fb_social
     if (!empty($config['omit_fb_init_code'])) {
       unset($footer['fb-init-code']);
@@ -470,12 +504,17 @@ CODE;
 
     if (!fbconnect_get_fbuid(TRUE)) {
       // Display the autoconnect form.
-      $footer['autoconnect-form'] = '<div style="display: none">' . drupal_get_form('fbconnect_autoconnect_form') . '</div>';
+      $fbconnect_autoconnect_form = drupal_get_form('fbconnect_autoconnect_form');
+      $footer['autoconnect-form'] = '<div style="display: none">' . drupal_render($fbconnect_autoconnect_form) . '</div>';
     }
 
     //    drupal_alter('fbconnect_footer', $footer);
-
-    return join("\n", $footer);
+    if(isset($footer)){
+        $page['page_bottom']['fb_init_code'] = array(
+           '#type' => 'markup',
+           '#markup' => implode("\n", $footer),
+         );
+    }
   }
 }
 
@@ -506,7 +545,7 @@ function fbconnect_autoconnect_form_submit($form, &$form_state) {
     $error_msg = 'fbconnect_autoconnect_form_submit has been called without valid facebook session. '
                . 'Check your Application Secret on fbconnect settings page';
 
-    watchdog('fbconnect', $error_msg, array(), WATCHDOG_ALERT, l(t('configure'), 'admin/config/fbconnect'));
+    watchdog('fbconnect', $error_msg, array(), WATCHDOG_ALERT, l(t('configure'), 'admin/config/people/fbconnect'));
     return;
   }
 
@@ -529,7 +568,9 @@ function fbconnect_autoconnect_form_submit($form, &$form_state) {
       }
       elseif ($uid) {
         $account = user_load($uid);
-        user_external_login($account);
+        //user_external_login($account);
+        $form_state['uid'] = $uid;
+        user_login_submit(array(), $form_state);
         module_invoke('rules', 'invoke_event', 'fbconnect_user_login', $account);
       }
     }
@@ -548,12 +589,13 @@ function fbconnect_autoconnect_form_submit($form, &$form_state) {
  */
 function _is_fbconnect_user($fbuid) {
   if ($fbuid) {
-    $query = db_select('users', 'u')
-      ->fields('u', 'uid')
-      ->innerJoin('fbconnect_users', 'f', 'u.uid = f.uid')
-      ->condition('fbuid', $fbuid)
-      ->execute();
-    return $query->fetchField();
+    $query = db_select('fbconnect_users', 'fbu')
+        ->fields('fbu',array('uid'))
+        ->condition('fbuid', $fbuid,'=');
+    $user_registered = $query->execute()->fetchField();
+    if(!empty($user_registered)){
+        return $user_registered;
+    }
   }
 }
 
@@ -566,8 +608,11 @@ function _is_fbconnect_user($fbuid) {
  */
 function _get_user_fbuid($uid) {
   if ($uid) {
-    $query = "SELECT fbuid FROM {fbconnect_users} where uid = %d";
-    return db_query($query, $uid)->fetchField();
+    $query = db_select('fbconnect_users', 'fb_u');
+    $query->fields('fb_u',array('fbuid'));
+    $query->condition('uid', $uid, '=');  
+    //$query = "SELECT fbuid FROM {fbconnect_users} where uid = %d";
+    return $query->execute()->fetchField();
   }
 }
 
@@ -587,9 +632,8 @@ function fbconnect_register($uid, $fbuid) {
     return FALSE;
   }
 
-  $delete_query = 'DELETE FROM {fbconnect_users} WHERE uid = %d';
-  $insert_query = 'INSERT INTO {fbconnect_users} (uid, fbuid, timestamp) VALUES (%d, \'%s\', %d)';
-
+  //$delete_query = 'DELETE FROM {fbconnect_users} WHERE uid = %d';
+  //$insert_query = 'INSERT INTO {fbconnect_users} (uid, fbuid, timestamp) VALUES (%d, \'%s\', %d)';
   db_delete('fbconnect_users')
     ->condition('uid', $uid)
     ->execute();
@@ -726,7 +770,8 @@ function fbconnect_graph_query($path, $params = array()) {
     try {
       try {
         $args = func_get_args();
-        return call_user_func_array(array(facebook_client(), 'api'), $args);
+        $facebook_data = call_user_func_array(array(facebook_client(), 'api'), $args);
+        return $facebook_data;
       }
       catch (FacebookApiException $e) {
         drupal_set_message($e->getMessage(), 'error');
@@ -739,7 +784,16 @@ function fbconnect_graph_query($path, $params = array()) {
     }
   }
 }
-
+/**
+* Check the users table to see if the email is allready in the drupal system
+*  returns uid of user with the email
+*/
+function _email_allready_exist($email){
+    $query = db_select('users', 'u');
+    $query->fields('u',array('uid'));
+    $query->condition('mail', $email, '=');
+    return $query->execute()->fetchField();
+}
 
 /**
  * Query information from facebook user table.
@@ -796,6 +850,7 @@ function fbconnect_theme_registry_alter(&$theme_registry) {
       'arguments' => array('account' => NULL),
       'function' => 'fbconnect_theme_user_picture_override',
       'preprocess functions' => array(),
+      'theme path' => drupal_get_path('module','fbconnect'),
     );
   }
 }
@@ -803,7 +858,8 @@ function fbconnect_theme_registry_alter(&$theme_registry) {
 /**
  * Our replacement for theme('user_picture', ...)
  */
-function fbconnect_theme_user_picture_override($account) {
+function fbconnect_theme_user_picture_override($variables) {
+  $account = $variables['account'];
   // manually set in nodeapi or comments
   if (isset($account->fbconnect_picture_preset)) {
     $preset = $account->fbconnect_picture_preset;
@@ -814,15 +870,20 @@ function fbconnect_theme_user_picture_override($account) {
   }
 
   if (!isset($account->fbuid)) {
-    $user_data = unserialize(db_query('SELECT data FROM {users} WHERE uid = :uid', array(':uid' => $account->uid))->fetchField());
-    $account->fbuid     = @$user_data['fbuid'];
-    $account->fb_avatar = @$user_data['fb_avatar'];
+    $user_data = unserialize(db_query('SELECT data FROM {users} WHERE uid = :uid', array(':uid' => $account->uid))->fetchField());    
+    if(isset($user_data['fbuid'])){
+        $account->fbuid = @$user_data['fbuid'];
+    }else{
+        $account->fbuid = _get_user_fbuid($account->uid);
+    }
+    $account->data['fb_avatar'] = @$user_data['fb_avatar'];
   }
+
   // First learn the Facebook id
   $fbuid    = $account->fbuid;
   $user_url = url('user/' . $account->uid);
 
-  if ($fbuid && isset($account->fb_avatar) && $account->fb_avatar) {
+  if ($fbuid && isset($account->data['fb_avatar']) && $account->data['fb_avatar']) {
     $output = theme('fbconnect_user_picture_override', array('fbuid' => $fbuid, 'account' => $account, 'user_url' => $user_url, 'size' => variable_get('fbconnect_pic_size', $preset), 'logo' => variable_get('fbconnect_pic_logo', TRUE)));
   }
   else {
@@ -906,7 +967,7 @@ function theme_fbconnect_login_button($variables) {
 /**
  * Implementation of hook_menu_alter().
  */
-function fbconnect_menu_alter(&$callbacks) {
+function fbconnect_menu_alter(&$callbacks) { 
   $callbacks['user/%user_category/edit/fbconnect']['page callback'] = 'fbconnect_user_identities';
   $callbacks['user/%user_category/edit/fbconnect']['module'] = 'fbconnect';
   $callbacks['user/%user_category/edit/fbconnect']['file'] = 'fbconnect.pages.inc';
diff --git a/fbconnect.pages.inc b/fbconnect.pages.inc
old mode 100644
new mode 100755
index 13f64cf..cc87bd2
--- a/fbconnect.pages.inc
+++ b/fbconnect.pages.inc
@@ -11,10 +11,37 @@
  * Called when user perform facebook registration
  */
 function fbconnect_prompt_page() {
-  return theme('links', array('links' => fbconnect_prompt_page_links()));
+  $conf = fbconnect_get_config();
+  $params = array(
+    '!site_name' => $conf['invite_name'],
+  );
+  $reg_msg  = t('Log in to !site_name with Facebook account', $params);
+  $link_msg = t('Click here to connect your existing !site_name account with Facebook', $params);
+
+  $reg_attr = array();
+  if ($conf['fast_reg_mode']) {
+    $reg_attr = array('attributes' => array(
+      'onclick' => 'Drupal.fbconnect.DoFastRegistration(this); return false;'
+    ));
+  }
+  $items = array(/*'title' => 'Facebook Connection Options',*/'items' => array(l($reg_msg, 'fbconnect/register/create',$reg_attr),l($link_msg,'user',array('query' => array('destination' => 'fbconnect/link')))));
+  /*$links = array(
+    array(
+      'title' => $reg_msg,
+      'href' => 'fbconnect/register/create',
+      $reg_attr,
+    ),
+    array(
+      'title' => $link_msg,
+      'href' => 'user',
+      array('attributes' => array('query' => 'destination=fbconnect/link')),
+    ),
+  );*/
+
+  return /*theme('links', array('links' => fbconnect_prompt_page_links))*/theme('item_list',$items);
 }
 
-function fbconnect_prompt_page_links() {
+/*function fbconnect_prompt_page_links() {
   $conf = fbconnect_get_config();
   $params = array(
     '!site_name' => $conf['invite_name'],
@@ -38,12 +65,12 @@ function fbconnect_prompt_page_links() {
     array(
       'title' => $link_msg,
       'href' => 'user',
-      array('query' => 'destination=fbconnect/link'),
+      array('attributes' => array('query' => 'destination=fbconnect/link')),
     ),
   );
 
   return $links;
-}
+}*/
 
 /**
  * Menu callback.
@@ -51,8 +78,29 @@ function fbconnect_prompt_page_links() {
  */
 function fbconnect_register_page($form, $form_state = array()) {
   $conf  = fbconnect_get_config();
-  $data  = fbconnect_get_user_info('name, email');
-  $form  = drupal_retrieve_form('user_register', $form_state);
+  $data  = fbconnect_get_user_info(array('name', 'email'));
+  //user not logged into facebook we dont have any information for them, let them log in
+  if(empty($data)){
+      return facebook_login_page($form,$form_state);
+  }
+  //check if user has allready registered with thier facebook account
+  $user_id = _is_fbconnect_user($data['id']);
+  if($user_id){
+     //user allready registered facebook account to site, log them in
+     $form_state['uid'] = $user_id;     
+     user_login_submit($form, $form_state);
+  }elseif( _email_allready_exist($data['email']) ){
+     //the user was not found in the fbconnect_users table, but the emial from facebook might allready have an account on coracle.
+     //Redirect user to the login page with a message.
+     drupal_set_message('This email address is allready registered to an account. Please log in to this account with the username and password provided during registration');
+     $options = array('query' => array(
+        'fbconnect' => 'true'
+     ));
+     drupal_goto('user/login',$options);
+     return;
+  }
+  
+  $form  = drupal_retrieve_form('user_register_form', $form_state);
   $site  = $conf['invite_name'];
 
   // #758918 : prevent users registration if admin has blocked free registration
@@ -61,10 +109,11 @@ function fbconnect_register_page($form, $form_state = array()) {
     drupal_access_denied();
     return;
   }
-
-  drupal_prepare_form('user_register', $form, $form_state);
-
-  $form['name']['#default_value'] = $data['name'];
+  
+  $form['account']['name']['#value'] = $data['name'];
+  $form['account']['mail']['#parents'] = array('name');
+  $form['account']['mail']['#value'] = $data['email'];
+  $form['account']['mail']['#parents'] = array('mail');
 
   $form['fbconnect'] = array(
     '#type' => 'fieldset',
@@ -85,20 +134,35 @@ function fbconnect_register_page($form, $form_state = array()) {
       '#default_value' => 1,
     );
   }
-  $form_state['#redirect'] = isset($_GET['destination']) ? $_GET['destination'] : '';
-  $form['#submit'][] = 'fbconnect_register_form_submit';
+
+  $form_state['redirect'] = isset($_GET['destination']) ? $_GET['destination'] : '';
+  $form['#validate'][] = 'fbconnect_register_form_validate';
+  $form['#submit'][] = 'fbconnect_register_form_submit';  
 
   // Fast registration mode, we by pass std drupal reg form.
   if ($conf['fast_reg_mode']) {
     $newname = $conf['fast_reg_autoname']? _fbconnect_get_username($data['name']) : $data['name'];
+    //drupal_set_message("new name : " . $newname);
     $form_state['values']['name']       = $newname;
     $form_state['values']['mail']       = $data['email'];
     $form_state['values']['pass']       = user_password();
     $form_state['values']['status']     = 1;
     $form_state['values']['fb_visible'] = 1;
     $form_state['values']['fb_avatar']  = 1;
-
+    
+    //to make validation work have to unset some form fields and settings Taxonomy fields cause errors 
+    //TODO:: check this code sure it will need to check for required profile fields and fill them with data  
+    foreach($form as $key => $val){
+        //unset all cusom fields
+        if(strstr($key,'field_')){
+            //unset this custom field
+            unset($form[$key]);
+        }
+    }
+    $form_state['complete form'] = $form;
+     //taxonomy linked fields causes issues with this validate form function
     drupal_validate_form('user_register', $form, $form_state);
+    
     $success = !form_get_errors();
     if (!$success) {
       $dest = array('query' => 'destination=fbconnect/link');
@@ -119,9 +183,20 @@ function fbconnect_register_page($form, $form_state = array()) {
   return $form;
 }
 
+function facebook_login_page(&$form, $form_state = array()){
+    $facebook = facebook_client();
+    $login_url = $facebook->getLoginUrl();
+    $form['log_in_to_facebook'] = array(
+        '#title' => t('You are not logged in to facebook you would have to log in AND give the site access to use this feature'),
+        '#description' => t('Log in using your facebook account'),
+        '#type' => 'item',
+        '#markup' => l('Log in to Facebook',$login_url),
+    );
+    return $form;
+}
 function _fbconnect_get_username($name_suggestion) {
-  $query = "SELECT COUNT(name) FROM users WHERE name = '%s'";
-  $result = db_result(db_query($query, $name_suggestion));
+  $query = "SELECT COUNT(name) FROM users WHERE name = ':user_name'";
+  $result = db_query($query, array('user_name' => $name_suggestion))->fetchField();
   if ($result['name'] != 0) {
     $counter = variable_get('fbconnect_namecounter', 1);
     $newname = 'fbuser'.$counter;
@@ -132,11 +207,20 @@ function _fbconnect_get_username($name_suggestion) {
   }
   return $newname;
 }
-
+function fbconnect_register_form_validate($form, &$form_state){
+   $fbuid = fbconnect_get_fbuid();
+   //make sure we have a facebook user object by the time we get here otherwise drop form and exit with error message
+   if($fbuid == 0){
+     $msg = t("You are not logged into facebook log into facebook and try again.");
+     drupal_set_message($msg, 'error');
+     watchdog('fbconnect', $msg);
+     drupal_goto();
+   }
+}
 function fbconnect_register_form_submit($form, &$form_state) {
   $fbuid = fbconnect_get_fbuid();
   if (_is_fbconnect_user($fbuid)) {
-    $msg = t("Retrying registration. Your Facebook account already assigned to one account on our site");
+    $msg = t("Retrying registration. Your Facebook account is already assigned to one account on our site");
     drupal_set_message($msg, 'error');
     watchdog('fbconnect', 'Error creating account for @fbuid', array('@fbuid' => $fbuid), WATCHDOG_ALERT);
     drupal_goto();
@@ -148,7 +232,6 @@ function fbconnect_register_form_submit($form, &$form_state) {
   else {
     $user = $form_state['user'];
   }
-
   if (!$user) {
     drupal_set_message(t("Error saving user account."), 'error');
     drupal_goto();
@@ -158,21 +241,33 @@ function fbconnect_register_form_submit($form, &$form_state) {
   $data['fb_avatar']  = isset($form_state['values']['fb_avatar']) ? $form_state['values']['fb_avatar'] : 0;
   $data['fb_visible'] = $form_state['values']['fb_visible'];
 
-  user_save($user, $data);
-  user_external_login($user);
+  $user = user_save($user, $data);
+
+  $form_state['uid'] = $user->uid;
+  user_login_submit(array(), $form_state);
   $_SESSION['fbconnect_feed']['type'] = 'register';
 }
 
 /**
  * Menu callback fbconnect identities
  */
-function fbconnect_user_identities($account) {
+function fbconnect_user_identities($form_id,$account) {
   $conf  = fbconnect_get_config();
   $fbuid = fbconnect_get_fbuid();
-  $uid   = _is_fbconnect_user($fbuid);
-
   drupal_set_title(check_plain($account->name));
-
+  $uid = NULL;
+  if($fbuid){
+    $uid = _is_fbconnect_user($fbuid);
+    $account->fbuid = $fbuid;
+    if($uid){     
+       //user in fbconnect_users table
+       //continue      
+    }else{
+        //we have facebook access for this user but we dont have the user in the fbconnect_users table, add him
+        fbconnect_register($account->uid, $fbuid);
+        $uid  = _is_fbconnect_user($fbuid);        
+    }  
+  } 
   if (isset($account->fbuid) && $account->fbuid) {
     return drupal_get_form('fbconnect_user_settings_form', $account);
   }
@@ -198,7 +293,7 @@ function fbconnect_user_settings_form($form, $form_state, $account) {
     '#type' => 'checkbox',
     '#title' => t('Let my Facebook friends see me on @site', array('@site' => $conf['invite_name'])),
     '#description' => t('My Facebook friends will be able to see that I own an account on this website.'),
-    '#default_value' => isset($account->fb_visible) ? $account->fb_visible : NULL,
+    '#default_value' => isset($account->data['fb_visible']) ? $account->data['fb_visible'] : NULL,
   );
 
   // Whether user picture is enabled,
@@ -207,7 +302,8 @@ function fbconnect_user_settings_form($form, $form_state, $account) {
     $form['fb_avatar'] = array(
       '#type' => 'checkbox',
       '#title' => t('Use my Facebook picture as user picture'),
-      '#default_value' => isset($account->fb_avatar) ? $account->fb_avatar : NULL,
+      '#description' => t('Your picture will be loaded from facebook.'),
+      '#default_value' => isset($account->data['fb_avatar']) ? $account->data['fb_avatar'] : NULL,
     );
   }
 
@@ -257,18 +353,18 @@ function fbconnect_user_settings_form($form, $form_state, $account) {
  */
 function fbconnect_user_settings_form_submit($form, &$form_state) {
   $account = $form_state['values']['account'];
-  $visible = $form_state['values']['visibility'];
+  //$visible = $form_state['values']['visibility'];
   $avatar  = $form_state['values']['fb_avatar'];
   $op      = $form_state['values']['op'];
   $fbuid   = $account->fbuid;
 
-  $data['fb_visible'] = $form_state['values']['fb_visible'];
-  $data['fb_avatar']  = $form_state['values']['fb_avatar'];
+  $data['data']['fb_visible'] = $form_state['values']['fb_visible'];
+  $data['data']['fb_avatar']  = $form_state['values']['fb_avatar'];
 
   if ($op == $form['fbconnect']['unlink']['#value']) {
     if (fbconnect_unregister($fbuid)) {
-      $data['fb_visible'] = NULL;
-      $data['fb_avatar'] = NULL;
+      $data['data']['fb_visible'] = NULL;
+      $data['data']['fb_avatar'] = NULL;
     }
     else {
       form_set_error('fbconnect', t("Something went wrong. Can't unlink account"));
@@ -291,9 +387,9 @@ function theme_fbconnect_user_picture_override($variables) {
   $size = $variables['size'];
   $logo = $variables['logo'];
   $protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
-  $path = $protocol . '://graph.facebook.com/' . $fbuid . ' /picture?type=' . $size;
-  $image = theme('image', array('path' => $path));
-  return '<div class="picture">' . l($image, $path, array('html' => TRUE)) . '</div>';
+  $path = $protocol . '://graph.facebook.com/' . $fbuid . '/picture?type=' . $size;
+  $image = theme('image', array('path' => $path,'alt' => $account->name));
+  return '<div class="picture">' . $image/*l($image, $path, array('html' => TRUE))*/ . '</div>';
 }
 
 
diff --git a/fbconnect.rules.inc b/fbconnect.rules.inc
old mode 100644
new mode 100755
diff --git a/fbconnect_invite/fbconnect_invite.info b/fbconnect_invite/fbconnect_invite.info
old mode 100644
new mode 100755
index 6534094..2e0ef78
--- a/fbconnect_invite/fbconnect_invite.info
+++ b/fbconnect_invite/fbconnect_invite.info
@@ -3,3 +3,10 @@ name = Facebook Friends Invite
 dependencies[] = fbconnect
 package = "Facebook Social"
 core = 7.x
+
+; Information added by drupal.org packaging script on 2011-03-17
+version = "7.x-2.x-dev"
+core = "7.x"
+project = "fbconnect"
+datestamp = "1300320371"
+
diff --git a/fbconnect_invite/fbconnect_invite.module b/fbconnect_invite/fbconnect_invite.module
old mode 100644
new mode 100755
index f322d32..15147e6
--- a/fbconnect_invite/fbconnect_invite.module
+++ b/fbconnect_invite/fbconnect_invite.module
@@ -21,7 +21,7 @@ function fbconnect_invite_menu() {
     'type' => MENU_CALLBACK,
   );
 
-  $items['admin/config/fbconnect/invite'] = array(
+  $items['admin/config/people/fbconnect/invite'] = array(
     'title' => 'Friends Invite',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('fbconnect_invite_settings'),
@@ -62,8 +62,10 @@ function fbconnect_invite_theme() {
  * Implements hook_block_info().
  */
 function fbconnect_invite_block_info() {
-  $blocks['facebook_friends']['info']  = t('Facebook Friends');
-  $blocks['facebook_friends']['cache'] = BLOCK_NO_CACHE;
+  $blocks['facebook_friends'] = array(
+    'info' => t('Facebook Friends'), 
+    'cache' => DRUPAL_NO_CACHE,
+  );
   return $blocks;
 }
 
diff --git a/fbconnect_invite/fbconnect_invite.rules.inc b/fbconnect_invite/fbconnect_invite.rules.inc
old mode 100644
new mode 100755
diff --git a/fbconnect_stream_publish/README.txt b/fbconnect_stream_publish/README.txt
old mode 100644
new mode 100755
diff --git a/fbconnect_stream_publish/fbconnect_stream_publish.info b/fbconnect_stream_publish/fbconnect_stream_publish.info
old mode 100644
new mode 100755
index 2b1119e..5277a32
--- a/fbconnect_stream_publish/fbconnect_stream_publish.info
+++ b/fbconnect_stream_publish/fbconnect_stream_publish.info
@@ -2,3 +2,10 @@
 name =  Facebook Stream Publish
 package = "Facebook Social"
 core = 7.x
+
+; Information added by drupal.org packaging script on 2011-03-17
+version = "7.x-2.x-dev"
+core = "7.x"
+project = "fbconnect"
+datestamp = "1300320371"
+
diff --git a/fbconnect_stream_publish/fbconnect_stream_publish.install b/fbconnect_stream_publish/fbconnect_stream_publish.install
old mode 100644
new mode 100755
diff --git a/fbconnect_stream_publish/fbconnect_stream_publish.js b/fbconnect_stream_publish/fbconnect_stream_publish.js
old mode 100644
new mode 100755
diff --git a/fbconnect_stream_publish/fbconnect_stream_publish.module b/fbconnect_stream_publish/fbconnect_stream_publish.module
old mode 100644
new mode 100755
diff --git a/tests/fbconnect.test b/tests/fbconnect.test
old mode 100644
new mode 100755
diff --git a/tests/fbconnect_test.info b/tests/fbconnect_test.info
old mode 100644
new mode 100755
index 0feaf40..481f826
--- a/tests/fbconnect_test.info
+++ b/tests/fbconnect_test.info
@@ -1,4 +1,10 @@
 name = FBConnect Test
 package = 'Test'
 core = 6.x
-dependencies[] = fbconnect
\ No newline at end of file
+dependencies[] = fbconnect
+; Information added by drupal.org packaging script on 2011-03-17
+version = "7.x-2.x-dev"
+core = "7.x"
+project = "fbconnect"
+datestamp = "1300320371"
+
diff --git a/tests/fbconnect_test.module b/tests/fbconnect_test.module
old mode 100644
new mode 100755
