--- wymeditor/wymeditor.module	2008-10-08 21:42:52.000000000 +0200
+++ wymeditor.new/wymeditor.module	2008-11-10 15:12:12.000000000 +0100
@@ -8,74 +8,92 @@
  */
 
 /**
+ * Implementation of hook_elements().
+ */
+function wymeditor_elements() {
+  return array( 'textarea' => array('#process' => array('wymeditor_textarea')),'radio'=>array('#process' => array('wymeditor_textarea')) );
+}
+
+/**
+ * Inline image/link insertion to textareas.
+ */
+function wymeditor_textarea($element) {
+    static $textarea;
+    if($element['#type']=='textarea')
+        $textarea = $element['#id'];
+    if($element['#type']=='radio' && $element['#title']=='WYMeditor') {
+        $element['#attributes']=array('class'=>'wymradio');
+        wymeditor_element($element['#id'],$textarea);
+    }
+    return $element;
+}
+
+/**
  * Implementation of hook_form_alter().
  */
-function wymeditor_form_alter(&$form, $form_state, $form_id) {
+function wymeditor_element($radio,$textarea) {
   global $user;
   global $language;
-
-  // Check if the form has an input format selection group
-  $format_types = _wymeditor_input_format_alter($form);
-
-  if ($key = array_search('WYMeditor', $format_types)) {
-    $rids = array();
-    $filename = array();
-    if (count($user->roles) == 1) {
-      foreach ($user->roles as $rid => $role) {
-        $filename[] = $rid;
-        $rids[] = $rid;
-      }
-    } 
-    else {
-      foreach ($user->roles as $rid => $role) {
-        $filename[] = $rid;
-        $rids[] = $rid;
-      }
+  $rids = array();
+  $filename = array();
+  if (count($user->roles) == 1) {
+    foreach ($user->roles as $rid => $role) {
+      $filename[] = $rid;
+      $rids[] = $rid;
     }
-
-    $module_path = drupal_get_path('module', 'wymeditor');
-    $settings = $buttons = $plugins = array();
-    foreach ($rids as $rid) {
-      $wym_profiles = _wymeditor_get_profiles($rid);
-      $settings[] = unserialize($wym_profiles[0]['settings']);
-    }
-    foreach ($settings as $setting) {
-      if ($setting['button']['enable']) {
-        $button_enable = 1;
-        foreach ($setting['button']['buttons'] as $button => $value) {
+  } 
+  else {
+    foreach ($user->roles as $rid => $role) {
+      $filename[] = $rid;
+      $rids[] = $rid;
+    }
+  }
+  $module_path = drupal_get_path('module', 'wymeditor');
+  $settings = $buttons = $plugins = array();
+  foreach ($rids as $rid) {
+    $wym_profiles = _wymeditor_get_profiles($rid);
+    $settings[] = unserialize($wym_profiles[0]['settings']);
+  }
+  foreach ($settings as $setting) {
+    if ($setting['button']['enable']) {
+       $button_enable = 1;
+       foreach ($setting['button']['buttons'] as $button => $value) {
           if ($value == $button) $buttons[$value] = $value;
-        }
-      }
-      foreach ($setting['plugin']['plugins'] as $plugin => $value) {
-        if ($plugin === $value && !is_numeric($value)) $plugins[$plugin] = $plugin;
-      }
+       }
     }
-    if (!file_exists(file_directory_path() .'/js/jquery.wymeditor.init.'. implode('-', $filename) .'.js')) {
-      $js_file = wymeditor_javascript($button_enable, $buttons, $plugins, $filename);
-    } 
-    else {
-      $js_file = file_directory_path() .'/js/jquery.wymeditor.init.'. implode('-', $filename) .'.js';
-    }
-    // add this file for run after the filters-format.js
-    drupal_add_js('misc/textarea.js');
-    // Include the javascript and css files
-    drupal_add_css($module_path .'/wymeditor/skins/default/skin.css');
-    // add the css for format the filter format
-    drupal_add_css($module_path .'/wymeditor.css');
-    drupal_add_js($module_path .'/wymeditor/jquery.wymeditor.js');
-    drupal_add_js($module_path .'/wymeditor/jquery.wymeditor.pack.js');
-    drupal_add_js($module_path .'/wymeditor/lang/'. $language->language .'.js');
-
-    if (count($plugins) > 0) {
-      foreach ($plugins as $plugin) {
-        drupal_add_js($module_path .'/wymeditor/plugins/'. $plugin .'/jquery.wymeditor.'. $plugin .'.js');
-      }
+    foreach ($setting['plugin']['plugins'] as $plugin => $value) {
+      if ($plugin === $value && !is_numeric($value)) $plugins[$plugin] = $plugin;
     }
-    drupal_add_js($js_file);
-    drupal_add_js($module_path .'/filters-format.js');
-    drupal_add_js(array('wymeditor_key' => array($key), 'wymeditor_path' => array(url(drupal_get_path('module', 'wymeditor')))), 'setting');
   }
+  if (!file_exists(file_directory_path() .'/js/jquery.wymeditor.init.'. implode('-', $filename) .'.js')) {
+    $js_file = wymeditor_javascript($button_enable, $buttons, $plugins, $filename);
+  } 
+  else {
+   $js_file = file_directory_path() .'/js/jquery.wymeditor.init.'. implode('-', $filename) .'.js';
+  }
+  // add this file for run after the filters-format.js
+  drupal_add_js('misc/textarea.js');
+  // Include the javascript and css files
+  drupal_add_css($module_path .'/wymeditor/skins/default/skin.css');
+  // add the css for format the filter format
+  drupal_add_css($module_path .'/wymeditor.css');
+  drupal_add_js($module_path .'/wymeditor/jquery.wymeditor.js');
+  drupal_add_js($module_path .'/wymeditor/jquery.wymeditor.pack.js');
+  drupal_add_js($module_path .'/wymeditor/lang/'. $language->language .'.js');
+  if (count($plugins) > 0) {
+    foreach ($plugins as $plugin) {
+      drupal_add_js($module_path .'/wymeditor/plugins/'. $plugin .'/jquery.wymeditor.'. $plugin .'.js');
+    }
+  }
+  drupal_add_js($js_file);
+  drupal_add_js($module_path .'/filters-format.js');
+  drupal_add_js(array('wymeditor_radio' => array($radio=>$textarea)), 'setting');
+}
 
+/**
+ * Implementation of hook_form_alter().
+ */
+function wymeditor_form_alter(&$form, $form_state, $form_id) {
   // Alter filter format
   if ($form_id == 'filter_admin_format_form' && $form['name']['#default_value'] == 'WYMeditor') {
     $form['name']['#attributes'] = array('readonly' => TRUE);
@@ -139,41 +157,6 @@
 }
 
 /**
- * Helper; Alter the input format so it can be accessed by the jquery system
- */
-function _wymeditor_input_format_alter(&$form) {
-  if ($format_types == NULL) {
-    static $format_types = array();
-  }
-
-  if (is_array($form)) {
-    foreach (element_children($form) as $key) {
-      if (is_array($form[$key]['format'])) {
-        // The common point for the filter system is the declaration of the function
-        // 'filter_form_validate' on the '#validate' atribute of Form API
-        if (is_array($form[$key]['format']['#element_validate']) && in_array('filter_form_validate', $form[$key]['format']['#element_validate'])) {
-          $form[$key]['format']['#attributes'] = array('class' => 'filter-formats');
-          if (!$format_types) {
-            $formats = array();
-            foreach ($form[$key]['format'] as $key => $e) {
-              if ($e['#type'] == 'radio' && $e['#parents'][0] == 'format') {
-                $formats[$key] = $e['#title'];
-              }
-            }
-            $format_types = $formats;
-          }
-        }
-      }
-
-      // This function is recursive to find all input formats groups
-      _wymeditor_input_format_alter(&$form[$key]);
-    }
-  }
-
-  return $format_types;
-}
-
-/**
  * Implementation of hook_nodeapi
  */
 function wymeditor_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
@@ -181,8 +164,9 @@
     case 'presave':
       if ($node->files) {
         $path = file_directory_path();
-        foreach ($node->files as $file) {
-          $node->body = str_replace(file_create_url(substr($file['filepath'], strrpos($file['filepath'], 'temp'))), file_create_url($path .'/'. $file['filename']), $node->body);
+        $files = $node->files;
+        foreach ($files as $file) {
+          $node->body = str_replace(file_create_url(substr($file->filepath, strrpos($file->filepath, 'temp'))), file_create_url($path .'/'. $file->filename), $node->body);
         }
       }
       break;
@@ -419,6 +403,12 @@
       + "</div>",';
   }
 
+ if (module_exists('imce')) {
+    $link = '+ "<p style=\"width:100%;height: 1.5em;\">"
+       + "<a href=\"javascript:;\" style=\"position: absolute;right:0;\" onclick=\"window.open(\'/?q=imce&app=WymEditor|url@image_src\',\'\', \'width=760,height=560,resizable=1\');\">IMCE</a>"
+       + "</p>"';
+  }
+
   $filepath = file_directory_path() .'/js';
   $module_path = drupal_get_path('module', 'wymeditor');
   $js_file = $filepath .'/jquery.wymeditor.init.'. implode('-', $filename) .'.js';
@@ -431,8 +421,9 @@
     .'dialogImageHtml: "<script type=\'text/javascript\' src=\''. url($module_path) .'/wymeditor-image.js\'></script>"
        + "<body class=\'wym_dialog wym_dialog_image\'"
        + " onload=\'WYMeditor.INIT_DIALOG(" + WYMeditor.INDEX + ")\'"
-       + ">"
-       + "<form>"
+       + ">"'
+       .$link
+       .'+ "<form>"
        + "<fieldset>"
        + "<input type=\'hidden\' class=\'wym_dialog_type\' value=\'"
        + WYMeditor.DIALOG_IMAGE
@@ -440,7 +431,7 @@
        + "<legend>{Image}</legend>"
        + "<div class=\'row\'>"
        + "<label>{URL}</label>"
-       + "<input type=\'text\' class=\'wym_src\' value=\'\' size=\'40\' />"
+       + "<input type=\'text\' id=\'image_src\' class=\'wym_src\' value=\'\' size=\'40\' />"
        + "</div>"
        + "<div class=\'row\'>"
        + "<label>{Alternative_Text}</label>"
--- wymeditor/filters-format.js	2008-09-21 16:38:39.000000000 +0200
+++ wymeditor.new/filters-format.js	2008-11-10 14:12:44.000000000 +0100
@@ -1,70 +1,32 @@
 Drupal.filterFormatLoader = function() {
-  // prepare the new input formats scheme
-  $('.filter-formats .form-item input[@type=radio][@checked]').each( function() {
-    var filter_format = $(this).parents('.filter-formats');//.prevAll(":first");
-    var form_item = $(filter_format).parent();
-    var textareas = $(form_item).find('textarea');
-    if (textareas[0].id=='edit-teaser-js') {
-	    textarea = textareas[1];
-    } else {
-	    textarea = textareas[0];
-    }
-
-    // change the input format HTML
-    var selected = $(this).parent().text();
-    var legend = $(this).parents('.filter-formats').children('legend');
-    var fieldset = $(this).parents('.filter-formats');
-    var img = Drupal.settings.wymeditor_path[0];
-    var key = Drupal.settings.wymeditor_key[0];
-
-    $(filter_format).after('<div class="filter-formats"><a class="link-open" href="#"><img src="'+ img +'/filter-icon.jpg" alt="" /><span>'+ $(legend).text() +'</span></a><span class="active">'+ selected +'</span></div><div class="filter-formats-box"></div>');
-    $(legend).remove();
-    $(form_item).find('.filter-formats-box').append($(fieldset).children());
-    $(form_item).find('.filter-formats-box').hide();
-    $(fieldset).remove();
-
-    // initialize the WYMeditor if it is active
-    if ($(this).attr('value') == key) {
-      $(textarea).wymeditor($.wymeditor_config);
-      $(textarea).fadeOut();
-      $(form_item).parent().find('.grippie').fadeOut();
-    } else {
-      $(form_item).parent().find('.filter-formats').css('margin-right', $(form_item).find('.grippie').css('margin-right'));
-    }
-  });
-
-  // function to show/hide the new input formats box
-  $('.filter-formats .link-open').click( function() {
-    var filter_box = $(this).parents('div')[1];
-    $(filter_box).find('.link-open').toggleClass('open');
-    $(filter_box).find('.filter-formats-box').slideToggle('slow');
-    return false;
-  });
+  // prepare the new input formats scheme
+  $('.wymradio').each( function() {
+        $(this).parents('fieldset').addClass('wym_fieldset');
+        if(this.checked) {
+            var radio_id = this.id;
+            var textarea_id = Drupal.settings.wymeditor_radio[radio_id];
+            var textarea = $('#'+textarea_id);
+            textarea.wymeditor($.wymeditor_config);
+            textarea.fadeOut();
+        }
+  });
 
   // function to allow the change of the active editor
-  $('.filter-formats-box .form-item input[@type=radio]').change( function() {
-    var form_item = $(this).parents('.filter-formats-box').parent();
-    var textarea = $(form_item).find('textarea')[1];
-
-    var selected = $(this).parent().text();
-    $(form_item).find('.active').html(selected);
-
-    var key = Drupal.settings.wymeditor_key;
-    if ($(this).attr('value') == key) {
-      $(textarea).fadeOut();
-      $(form_item).find('.grippie').fadeOut();
-      $(form_item).find('.filter-formats').css('margin-right', '0');
-      $(textarea).wymeditor($.wymeditor_config);
-    } else {
-      $(textarea).fadeIn();
-      $(form_item).find('.grippie').fadeIn();
-      $(form_item).find('.filter-formats').css('margin-right', $(form_item).find('.grippie').css('margin-right'));
-      $(form_item).find('.wym_box').remove();
-    }
-    $(this).parents('.filter-formats-box').slideUp('slow');
-  });
-};
+  $('.wym_fieldset input[@type=radio]').change( function() {
+        var fieldset = $(this).parents('fieldset');
+        var wym_radio = $(fieldset).find('.wymradio')[0];
+        var textarea_id = Drupal.settings.wymeditor_radio[wym_radio.id];
+        var textarea = $('#'+textarea_id);
+        if(wym_radio.checked) {
+            textarea.wymeditor($.wymeditor_config);
+            textarea.fadeOut();
+        } else {
+            textarea.fadeIn();
+            $('#'+textarea_id+' + .wym_box').remove();
+       }
+  });
+};
 
 if (Drupal.jsEnabled) {
-  $(document).ready(Drupal.filterFormatLoader);
-};
+  $(document).ready(Drupal.filterFormatLoader);
+};
