--- fckeditor.module.orig	2008-10-31 16:16:48.000000000 -0400
+++ fckeditor.module	2008-11-11 19:46:19.000000000 -0400
@@ -509,6 +509,12 @@ function fckeditor_profile_save($edit) {
       db_query("INSERT INTO {fckeditor_role} (name, rid) VALUES ('%s', %d)", $edit['name'], $rid);
     }
   }
+  db_query("DELETE FROM {fckeditor_node_type} WHERE name = '%s' or name = '%s'", $edit['name'], $edit['old_name']);
+  if ($edit['types']) {
+    foreach ($edit['types'] as $type => $value) {
+      db_query("INSERT INTO {fckeditor_node_type} (name, type) VALUES ('%s', '%s')", $edit['name'], $type);
+    }
+  }
   // if users can't set their own defaults, make sure to remove $user->fckeditor_status so their default doesn't override the main default
   if ($edit['user_choose'] == 'false') {
     global $user;
@@ -586,6 +592,14 @@ function fckeditor_profile_load($name = 
       }
       $data->rids = $role;
 
+      // get fckeditor node types
+      $result3 = db_query("SELECT type FROM {fckeditor_node_type} WHERE name = '%s'", $data->name);
+      $type = array();
+      while ($r = db_fetch_object($result3)) {
+        $type[$r->type] = $types[$r->type];
+      }
+      $data->types = $type;
+
       $profiles[$data->name] = $data;
     }
   }
@@ -1379,6 +1393,7 @@ function fckeditor_profile_form_build($e
   // profile per role.
   $orig_roles = user_roles(FALSE, 'access fckeditor');
   $roles = $orig_roles;
+  $types = node_get_types('names');
   
   if ($edit->rids && !user_roles(false, 'access fckeditor')) {
     drupal_set_message(t('You haven\'t assigned <code>!access1</code> !permissions yet.<br/>It is recommended to assign the <code>!access1</code> !permissions before updating FCKeditor profiles.', 
@@ -1435,6 +1450,15 @@ function fckeditor_profile_form_build($e
   '#required' => TRUE
   );
 
+  $form['basic']['types'] = array(
+  '#type' => 'checkboxes',
+  '#title' => t('Content types that can use this profile'),
+  '#default_value' => array_keys((array) $edit->types),
+  '#options' => $types,
+#  '#description' =>  t(''),
+  '#required' => TRUE
+  );
+
   $form['basic']['allow_user_conf'] = array(
   '#type' => 'select',
   '#title' => t('Allow users to customize FCKeditor appearance'),
@@ -1884,29 +1908,65 @@ function fckeditor_user_get_setting($use
 function fckeditor_user_get_profile($user) {
   static $profile_name;
 
+  $page_node_type = null;
+  if ('node' == arg(0)) {
+    if ('add' == arg(1)) {
+      $page_node_type = strtr(arg(2),'-','_');
+    } // end if add
+    else if ('edit' == arg(2)) {
+      $nid = arg(1);
+      $node = node_load($nid);
+      $page_node_type = $node->type;
+    } // end if edit
+  } // end if node
+
   // Since fckeditor_profile_load() makes a db hit, only call it when we're pretty sure
   // we're gonna render fckeditor.
-  if (!isset($profile_name[$user->uid])) {
+  if (!isset($profile_name[$page_node_type][$user->uid])) {
     $sorted_roles = fckeditor_sorted_roles();
-    foreach ($sorted_roles as $rid => $name) {
-      if (isset($user->roles[$rid])) {
-        break;
-      }
-    }
+    if (!empty($sorted_roles)) {
+      $sql = "SELECT r.rid, " .
+        (isset($page_node_type) ? "nt.type, " : '') .
+        "s.name FROM {fckeditor_settings} s " .
+        "INNER JOIN {fckeditor_role} r ON r.name = s.name " .
+        (isset($page_node_type)
+          ? "INNER JOIN {fckeditor_node_type} nt ON nt.name = s.name " : '') .
+        "WHERE r.rid IN (%s) " .
+        (isset($page_node_type) ? "AND nt.type = '%s' " : '' );
+      $res = db_query($sql, implode(",", array_keys($sorted_roles)),
+        $page_node_type);
+
+      $profiles = array();
+      while ($row = db_fetch_array($res)) {
+        $profiles[] = $row;
+      } // end while rows
+
+      $sorted_profiles = array();
+      foreach ($sorted_roles as $rid => $name) {
+        foreach ($profiles as $key => $row) {
+          if ($rid == $row['rid']) {
+            $sorted_profiles[] = $row;
+            unset($profiles[$key]);
+            break;
+          } // end if found rid
+        } // end foreach $row
+      } // end foreach $rid
+
+      $preferred_profile = current($sorted_profiles);
+      $profile_name[$page_node_type][$user->uid] = $preferred_profile['name'];
+    } // end if $sorted_roles
+  } // end if no profile
+
+  if (isset($profile_name[$page_node_type][$user->uid])
+    && $profile_name[$page_node_type][$user->uid]) {
+    $profile = fckeditor_profile_load($profile_name[$page_node_type][$user->uid]);
+    if (!isset($page_node_type)
+      || in_array($page_node_type, array_keys($profile->types)))
+    {
+      return $profile;
+    } // end if no page node type or valid node type
+  } // end if valid profile name for user
 
-    if (isset($user->roles[$rid])) {
-      $profile_name[$user->uid] = db_result(db_query("SELECT s.name FROM {fckeditor_settings} s INNER JOIN {fckeditor_role} r ON r.name = s.name WHERE r.rid='%s'", $rid));
-    }
-    else if ($user->uid == "1") {
-      $profile_name[$user->uid] = db_result(db_query_range("SELECT s.name FROM {fckeditor_settings} s INNER JOIN {fckeditor_role} r ON r.name = s.name ORDER BY r.rid DESC", 1));
-    }
-  }
-  
-  if (isset($profile_name[$user->uid]) && $profile_name[$user->uid]) {
-    $profile = fckeditor_profile_load($profile_name[$user->uid]);
-    return $profile;
-  }
-  
   return FALSE;
 }
 
