diff -urPp wysiwyg_template/README.txt wysiwyg_template_new/README.txt
--- wysiwyg_template/README.txt	2009-06-10 21:53:53.000000000 +0200
+++ wysiwyg_template_new/README.txt	2010-11-14 23:00:29.000000000 +0200
@@ -19,27 +19,22 @@ Bug reports, feature suggestions and lat
 
 * Install as usual, see http://drupal.org/node/70151 for further information.
 
-* Go to Administer > Site configuration > Wysiwyg Template, and verify the values
-  for your path and file name of your template registry file  (this file will 
-  hold the names and locations of your templates)
-
-* Create the template registry file in the specified location, its contents should
-  look something like this: 
-  
-  // These templates will be displayed as a dropdown
-  var tinyMCETemplateList = [
-  // Name, URL, Description
-  ["Template One", "sites/all/files/templates/template1.html", "Template One"],
-  ["Template Two", "sites/all/files/templates/template2.html", "Template Two"]
-  ];
-  
-*  Create your template files at the locations specified in your registry file
-
--- CONFIGURATION --
-
 * Go to Administer > Site configuration > Wysiwyg and
 
   - click "edit" to set up your editor profile
 
   - under "Buttons and Plugins" you'll see "Insert Templates" as a new option
 
+* Administer > Site configuration > Wysiwyg > Wysiwyg Templates to manage templates.
+
+
+-- EDITOR COMPATIBILITY --
+
+Supported editors:
+
+ - FCK Editor 2.66
+ 
+ - CK Editor 3.4.0
+ 
+ - Tiny MCE 3.3.8
+
Only in wysiwyg_template: js
diff -urPp wysiwyg_template/wysiwyg_template.admin.inc wysiwyg_template_new/wysiwyg_template.admin.inc
--- wysiwyg_template/wysiwyg_template.admin.inc	1970-01-01 02:00:00.000000000 +0200
+++ wysiwyg_template_new/wysiwyg_template.admin.inc	2010-11-14 23:48:44.000000000 +0200
@@ -0,0 +1,177 @@
+<?php
+
+/**
+ * @file
+ * Administrative page callbacks for the Wysiwyg Template module.
+ */
+
+/**
+ * Wysiwyg template overview page - view templates and edit them
+ */
+function wysiwyg_template_overview(){
+  $rows = array();
+  // load the templates and display them on the admin form
+  $templateResults = db_query("SELECT * FROM {wysiwyg_templates}");
+  while ($template = db_fetch_object($templateResults)) {
+    $row = array();
+    $row[] = $template->title;
+    $row[] = $template->description;
+    $row[] = l(t('edit'), "admin/settings/wysiwyg/templates/edit/" . $template->tid);
+    $row[] = l(t('delete'), "admin/settings/wysiwyg/templates/delete/" . $template->tid);
+    
+    $rows[] = $row;
+  }
+  
+  if (empty($rows)){
+    $rows[] = array(array(
+      'data' => t('No templates available.'), 
+      'colspan' => 4
+    ));
+  }
+  $rows[] = array(array(
+    'data' => l(t('Create a new template'), 'admin/settings/wysiwyg/templates/add'),
+    'colspan' => 4,
+  ));
+  
+  $header = array(t('Name'), t('Description'), array('data' => t('Operations'), 'colspan' => 2));
+  return theme('table', $header, $rows, array('id' => 'wysiwyg_template'));
+}
+
+/**
+ * Wysiwyg template create/modify form
+ */
+function wysiwyg_template_template_form($form_state, $template = NULL) {
+  if($form_state['confirm_delete']){
+    // Rebuild the form to confirm term deletion.
+    $form['tid'] = array('#type' => 'value', '#value' => $form_state['values']['tid']);
+    $form['delete'] = array('#type' => 'value', '#value' => true);
+    return confirm_form($form, 
+      t('Are you sure you want to delete the template %title?', array('%title' => $form_state['values']['title'])),
+      'admin/content/wysiwyg-template',
+      t('This action cannot be undone.'),
+      t('Delete'),
+      t('Cancel')
+    );
+  }
+
+  if($template){
+    // add the current values as defaults to the form, if editing an existing item
+    $form_state['values'] = $template;
+  }
+  
+  $form = array();
+  
+  $form['title'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Template Name'),
+    '#default_value' => $form_state['values']['title'],
+    '#description' => t('Select a name for this template.'),
+    '#maxlength' => 80,
+    '#required' => true
+  );
+  $form['description'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Template Description'),
+    '#default_value' => $form_state['values']['description'],
+    '#description' => t('A description to be shown with the template.')
+  );
+  $form['body'] = array(
+    '#type' => 'textarea',
+    '#title'  => t('HTML Template'),
+    '#rows'  =>  10,
+    '#default_value' => $form_state['values']['body'],
+    '#required' => true
+  );
+  $form['submit'] = array(
+    '#type' => 'submit', 
+    '#value' => t('Save')
+  );
+  
+  if($form_state['values']['tid']){
+    // if it's an existing template, offer a delete button
+    $form['delete'] = array(
+      '#type' => 'submit',
+      '#value' => t('Delete')
+    );
+    // and keep the template id going through a hidden field
+    $form['tid'] = array(
+      '#type' => 'hidden',
+      '#value' => $form_state['values']['tid'],
+    );
+  }
+  return $form;
+}
+
+/**
+ * Wysiwyg template form submit - delete and save handlers
+ */
+function wysiwyg_template_template_form_submit($form, &$form_state) {
+  //if we're deleting the template
+  if ($form_state['clicked_button']['#id'] == 'edit-delete') {
+    //show the confirmation
+    $form_state['rebuild'] = true;
+    $form_state['confirm_delete'] = true;
+    return;
+  }
+  // delete confirmation provided
+  if ($form_state['values']['delete'] === TRUE) {
+    wysiwyg_template_delete_template($form_state['values']['tid']);                             
+    drupal_set_message(t('The template has been deleted.'));    
+    $form_state['redirect'] = 'admin/settings/wysiwyg/templates';
+    return;
+  }
+
+  // save the template
+  if(wysiwyg_template_save_template($form_state['values'])){
+    drupal_set_message(t('The template has been saved.'));
+  }
+  else{
+    drupal_set_message(t('There was an error saving the template to the database.'));
+  }
+  // redirect back to the overview page
+  $form_state['redirect'] = 'admin/settings/wysiwyg/templates';
+}
+
+/**
+ * Menu callback -- ask for confirmation of rule deletion.
+ */
+function wysiwyg_template_delete_confirm(&$form_state, $tid) {
+  $form['tid'] = array(
+    '#type' => 'value',
+    '#value' => $tid,
+  );
+
+  $template = wysiwyg_template_load_template($tid);
+  return confirm_form($form,
+    t('Are you sure you want to delete the template %title?', array('%title' => $template['title'])),
+    isset($_GET['destination']) ? $_GET['destination'] : 'admin/settings/wysiwyg/templates',
+    t('This action cannot be undone.'),
+    t('Delete'),
+    t('Cancel')
+  );
+}
+
+/**
+ * Execute node deletion.
+ */
+function wysiwyg_template_delete_confirm_submit($form, &$form_state) {
+  if ($form_state['values']['confirm']) {
+    wysiwyg_template_delete_template($form_state['values']['tid']);
+  }
+
+  $form_state['redirect'] = 'admin/settings/wysiwyg/templates';
+  return;
+}
+
+
+/**
+ * Implementation of hook_help
+ */
+function wysiwyg_template_help($path, $arg) {
+  switch ($path) {
+    case 'admin/settings/wysiwyg/templates':
+      $output = t('<p>The Wysiwyg Template module allows you to create templates to be used with a Wysiwyg editor of your choice. The template button for the editor will need to be enabled from the !profiles settings. The currently supported editors are FCK Editor, CK Editor and TinyMCE.</p>', array('!profiles', l(t('WYSIWYG Profile'), 'admin/settings/filters')));
+      return $output;
+    break;
+  }
+}
diff -urPp wysiwyg_template/wysiwyg_template.install wysiwyg_template_new/wysiwyg_template.install
--- wysiwyg_template/wysiwyg_template.install	1970-01-01 02:00:00.000000000 +0200
+++ wysiwyg_template_new/wysiwyg_template.install	2010-11-14 23:19:19.000000000 +0200
@@ -0,0 +1,46 @@
+<?php
+
+/**
+ * Implementation of hook_install().
+ */
+function wysiwyg_template_install() {
+  drupal_install_schema('wysiwyg_template');
+}
+
+/**
+ * Implementation of hook_schema().
+ */
+function wysiwyg_template_schema() {
+  $schema['wysiwyg_templates'] = array(
+    'fields' => array(
+      'tid' => array(
+        'description' => 'The primary identifier for the Wysiwyg template',
+        'type' => 'serial',
+        'unsigned' => TRUE,
+        'not null' => TRUE),
+      'title' => array(
+        'description' => 'The title of the Wysiwyg template',
+        'type' => 'varchar',
+        'length' => 255,
+        'not null' => TRUE),
+      'description' => array(
+        'description' => 'The description of the Wysiwyg template',
+        'type' => 'varchar',
+        'length' => 255,
+        'not null' => TRUE),
+      'body' => array(
+        'description' => 'The Wysiwyg template HTML',
+        'type' => 'text'),
+    ),
+    'primary key' => array('tid'),
+  );
+  return $schema;
+}
+
+/**
+ * Implementation of hook_uninstall().
+ */
+function wysiwyg_template_uninstall() {
+  cache_clear_all('wysiwyg_template:*', 'cache', TRUE);
+  db_query("DROP TABLE {wysiwyg_templates}");
+}
diff -urPp wysiwyg_template/wysiwyg_template.module wysiwyg_template_new/wysiwyg_template.module
--- wysiwyg_template/wysiwyg_template.module	2009-06-10 23:50:19.000000000 +0200
+++ wysiwyg_template_new/wysiwyg_template.module	2010-11-14 23:47:04.000000000 +0200
@@ -1,5 +1,4 @@
 <?php
-// $Id: wysiwyg_template.module,v 1.4 2009/06/10 21:50:19 jenlampton Exp $
 
 /**
  * @file
@@ -7,79 +6,273 @@
  * Wysiwyg API.
  */
 
+ 
+ /**
+ * Implementation of hook_menu().
+ */
+function wysiwyg_template_menu() {
+  $items = array();
+  
+  // template overview settings page
+  $items['admin/settings/wysiwyg/templates'] = array(
+    'title' => 'Templates',
+    'description' => 'Create and Modify Templates',
+    'page callback' => 'wysiwyg_template_overview',
+    'type' => MENU_CALLBACK,
+    'access arguments' => array('administer filters'),
+    'file' => 'wysiwyg_template.admin.inc',
+  );
+  //list template
+  $items['admin/settings/wysiwyg/templates/list'] = array(
+    'title' => 'List',
+    'type' => MENU_DEFAULT_LOCAL_TASK,
+    'weight' => -10,
+  );
+  // add template
+  $items['admin/settings/wysiwyg/templates/add'] = array(
+    'title' => 'Add Template',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('wysiwyg_template_template_form'),
+    'access arguments' => array('administer filters'),
+    'type' => MENU_LOCAL_TASK,
+    'file' => 'wysiwyg_template.admin.inc',
+  );
+  // edit template
+  $items['admin/settings/wysiwyg/templates/edit/%wysiwyg_template'] = array(
+    'title' => 'Edit Template',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('wysiwyg_template_template_form', 5),
+    'access arguments' => array('administer filters'),
+    'type' => MENU_CALLBACK,
+    'parent' => 'admin/settings/wysiwyg/templates',
+    'file' => 'wysiwyg_template.admin.inc',
+  );
+  //delete template
+  $items['admin/settings/wysiwyg/templates/delete'] = array(
+    'title' => 'Delete Template',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('wysiwyg_template_delete_confirm'),
+    'access arguments' => array('administer filters'),
+    'file' => 'wysiwyg_template.admin.inc',
+    'type' => MENU_CALLBACK,
+  );
+  // javascript template list
+  $items['wysiwyg-templates/%/list'] = array(
+    'page callback' => 'wysiwyg_template_list_js',
+    'page arguments' => array(1),
+    'access arguments' => array('access content'),
+    'type' => MENU_CALLBACK,
+  );
+  // individual template html - referenced by javascript above
+  $items['wysiwyg-templates/%/load/%wysiwyg_template_html'] = array(
+    'page callback' => 'wysiwyg_template_html_print',
+    'page arguments' => array(3, 1),
+    'access arguments' => array('access content'),
+    'type' => MENU_CALLBACK,
+  );
+  return $items;
+}
+
 /**
- * Implementation of hook_wysiwyg_plugin().
+ * Wysiwyg template Wildcard menu loader - prepare item for the create/modify admin form
+ * 
+ * @ tid - template ID
+ *
+ * return $template - the loaded template from the db
  */
-function wysiwyg_template_wysiwyg_plugin($editor, $version) {
-  static $integrated = array();
+function wysiwyg_template_load($tid){
+  $template = wysiwyg_template_load_template($tid);
+  return $template;
+} 
 
-  switch ($editor) {
+/**
+ * Implementation of hook_wysiwyg_plugin().
+ */
+function wysiwyg_template_wysiwyg_plugin($editorName, $version) {
+  switch ($editorName) {
     case 'tinymce':
-      // get values defined on settings page
-      $jspath = variable_get('wysiwyg_template_path', 'sites/all/files');
-      $jsfile = variable_get('wysiwyg_template_file', 'templates.js');
-      // include path to js file
-      /*
-      $settings = array(
-        'template' => array('template_external_list_url' => base_path() . $jspath . '/' . $jsfile),
+      return array(
+	'template' => array(
+	  'buttons' => array('template' => t('Insert templates')),
+	  'url' => 'admin/content/wysiwyg-template',
+	  'internal' => TRUE,
+	  'options' => array(
+	    'template_external_list_url' => url('wysiwyg-templates/' . $editorName . '/list'), //load the javascript template list
+	    'template_selected_content_classes' => variable_get('wysiwyg_template_selected_content_classes', ''),
+	    //'template_replace_values' => array('caption' => 'Replace with this content')
+	  ),
+	  'extended_valid_elements' => array( //tinymce specific - allows additional elements to be used
+	    'img[class|src|border=0|alt|title|width|height|align|name|style]',
+	  ),
+	  'load' => TRUE,
+	),
+      );
+    break;
+    case 'ckeditor':
+      return array(
+	'templates' => array(
+	  'buttons' => array('Templates' => t('Insert templates')),
+	  'url' => 'admin/content/wysiwyg-template',
+	  'internal' => TRUE,
+	  'options' => array(
+	    'templates_files' => array(url('wysiwyg-templates/' . $editorName . '/list')), //load the javascript template list
+	  ),
+	  'load' => TRUE,
+	),
       );
-      drupal_add_js($settings, 'setting');
-      */
-      $tinymce = wysiwyg_get_editor($editor);
+    break;
+    case 'fckeditor':
       return array(
-        'template' => array(
-          'path' => $tinymce['library path'] .'/plugins/template',
-          'buttons' => array('template' => t('Insert templates')),
-          'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/template',
-          'internal' => TRUE,
-          'options' => array(
-            'template_external_list_url' => base_path() . $jspath . '/' . $jsfile,
-          ),
-          'load' => TRUE,
-        ),
+	'templates' => array(
+	  'buttons' => array('Templates' => t('Insert templates')),
+	  'url' => 'admin/content/wysiwyg-template',
+	  'internal' => TRUE,
+	  'options' => array(
+	    'TemplatesXmlPath' => array(url('wysiwyg-templates/' . $editorName . '/list')), //load the javascript template list
+	  ),
+	  'load' => TRUE,
+	),
       );
+    break;
   }
 }
 
 /**
- * Implementation of hook_menu().
+ * Generate javascript for template loading
  */
-function wysiwyg_template_menu() {
-  $items = array();
+function wysiwyg_template_list_js($editorName){
+  //don't cache templates
+  drupal_set_header("CacheControl: no-cache");
+  drupal_set_header("Expires: -1");
+  drupal_set_header("Content-Type:text/javascript; charset=UTF-8");
+  $templates = wysiwyg_template_load_all();
+  switch($editorName){
+    case 'tinymce':
+      print 'var tinyMCETemplateList = ';
+      $outArray = array();
+      foreach($templates as $template){
+	$outArray[] = array(
+		$template['title'],
+		url('wysiwyg-templates/' . $editorName . '/load/') . $template['tid'],
+		$template['description'],
+	);
+      }
+      print json_encode($outArray) . ";";
+    break;
+    case 'ckeditor':
+      print "CKEDITOR.addTemplates( 'default', { templates:";
+      //todo: include an image with templates...
+      //imagesPath : CKEDITOR.getUrl( CKEDITOR.plugins.getPath( 'templates' ) + 'templates/images/' ),
+      // load the templates into the json array structure
+      foreach($templates as &$template){
+	$template['html'] = $template['body'];
+	unset($template['body']);
+	unset($template['tid']);
+      }
+      print json_encode($templates);
+      print "});";
+    break;
+    case 'fckeditor':
+      print '<?xml version="1.0" encoding="utf-8" ?>';
+      print '<Templates imagesBasePath="">';
+      foreach($templates as $template){
+	print '<Template title="' . check_plain($template['title']) . '" image="">';
+	print '<Description>' . check_plain($template['description']) . '</Description>';
+	print '<Html><![CDATA[';
+	print $template['body'];
+	print ']]></Html></Template>';
+      }
+      print '</Templates>';
+    break;
+    case '':
+    break;
+  }
+}
 
-  $items['admin/settings/wysiwyg_template'] = array(
-    'title' => 'Wysiwyg Templates',
-    'description' => 'Wysiwyg Template plugin settings',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('wysiwyg_template_admin_settings'),
-    'access arguments' => array('administer filters'),
-  );
+/**
+ * Menu callback to display the loaded template
+ */
+function wysiwyg_template_html_print($body, $editorName){
+  //don't cache templates
+  drupal_set_header("CacheControl: no-cache");
+  drupal_set_header("Expires: -1");
+  drupal_set_header("Content-Type:text/html; charset=utf-8");
+  switch($editorName){
+    case 'tinymce':
+	    print $body;
+    break;
+  }
+}
 
-  return $items;
+/**
+ * Load a specific template for viewing, as called through the menu wildcard
+ */
+function wysiwyg_template_html_load($tid){
+  $template = wysiwyg_template_load_template($tid);
+  return $template['body'];
 }
 
 /**
- * Implementation of hook_menu().
+ * Wysiwyg template database load function
  */
-function wysiwyg_template_admin_settings() {
-  $form = array();
+function wysiwyg_template_load_template($tid){
+  $templateResults = db_query("SELECT * FROM {wysiwyg_templates} WHERE tid = %d", $tid);
+  $template = db_fetch_object($templateResults);
+  $tItem = null;
+  if($template != null){
+    $tItem = array(
+      'title' => $template->title,
+      'description' => $template->description,
+      'body' => $template->body,
+      'tid' => $tid,
+    );
+  }
   
-  $form['wysiwyg_template_path'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Path to your js file'),
-    '#default_value' => variable_get('wysiwyg_template_path', 'sites/all/files'),
-    '#description' => t('defaults to sites/all/files'),
-  );
-  $form['wysiwyg_template_file'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Name of your js file'),
-    '#default_value' => variable_get('wysiwyg_template_file', 'templates.js'),
-    '#description' => t('defaults to templates.js'),
-  );
-  $form['info'] = array(
-    '#title' => t('Next Steps'),
-    '#value' => '<p>'.t('Create a file called ').variable_get('wysiwyg_template_file', 'template.js').t('(from above) and upload it to ').variable_get('wysiwyg_template_path', 'sites/all/files').t('(from above).  The file should define the names and locations of the HTML templates you would like to use in your WYSIWYG editor.  You may copy and paste the syntax from the README.txt file.').'</p>',
+  return $tItem;
+}
+
+/**
+ * Wysiwyg load all templates from the database
+ */
+function wysiwyg_template_load_all(){
+  $templateResults = db_query("SELECT * FROM {wysiwyg_templates}");
+  $templates = array();
+  while ($dbTemplate = db_fetch_object($templateResults)) {
+    $template = array(
+      'title' => $dbTemplate->title,
+      'description' => $dbTemplate->description,
+      'body' => $dbTemplate->body,
+      'tid' => $dbTemplate->tid,
+    );
+    $templates[] = $template;
+  }
+  return $templates;
+}
+
+/**
+ * Wysiwyg template database save function
+ */
+function wysiwyg_template_save_template($template){
+  $tItem = array(
+    'title' => $template['title'],
+    'description' => $template['description'],
+    'body' => $template['body'],
   );
   
-  return system_settings_form($form);
+  //if it's an existing item we're updating
+  $key = null;
+  if($template['tid']){
+    $key = 'tid';
+    $tItem['tid'] = $template['tid'];
+  }
+  
+  //save to database
+  return drupal_write_record('wysiwyg_templates', &$tItem, $key);
+}
+
+/**
+ * Wysiwyg template database delete function
+ */
+function wysiwyg_template_delete_template($tid){
+  db_query("DELETE FROM {wysiwyg_templates} WHERE tid = %s", $tid);
 }
