diff --git a/facebook_comments.info b/facebook_comments.info
index af0c30b..a990e9c 100644
--- a/facebook_comments.info
+++ b/facebook_comments.info
@@ -1,4 +1,4 @@
 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
+configure = admin/settings/facebook-comments
\ 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 68a1cc8..0f824bb 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',
@@ -41,26 +37,35 @@ function facebook_comments_block_info() {
 }
 
 /**
- * Implements hook_block_view().
- */
-function facebook_comments_block_view($delta = '') {
-  $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()
- */
+ * 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();
   $form['facebook_comments_style'] = array(
@@ -105,8 +110,9 @@ 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.'),
   );
+	
   $defaulttypes = array();
-  $types = node_type_get_types();
+	$types = node_get_types();
   foreach ($types as $key => $type) {
     $defaulttypes[$key] = $type->name;
   }
@@ -137,18 +143,135 @@ function facebook_comments_admin_applyall(&$form, $form_state) {
     foreach ($form_state['values']['facebook_comments_types'] as $key => $value) {
       if (!empty($value)) $types[] = $key;
     }
-    $results = db_select('node', 'n')
-      ->fields('n', array('nid'))
-      ->condition('type', $types, 'IN')
-      ->execute();
-    while ($result = $results->fetchAssoc()) {
-      db_merge('facebook_comments')
-        ->key(array('nid' => $result['nid']))
-        ->fields(array('enabled' => 1))
-        ->execute();
+    $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(t('Facebook comments have been enabled on existing content for the selected content types.'));
-  }
+    drupal_set_message('Facebook comments have been enabled on existing content for the selected content types.');
+ 	}
+}
+/**
+  * Implements hook_form_alter().
+  *
+  * Add the Facebook commenting options for a node.
+ */
+ 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,
+       );
+     }
+   }
+}
+
+ 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;
+   }
 }
 
 /**
@@ -159,7 +282,7 @@ function facebook_comments_field_extra_fields() {
 
   $default_types = variable_get('facebook_comments_types', array());
   foreach (node_type_get_types() as $type) {
-    if (facebook_comments_is_enabled($type->type)) {
+    if (isset($default_types[$type->type])) {
       $extra['node'][$type->type] = array(
         'display' => array(
           'facebook_comments' => array(
@@ -175,185 +298,31 @@ function facebook_comments_field_extra_fields() {
   return $extra;
 }
 
-/**
- * Helper function to determine if Facebook commenting is enabled.
- */
-function facebook_comments_is_enabled($type) {
-  $default_types = variable_get('facebook_comments_types', array());
-  if (isset($default_types[$type]) && $default_types[$type] === $type) {
-    return TRUE;
-  }
-
-  return FALSE;
-}
-
-/**
- * Implements hook_form_alter().
- *
- * Add the Facebook commenting options for a node.
- */
-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;
-  // Check if the content type is enabled.
-  if (!facebook_comments_is_enabled($form['#node']->type)) 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();
-    // 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 =  FALSE;
-      $defaults->amount = 15;
+  /**
+  * 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_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 = '<meta property="fb:app_id" content="' . $appid . '" />';
+     drupal_set_html_head($element);
     }
-  }
-  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(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) {
-  if (isset($node->facebook_comments_enabled) && !empty($node->facebook_comments_enabled)) {
-    db_insert('facebook_comments')
-      ->fields(array(
-        'nid' => $node->nid,
-        'enabled' => $node->facebook_comments_enabled,
-        'amount' => $node->facebook_comments_amount,
-    ))
-    ->execute();
-  }
-}
-
-/**
- * Implements hook_node_update().
- */
-function facebook_comments_node_update($node) {
-  if (isset($node->facebook_comments_enabled)) {
-    db_merge('facebook_comments')
-      ->key(array('nid' => $node->nid))
-      ->fields(array(
-        'enabled' => $node->facebook_comments_enabled,
-        'amount' => $node->facebook_comments_amount,
-      ))
-      ->execute();
-  }
-}
-
-/**
- * Implements hook_node_delete().
- */
-function facebook_comments_node_delete($node) {
-  db_delete('facebook_comments')
-    ->condition('nid', $node->nid)
-    ->execute();
-}
-
-/**
- * Implements hook_node_view().
- */
-function facebook_comments_node_view($node, $view_mode, $langcode) {
-  // Check if the content type is enabled.
-  if (!facebook_comments_is_enabled($node->type)) return;
-  // Check the view mode to display the comments or not
-  $fc_viewmode = variable_get('facebook_comments_viewmode', 'full');
-  if ($fc_viewmode != "both" && $view_mode != $fc_viewmode) return;
-  // 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 (!isset($comments->enabled) || !$comments->enabled) return;
-  // Add the Facebook App ID if it exists
-  $width = variable_get('facebook_comments_width', 620);
-  $fluid = variable_get('facebook_comments_width_fluid', 0);
-  $output = facebook_comments_display($width, $comments->amount, $fluid);
-  $node->content['facebook_comments'] = array(
-    '#markup' => $output,
-    '#weight' => 1002,
-  );
-}
-
-/**
- * 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_display($width, $amount, $fluid = 0) {
-  // 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 ($fluid) {
-    $class = "fb-comments-fluid";
-    drupal_add_css('.fb-comments-fluid, .fb-comments-fluid span, .fb-comments-fluid iframe {width: 100% !important;}', array('type' => 'inline'));
-  }
-  else {
-    $class = "";
-  }
-  // 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>
+   // 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;}
@@ -361,6 +330,6 @@ function facebook_comments_display($width, $amount, $fluid = 0) {
   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 ' . $class . '" data-href="' . $url . '" data-num-posts="' . $amount . '" data-width="' . $width . '" data-colorscheme="' . $style . '"></div>';
+	<div class="fb-comments" data-href="'. $url .'" data-num-posts="'. $amount .'" data-width="'. $width .'" data-colorscheme="'.$style.'"></div>';
   return $output;
-}
+}
\ No newline at end of file
