--- fb_friend.module.orig	2009-11-24 22:53:14.000000000 +0000
+++ fb_friend.module	2009-11-28 13:01:07.000000000 +0000
@@ -51,7 +51,20 @@ function fb_friend_block($op = 'list', $
       global $fb, $fb_app;
       
       if ($delta == FB_FRIEND_DELTA_INVITE_PAGE) {
-        $page_url = url($_GET['q'], array('absolute' => TRUE));
+        //diferentiate iframe from canvas
+        if (fb_is_iframe_canvas()) {
+          $page_url = url($_GET['q']);
+          $app_base = '/fb_cb/'. $fb_app->nid .'/';
+          $clean_request_uri = substr($page_url, strlen($app_base)-1);
+          $page_url = 'http://apps.facebook.com/'. $fb_app->canvas . $clean_request_uri;
+          $multi_selector_elements = array('actiontext' => t('Invite friends'),
+                                           'target' => '_top',
+                                          );
+        }
+        else {
+          $page_url = url($_GET['q'], array('absolute' => TRUE));
+          $multi_selector_elements = array('actiontext' => t('Invite friends'),);
+        }
         // Build the alterable data structure.
         // http://wiki.developers.facebook.com/index.php/Fb:request-form
         $fbml = fb_form_requestform(
@@ -72,7 +85,7 @@ function fb_friend_block($op = 'list', $
             'style' => "width: 630px; height: 400px",
           ));
       
-        $fbml['selector'] = fb_form_multi_selector(array('actiontext' => t('Invite friends')));
+        $fbml['selector'] = fb_form_multi_selector($multi_selector_elements);
       
         // Allow third-party to modify the form
         drupal_alter('fb_friend_invite_page', $fbml);
@@ -106,8 +119,39 @@ function fb_friend_block($op = 'list', $
         // TODO: support canvas pages, in addition to connect pages.
         fb_connect_require_feature('XFBML');
       
-        $app_url = url('<front>', array('absolute' => TRUE));
-        $page_url = url($_GET['q'], array('absolute' => TRUE));
+        // Exclude users who have already installed.
+        $rs = $fb->api_client->fql_query("SELECT uid FROM user WHERE has_added_app=1 and uid IN (SELECT uid2 FROM friend WHERE uid1 = $fbu)");
+        $arFriends = '';
+        $exclude_ids = '';
+        //  Build an delimited list of users...
+        if ($rs) {
+          $exclude_ids .= $rs[0]["uid"];
+          for ( $i = 1; $i < count($rs); $i++ ) {
+            if ( $exclude_ids != "" )      
+              $exclude_ids .= ",";
+            $exclude_ids .= $rs[$i]["uid"];
+          }
+        }
+        
+        //diferentiate iframe from connect
+        if (fb_is_iframe_canvas()) {
+          $page_url = url($_GET['q']);
+          $app_base = '/fb_cb/'. $fb_app->nid .'/';
+          $clean_request_uri = substr($page_url, strlen($app_base)-1);
+          $app_url = 'http://apps.facebook.com/'. $fb_app->canvas;
+          $page_url = 'http://apps.facebook.com/'. $fb_app->canvas . $clean_request_uri;
+          $multi_selector_elements = array('actiontext' => t('Invite friends'),
+                                           'exclude_ids' => $exclude_ids,
+                                           'target' => '_top',
+                                          );
+        }
+        else {
+          $app_url = url('<front>', array('absolute' => TRUE));
+          $page_url = url($_GET['q'], array('absolute' => TRUE));
+          $multi_selector_elements = array('actiontext' => t('Invite friends'),
+                                           'exclude_ids' => $exclude_ids,
+                                          );
+        }
         $site_name = variable_get('site_name', t('application'));
         // Build the alterable data structure.
         // http://wiki.developers.facebook.com/index.php/Fb:request-form
@@ -128,24 +172,7 @@ function fb_friend_block($op = 'list', $
             'method' => 'POST',
           ));
       
-        // Exclude users who have already installed.
-        $rs = $fb->api_client->fql_query("SELECT uid FROM user WHERE has_added_app=1 and uid IN (SELECT uid2 FROM friend WHERE uid1 = $fbu)");
-        $arFriends = '';
-        $exclude_ids = '';
-        //  Build an delimited list of users...
-        if ($rs) {
-          $exclude_ids .= $rs[0]["uid"];
-          for ( $i = 1; $i < count($rs); $i++ ) {
-            if ( $exclude_ids != "" )      
-              $exclude_ids .= ",";
-            $exclude_ids .= $rs[$i]["uid"];
-          }
-        }
-      
-        $fbml['selector'] = fb_form_multi_selector(
-          array('actiontext' => t('Invite friends'),
-                'exclude_ids' => $exclude_ids,
-          ));
+        $fbml['selector'] = fb_form_multi_selector($multi_selector_elements);
       
         // Allow third-party to modify the form
         drupal_alter('fb_friend_invite_app', $fbml);
