diff --git a/facebook_comments.info b/facebook_comments.info
index af0c30b..13cc9b0 100644
--- a/facebook_comments.info
+++ b/facebook_comments.info
@@ -1,4 +1,3 @@
 name = Facebook comments
 description = Enable Facebook comments on nodes
-core = 7.x
-configure = admin/config/content/facebook-comments
\ No newline at end of file
+core = 6.x
\ No newline at end of file
diff --git a/facebook_comments.install b/facebook_comments.install
index b051633..a09faf6 100644
--- a/facebook_comments.install
+++ b/facebook_comments.install
@@ -38,3 +38,18 @@ function facebook_comments_schema() {
     ),
   );
 }
+
+/**
+ * Implements hook_install().
+ */
+function facebook_comments_install() {
+  variable_set('facebook_comments_global_switch', array('status' => 'status'));
+  drupal_install_schema('facebook_comments');
+}
+
+/**
+ * Implements hook_uninstall().
+ */
+function mymodule_uninstall() {
+  drupal_uninstall_schema('facebook_comments');
+}
\ No newline at end of file
diff --git a/facebook_comments.module b/facebook_comments.module
index 22a91cd..4796d71 100644
--- a/facebook_comments.module
+++ b/facebook_comments.module
@@ -1,16 +1,12 @@
 <?php
 
 /**
- * Implements of hook_permission().
+ * Implements of hook_perm().
  */
-function facebook_comments_permission() {
+function facebook_comments_perm() {
   return array(
-    'administer facebook comments' => array(
-      'title' => t('Administer Facebook comments'),
-    ),
-    'moderate facebook comments' => array(
-      'title' => t('Enable/disable Facebook comments per node'),
-    ),
+    'administer facebook comments',
+    'moderate facebook comments'
   );
 }
 
@@ -19,7 +15,7 @@ function facebook_comments_permission() {
  */
 function facebook_comments_menu() {
   $items = array();
-  $items['admin/config/content/facebook-comments'] = array(
+  $items['admin/settings/facebook-comments'] = array(
     'title' => 'Facebook comments settings',
     'description' => 'Configure Facebook comments settings like the Facebook App ID.',
     'page callback' => 'drupal_get_form',
@@ -30,7 +26,34 @@ function facebook_comments_menu() {
 }
 
 /**
+ * Implements hook_block().
+ */
+function facebook_comments_block($op = 'list', $delta = 0, $edit = array()) {
+  switch ($op) {
+    case 'list':
+      $blocks = array();
+      $blocks['facebook-comments'] = array(
+        'info' => t('Facebook comments'),
+      );
+      return $blocks;
+    case 'view':
+      $block = array();
+      if ($delta == 'facebook-comments') {
+        $width = variable_get('facebook_comments_block_width', 208);
+        $amount = variable_get('facebook_comments_block_amount', 15);
+        $block = array(
+          'subject' => t('Facebook comments'),
+          'content' => facebook_comments_display($width, $amount),
+        );
+      }
+      return $block;
+  }
+}
+
+/**
  * Configure Facebook comments settings like the Facebook App ID.
+ *
+ * @see facebook_comments_admin_applyall()
  */
 function facebook_comments_admin() {
   $form = array();
@@ -40,14 +63,46 @@ function facebook_comments_admin() {
     '#default_value' => variable_get('facebook_comments_appid', ''),
     '#description' => t('Enter the Facebook App ID to ensure that all comments can be grouped for moderation.'),
   );
+  $form['facebook_comments_global_switch'] = array(
+    '#type' => 'checkboxes',
+    '#title' => t('Global on/off switch'),
+    '#default_value' => variable_get('facebook_comments_global_switch', 'status'),
+    '#options' => array('status' => t('Enable comments for select content types')),
+  );
+  $form['facebook_comments_style'] = array(
+    '#type' => 'select',
+    '#title' => t('Color Scheme'),
+    '#default_value' => variable_get('facebook_comments_style', 'light'),
+    '#options' => array('light' => t('Light'), 'dark' => t('Dark')),
+  );
+  $form['facebook_comments_viewmode'] = array(
+    '#type' => 'select',
+    '#title' => t('View mode'),
+    '#default_value' => variable_get('facebook_comments_viewmode', 'full'),
+    '#options' => array('both' => t('Both full node and teaser'), 'full' => t('Full node'), 'teaser' => t('Teaser')),
+  );
   $form['facebook_comments_width'] = array(
     '#type' => 'textfield',
-    '#title' => t('Facebook comment plugin width'),
-    '#default_value' => variable_get('facebook_comments_width', '620'),
-    '#description' => t('The width of the Facebook comment plugin width, in pixels. Example: 620'),
+    '#title' => t('Facebook comment plugin width (nodes)'),
+    '#default_value' => variable_get('facebook_comments_width', 620),
+    '#description' => t('The width of the Facebook comment plugin for nodes, in pixels. Example: 620'),
   );
+  $form['facebook_comments_block_width'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Facebook comment block plugin width (block)'),
+    '#default_value' => variable_get('facebook_comments_block_width', 208),
+    '#description' => t('The width of the Facebook comment plugin for the block, in pixels. Example: 250'),
+  );
+  $form['facebook_comments_block_amount'] = array(
+    '#type' => 'select',
+    '#title' => t('Amount of comments to display (block)'),
+    '#options' => array(1 => 1, 2 => 2, 3 => 3, 5 => 5, 7 => 7, 10 => 10, 15 => 15, 20 => 20, 30 => 30),
+    '#default_value' => variable_get('facebook_comments_block_amount', 15),
+  );
+
   $defaulttypes = array();
-  $types = node_type_get_types();
+//  $types = node_type_get_types();
+  $types = node_get_types();
   foreach ($types as $key => $type) {
     $defaulttypes[$key] = $type->name;
   }
@@ -58,134 +113,183 @@ function facebook_comments_admin() {
     '#default_value' => variable_get('facebook_comments_types', array()),
     '#description' => t('Check the content types that should have Facebook comments enabled by default.'),
   );
+  $form['facebook_comments_applyall'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Enable Facebook comments on existing content for the selected content types.'),
+    '#default_value' => FALSE,
+  );
+  $form['#submit'][] = 'facebook_comments_admin_applyall';
   return system_settings_form($form);
 }
 
 /**
- * Implements hook_form_alter().
+ * Form submission handler for facebook_comments_admin().
  *
- * Add the Facebook commenting options for a node.
+ * @see facebook_comments_admin()
  */
-function facebook_comments_form_node_form_alter(&$form, $form_state) {
-  // Check if the user has permission to enabled and disable Facebook comments for this node
-  if (!user_access('moderate facebook comments')) return;
-  // Load the default values
-  $node = $form['#node'];
-  // If this is a preview then get the values from the form, not the db
-  if (isset($form_state['values']['op']) && $form_state['values']['op'] == t('Preview')) {
-    $defaults = new StdClass;
-    $defaults->enabled = $form_state['values']['facebook_comments_enabled'];
-    $defaults->amount = $form_state['values']['facebook_comments_amount'];
-  }
-  elseif (isset($node->nid) && $node->nid > 0) {
-    // Load the values from the db if we are viewing an existing node.
-    $defaults = db_select('facebook_comments', 'f')
-      ->fields('f', array('enabled', 'amount'))
-      ->condition('f.nid', $node->nid, '=')
-      ->execute()
-      ->fetchObject();
+function facebook_comments_admin_applyall(&$form, $form_state) {
+  if ($form_state['values']['facebook_comments_applyall']) {
+    $types = array();
+    foreach ($form_state['values']['facebook_comments_types'] as $key => $value) {
+      if (!empty($value)) $types[] = $key;
+    }
+    $select_placeholders = db_placeholders($types, 'text');
+    $select_query = "SELECT n.nid FROM {node} n WHERE n.type IN (" . $select_placeholders . ")";
+    $results = db_query($select_query, $types);
+    $nids = array();
+    while ($result = db_fetch_array($results)) {
+      $nids[] = $result['nid'];
+      $alter_args = array($result['nid']);
+      if (db_result(db_query("SELECT COUNT(*) FROM {facebook_comments} WHERE nid=%d", array($result['nid'])))) {
+        $alter_query = "UPDATE {facebook_comments} SET enabled = 1 WHERE nid = %d";
+      }
+      else {
+        $alter_query = "INSERT INTO {facebook_comments} VALUES (%d, 1, %d)";
+        $alter_args[] = 15;
+      }
+      db_query($alter_query, $alter_args);
+    }
+    drupal_set_message('Facebook comments have been enabled on existing content for the selected content types.');
   }
-  else {
-    // init standard values
-    $defaulttypes = variable_get('facebook_comments_types', array());
-    $defaults = new StdClass;
-    $defaults->enabled = !empty($defaulttypes[$node->type]) ? 1 : 0;
-    $defaults->amount = 15;
-  }
-  $form['facebook_comments'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Facebook comments'),
-    '#group' => 'additional_settings',
-    '#attributes' => array('class' => array('edit-facebook-comments')),
-    '#attached' => array(
-      'js' => array('vertical-tabs' => drupal_get_path('module', 'facebook_comments') . "/facebook_comments_vertical_tabs.js"),
-    ),    
-  );
-  $form['facebook_comments']['facebook_comments_description'] = array(
-    '#prefix' => '<div class="description">',
-    '#suffix' => '</div>',
-    '#markup' => t('The Facebook App ID can be set <a href="@link">here</a>.', array('@link' => url('admin/config/content/facebook-comments'))),
-  );
-  // Enable or disable Facebook comments for this node
-  $form['facebook_comments']['facebook_comments_enabled'] = array(
-    '#type' => 'checkbox',
-    '#title' => t('Enable Facebook comments'),
-    '#default_value' => $defaults->enabled,
-  );
-  // Amount of comments
-  $form['facebook_comments']['facebook_comments_amount'] = array(
-    '#type' => 'select',
-    '#title' => t('Amount of comments to display'),
-    '#options' => array(0 => 0, 1 => 1, 2 => 2, 3 => 3, 5 => 5, 7 => 7, 10 => 10, 15 => 15, 20 => 20, 30 => 30),
-    '#default_value' => $defaults->amount,
-  );
-}
-
-/**
- * Implements hook_node_insert().
- */
-function facebook_comments_node_insert($node) {
-  db_insert('facebook_comments')
-    ->fields(array(
-      'nid' => $node->nid,
-      'enabled' => $node->facebook_comments_enabled,
-      'amount' => $node->facebook_comments_amount,
-    ))
-    ->execute();
 }
 
 /**
- * Implements hook_node_update().
+ * Implements hook_form_alter().
+ *
+ * Add the Facebook commenting options for a node.
  */
-function facebook_comments_node_update($node) {
-  db_update('facebook_comments')
-    ->fields(array(
-      'enabled' => $node->facebook_comments_enabled,
-      'amount' => $node->facebook_comments_amount,
-    ))
-    ->condition('nid', $node->nid)
-    ->execute();
+function facebook_comments_form_alter(&$form, $form_state) {
+  // Load the default values
+  if (isset($form['#node'])) {
+    $node = $form['#node'];
+    $node_types = variable_get('facebook_comments_types', array());
+    // Check if the user has permission to enabled and disable Facebook comments for this node
+    if (!user_access('moderate facebook comments')) return;
+    if (in_array($node->type, array_values($node_types), TRUE)) {
+      // If this is a preview then get the values from the form, not the db
+      if (isset($form_state['values']['op']) && $form_state['values']['op'] == t('Preview')) {
+        $defaults = new StdClass;
+        $defaults->enabled = $form_state['values']['facebook_comments_enabled'];
+        $defaults->amount = $form_state['values']['facebook_comments_amount'];
+      }
+      elseif (isset($node->nid) && $node->nid > 0) {
+        // Load the values from the db if we are viewing an existing node.
+        $defaults_query = "SELECT enabled, amount FROM {facebook_comments} WHERE nid = %d";
+        $defaults = db_fetch_object(db_query($defaults_query, array($node->nid)));
+        // If the node is existed before we installed facebook_comments add default values.
+        if(!$defaults) {
+          $defaulttypes = variable_get('facebook_comments_types', array());
+          $defaults = new StdClass;
+          $defaults->enabled =  !empty($defaulttypes[$node->type]) ? 1 : 0;;
+          $defaults->amount = 15;
+        }
+      }
+      else {
+        // Init standard values
+        $defaulttypes = variable_get('facebook_comments_types', array());
+        $defaults = new StdClass;
+        $defaults->enabled = !empty($defaulttypes[$node->type]) ? 1 : 0;
+        $defaults->amount = 15;
+      }
+      $form['facebook_comments'] = array(
+        '#type' => 'fieldset',
+        '#title' => t('Facebook comments'),
+        '#group' => 'additional_settings',
+        '#attributes' => array('class' => array('edit-facebook-comments')),
+      );
+//      This breaks the default vertical tabs when the module is enabled
+//      drupal_add_js(drupal_get_path('module','facebook_comments') . '/facebook_comments_vertical_tabs.js');
+      $form['facebook_comments']['facebook_comments_description'] = array(
+        '#prefix' => '<div class="description">',
+        '#suffix' => '</div>',
+        '#markup' => t('The Facebook App ID can be set <a href="@link">here</a>.', array('@link' => url('admin/settings/facebook-comments'))),
+      );
+      // Enable or disable Facebook comments for this node
+      $form['facebook_comments']['facebook_comments_enabled'] = array(
+        '#type' => 'checkbox',
+        '#title' => t('Enable Facebook comments'),
+        '#default_value' => $defaults->enabled,
+      );
+      // Amount of comments
+      $form['facebook_comments']['facebook_comments_amount'] = array(
+        '#type' => 'select',
+        '#title' => t('Amount of comments to display'),
+        '#options' => array(1 => 1, 2 => 2, 3 => 3, 5 => 5, 7 => 7, 10 => 10, 15 => 15, 20 => 20, 30 => 30),
+        '#default_value' => $defaults->amount,
+      );
+    }
+  }
 }
 
-/**
- * Implements hook_node_delete().
- */
-function facebook_comments_node_delete($node) {
-  db_delete('facebook_comments')
-    ->condition('nid', $node->nid)
-    ->execute();
+function facebook_comments_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
+  switch ($op) {
+    case 'insert':
+      if (isset($node->facebook_comments_enabled) && !empty($node->facebook_comments_enabled)) {
+        $query = "INSERT INTO {facebook_comments} VALUES (%d, %d, %d)";
+        $args = array($node->nid, $node->facebook_comments_enabled, $node->facebook_comments_amount);
+        db_query($query, $args);
+      }
+      break;
+    case 'update':
+      if (isset($node->facebook_comments_enabled)) {
+        $query = "UPDATE {facebook_comments} SET enabled = %d, amount = %d WHERE nid = %d";
+        $args = array($node->facebook_comments_enabled, $node->facebook_comments_amount, $node->nid);
+        db_query($query, $args);
+      }
+      break;
+    case 'delete':
+      $query = "DELETE FROM {facebook_comments} WHERE nid=%d";
+      $args = array($node->nid);
+      db_query($query, $args);
+      break;
+    case 'view':
+      $switch = variable_get('facebook_comments_global_switch', 0);
+      if ($switch['status']) {
+        // Check the view mode to display the comments or not
+        $fc_viewmode = variable_get('facebook_comments_viewmode', 'full');
+        if ($fc_viewmode == "teaser") return;
+        // Check if Facebook comments are enabled for this node
+        $query = "SELECT enabled, amount FROM {facebook_comments} fb WHERE fb.nid = %d";
+        $args = array($node->nid);
+        $comments = db_fetch_object(db_query($query, $args));
+        if (!isset($comments->enabled) || !$comments->enabled) return;
+        // Add the Facebook App ID if it exists
+        $width = variable_get('facebook_comments_width', 620);
+        $output = facebook_comments_display($width, $comments->amount);
+        $node->content['facebook_comments'] = array(
+          '#value' => $output,
+          '#weight' => 1002,
+        );
+      }
+      break;
+  }
 }
 
 /**
- * Implements hook_node_view().
+ * Generate the output of a Facebook commenting plugin.
+ *
+ * @param width
+ *   The width of the plugin in pixels.
+ * @param amount
+ *   The amount of comments to display.
  */
-function facebook_comments_node_view($node, $view_mode, $langcode) {
-  // Check if Facebook comments are enabled for this node
-  $comments = db_select('facebook_comments', 'f')
-    ->fields('f', array('enabled', 'amount'))
-    ->condition('f.nid', $node->nid, '=')
-    ->execute()
-    ->fetchObject();
-  if (!$comments->enabled) return;
+function facebook_comments_display($width, $amount) {
+  $switch = variable_get('facebook_comments_global_switch', 0);
+  if (!$switch['status']) {
+    // for comment block
+    return 'Sorry, commenting on this page is currently closed';
+  }
   // Add the Facebook App ID if it exists
-  if ($appid = variable_get('facebook_comments_appid', '')) {    
-    $element = array(
-      '#tag' => 'meta',
-      '#attributes' => array(
-        'property' => 'fb:app_id',
-        'content' => $appid,
-      ),
-    );
-    drupal_add_html_head($element, 'facebook_comments');
+  if ($appid = variable_get('facebook_comments_appid', '')) {
+    $element = '<meta property="fb:app_id" content="' . $appid . '" />';
+    drupal_set_html_head($element);
   }
-  // Show the Facebook comments
-  $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
-  $protocol = strtolower($_SERVER["SERVER_PROTOCOL"]);
-  $protocol = substr($protocol, 0, strpos($protocol, "/")) . $s; 
-  $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]); 
-  $url = $protocol ."://". $_SERVER['SERVER_NAME'] . $port . $_SERVER['REQUEST_URI'];
-  $width = variable_get('facebook_comments_width', '620');
-  $facebook_comments = '<div id="fb-root"></div>
+  // Generate the URL
+  global $base_url;
+  $url = $base_url .'/'. drupal_get_path_alias($_GET['q']);
+  // Add user defined settings
+  $style = variable_get('facebook_comments_style', 'light');
+  $output = '<div id="fb-root"></div>
 <script>(function(d, s, id) {
   var js, fjs = d.getElementsByTagName(s)[0];
   if (d.getElementById(id)) {return;}
@@ -193,9 +297,6 @@ function facebook_comments_node_view($node, $view_mode, $langcode) {
   js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
   fjs.parentNode.insertBefore(js, fjs);
 }(document, "script", "facebook-jssdk"));</script>
-<div class="fb-comments" data-href="'. $url .'" data-num-posts="'. $comments->amount .'" data-width="'. $width .'"></div>';
-  $node->content['facebook_comments'] = array(
-    '#markup' => $facebook_comments, 
-    '#weight' => 1002, 
-  );
+<div class="fb-comments" data-href="'. $url .'" data-num-posts="'. $amount .'" data-width="'. $width .'" data-colorscheme="'.$style.'"></div>';
+  return $output;
 }
