diff --git a/fboauth.install b/fboauth.install
index 3043847..295d5ac 100644
--- a/fboauth.install
+++ b/fboauth.install
@@ -20,6 +20,7 @@
 function fboauth_uninstall() {
   variable_del('fboauth_id');
   variable_del('fboauth_secret');
+  variable_del('fboauth_popup');
   variable_del('fboauth_user_email');
   variable_del('fboauth_user_username');
   variable_del('fboauth_user_picture');
diff --git a/fboauth.module b/fboauth.module
index 0ef4138..e8ffa02 100644
--- a/fboauth.module
+++ b/fboauth.module
@@ -40,6 +40,15 @@
 }
 
 /**
+ * Implements hook_init().
+ */
+function fboauth_init() {
+  if (user_is_anonymous()) {
+    drupal_add_js(drupal_get_path('module', 'fboauth') . '/fboauth_popup.js');
+  }
+}
+
+/**
  * Implements hook_theme().
  */
 function fboauth_theme() {
@@ -351,7 +360,13 @@
       'redirect_uri' => fboauth_action_url('fboauth/' . $action_name, array('absolute' => TRUE, 'query' => $query)),
     ),
     'href' => 'https://www.facebook.com/dialog/oauth',
+    'attributes' => array(),
   );
+
+  if (variable_get('fboauth_popup', 0)) {
+    $return['attributes']['class'] = array('fboauth-popup');
+    $return['query']['display'] = 'popup';
+  }
 
   if ($permissions) {
     $return['query']['scope'] = implode(',', $permissions);
@@ -397,7 +412,7 @@
   // Because most Facebook actions initiate one-time actions, render it as a
   // button instead of a link. This makes for expected behavior that buttons
   // execute actions, not links.
-  $link['attributes']['class'] = isset($link['attributes']['class']) ? $link['attributes']['class'] : 'form-button facebook-button facebook-action-' . str_replace('_', '-', $action['name']);
+  $link['attributes']['class'] = array_merge($link['attributes']['class'], array('form-button', 'facebook-button', 'facebook-action-' . str_replace('_', '-', $action['name'])));
   $link['attributes']['name'] = isset($link['attributes']['name']) ? $link['attributes']['name'] : 'facebook_action_' . $action['name'];
   $link['attributes']['type'] = 'button';
   $attributes = drupal_attributes($link['attributes']);
@@ -420,7 +435,7 @@
   $action = $variables['action'];
   $link = $variables['properties'];
   $url = url($link['href'], array('query' => $link['query']));
-  $link['attributes']['class'] = isset($link['attributes']['class']) ? $link['attributes']['class'] : 'facebook-action-connect';
+  $link['attributes']['class'][] = 'facebook-action-connect';
   $link['attributes']['rel'] = 'nofollow';
   $attributes = isset($link['attributes']) ? drupal_attributes($link['attributes']) : '';
   $title = isset($link['title']) ? check_plain($link['title']) : '';
diff --git a/fboauth_popup.js b/fboauth_popup.js
new file mode 100644
index 0000000..c0c104a
--- /dev/null
+++ b/fboauth_popup.js
@@ -0,0 +1,37 @@
+(function ($) {
+
+/**
+ * Add Drupal behaviors
+ */
+Drupal.behaviors.fboauthPopup = {};
+Drupal.behaviors.fboauthPopup.attach = function(context, settings) {
+  /**
+   * modify Facbook Oauth (fboauth) button with class "facebook-action-connect"
+   * to open in a popup window, instead of using the current window.
+   * References:
+   * http://thinkdiff.net/facebook/create-facebook-popup-authentication-window-using-php-and-javascript/
+   * http://developers.facebook.com/docs/authentication/
+   * http://developers.facebook.com/docs/authentication/server-side/
+   * http://developers.facebook.com/docs/reference/dialogs/oauth/
+   */
+  $('.fboauth-popup').click(function() {
+    var screenX    = typeof window.screenX != 'undefined' ? window.screenX : window.screenLeft,
+      screenY      = typeof window.screenY != 'undefined' ? window.screenY : window.screenTop,
+      outerWidth   = typeof window.outerWidth != 'undefined' ? window.outerWidth : document.body.clientWidth,
+      outerHeight  = typeof window.outerHeight != 'undefined' ? window.outerHeight : (document.body.clientHeight - 22),
+      width    = 500,
+      height   = 270,
+      left     = parseInt(screenX + ((outerWidth - width) / 2), 10),
+      top      = parseInt(screenY + ((outerHeight - height) / 2.5), 10),
+      features = 'width=' + width + ',height=' + height + ',left=' + left + ',top=' + top;
+
+    var popupWindow = window.open($(this).attr("href"), 'fboauth', features);
+    if (window.focus) {
+      popupWindow.focus();
+    }
+
+    return false;
+  });
+};
+
+})(jQuery);
diff --git a/includes/fboauth.fboauth.inc b/includes/fboauth.fboauth.inc
index cc4b87e..3d31ef9 100644
--- a/includes/fboauth.fboauth.inc
+++ b/includes/fboauth.fboauth.inc
@@ -42,7 +42,21 @@
     if (empty($destination)) {
       $destination = isset($_REQUEST['destination']) ? $_REQUEST['destination'] : '<front>';
     }
-    drupal_goto($destination);
+
+    if (variable_get('fboauth_popup')) {
+      // Close the popup and set the parent window to the target destination.
+      $url = url($destination);
+      $output = "
+        <script>
+        window.opener.location='$url';
+        setTimeout(function(){ window.close(); }, 500);
+        </script>";
+      print $output;
+      drupal_exit();
+    }
+    else {
+      drupal_goto($destination);
+    }
   }
 
   // In the event of an error, we stay on this page.
diff --git a/includes/fboauth.pages.inc b/includes/fboauth.pages.inc
index dc37e44..0d3dcf8 100644
--- a/includes/fboauth.pages.inc
+++ b/includes/fboauth.pages.inc
@@ -28,6 +28,13 @@
     '#default_value' => variable_get('fboauth_secret', ''),
   );
 
+  $form['fboauth_popup'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Use a popup window for Facebook authentication dialog'),
+    '#description' => t('This may provide a better user experience when logging into Facebook, but may also be blocked by browser popup blocker plugins.'),
+    '#default_value' => variable_get('fboauth_popup', 0),
+  );
+
   $form['fboauth_basic_mapping'] = array(
     '#type' => 'fieldset',
     '#title' => t('Basic mapping'),
@@ -141,6 +148,7 @@
 function fboauth_settings_form_submit($form, &$form_state) {
   variable_set('fboauth_id', $form_state['values']['fboauth_id']);
   variable_set('fboauth_secret', $form_state['values']['fboauth_secret']);
+  variable_set('fboauth_popup', $form_state['values']['fboauth_popup']);
 
   variable_set('fboauth_user_email', $form_state['values']['fboauth_user_email']);
   variable_set('fboauth_user_username', $form_state['values']['fboauth_user_username']);