Index: web/src/main/webapp/skins/curriki8/groups/groupsmacros.vm
===================================================================
--- web/src/main/webapp/skins/curriki8/groups/groupsmacros.vm	(revision 21669)
+++ web/src/main/webapp/skins/curriki8/groups/groupsmacros.vm	(working copy)
@@ -98,6 +98,8 @@
 #groupsajaxjs()
 {pre}
 <script src="$xwiki.getSkinFile("Pork.Iframe.js")" type="text/javascript"></script>
+#set($wdoc = $xwiki.getDocument($spaceName, $pageName))
+#if($wdoc.syntaxId == "xwiki/1.0")
 <script type="text/javascript" src="$xwiki.getSkinFile("js/xwiki/wysiwyg/tinymce/tiny_mce/tiny_mce.js")"></script>
 <script type="text/javascript" src="$xwiki.getSkinFile("js/xwiki/wysiwyg/tinymce/wiki_editor/wiki_editor.js")"></script>
 <script type="text/javascript">
@@ -114,7 +116,17 @@
         wiki_images_path : '/xwiki/bin/download/${spaceName}/${pageName}/',
         wiki_attach_path : '/xwiki/bin/view/${spaceName}/${pageName}'
         });
+</script>
+#elseif($xwiki.wysiwyg.isSyntaxSupported($wdoc.syntaxId))
+## Import the JavaScript files required by the WYSIWYG editor (lazy=false).
+#wysiwyg_import(false)
+## Store the WYSIWYG editor configuration in a JavaScript variable which will be used later to create the editor.
+#wysiwyg_storeConfig("welcomeWysiwygConfig" $wdoc "XWiki.CurrikiWelcomeBlockClass_0_content" false)
+#end
+<script type="text/javascript">
 function editWelcomeBlock(spaceName,pageName,divid) {
+  // The user can click on the edit link even when the editor is loaded so it's better to release the existing editor.
+  releaseWysiwygEditor('welcomeWysiwyg');
   var pars = "space=" + spaceName + "&page=" + pageName + "&divid=" + divid + "&xpage=plain";
   $(divid).innerHTML = "<p>${msg.groups_loadinginprogress}</p>";
   // call url to get the edit html to edit the profile
@@ -123,7 +135,11 @@
 function editWelcomeBlockCallback(ajaxreq) {
  var divid = ajaxreq.options.divid;
  $(divid).innerHTML = ajaxreq.transport.responseText;
- tinyMCE.addMCEControl(document.getElementById("XWiki.CurrikiWelcomeBlockClass_0_content"), "XWiki.CurrikiWelcomeBlockClass_0_content");
+ if (typeof tinyMCE != 'undefined') {
+  tinyMCE.addMCEControl(document.getElementById("XWiki.CurrikiWelcomeBlockClass_0_content"), "XWiki.CurrikiWelcomeBlockClass_0_content");
+ } else {
+  createWysiwygEditor('welcomeWysiwyg', welcomeWysiwygConfig);
+ }
 }
 function cancelEditWelcomeBlock(spaceName,pageName,divid) {
  if (confirm('$msg.groups_welcomeblock_confirmcancel')) {
@@ -132,10 +148,15 @@
  #if($ua.indexOf("MSIE")!=-1) ##{
     history.go(0); 
  #else
-    var pars = "space=" + spaceName + "&page=" + pageName + "&divid=" + divid + "&xpage=plain";
-    $(divid).innerHTML = "<p>${msg.groups_loadinginprogress}</p>";
-    // call url to get the edit html to edit the profile
-    var myAjax = new Ajax.XWikiRequest( "Groups", "ViewWelcomeBlockService", {method: 'get', parameters: pars, onComplete: cancelEditWelcomeBlockCallback, divid: divid });
+    releaseWysiwygEditor('welcomeWysiwyg');
+    // We give the WYSIWYG editor a chance to execute its deferred updates before removing it from the document. This
+    // timeout shouldn't be necessary and will be removed in the future.
+    setTimeout(function() {
+     var pars = "space=" + spaceName + "&page=" + pageName + "&divid=" + divid + "&xpage=plain";
+     $(divid).innerHTML = "<p>${msg.groups_loadinginprogress}</p>";
+     // call url to get the edit html to edit the profile
+     var myAjax = new Ajax.XWikiRequest( "Groups", "ViewWelcomeBlockService", {method: 'get', parameters: pars, onComplete: cancelEditWelcomeBlockCallback, divid: divid });
+    }, 10);
  #end
  }
 }
@@ -143,6 +164,41 @@
  var divid = ajaxreq.options.divid;
  $(divid).innerHTML = ajaxreq.transport.responseText;
 }
+function beforeSubmitEditWelcomeBlock() {
+ if (typeof tinyMCE != 'undefined') {
+  document.forms['wblockform']['XWiki.CurrikiWelcomeBlockClass_0_content'].value = tinyMCE.getContent();
+ } else {
+  // We have to release the editor before submitting the form. The editor will be replaced by the response.
+  releaseWysiwygEditor('welcomeWysiwyg');
+ }
+}
+// With the new WYSIWYG editor it is possible to experience conversion exceptions (from HTML to wiki syntax) in which 
+// case we must reopen the editor to prevent the user from loosing unsaved changes.
+function afterSubmitEditWelcomeBlock(transport, divid) {
+ $(divid).innerHTML = transport.responseText;
+ // Check if the save failed. This test is not bullet proof: the user can add the specified form in the welcome block.
+ if (document.forms['wblockform']) {
+  createWysiwygEditor('welcomeWysiwyg', welcomeWysiwygConfig);
+ }
+}
+// Creates a new WYSIWYG editor based on the given configuration and stores it in a window property with the specified name.
+function createWysiwygEditor(name, config) {
+ if (typeof Wysiwyg != 'undefined') {
+  Wysiwyg.onModuleLoad(function() {
+   // We have to update the input URL because the user can edit/save multiple times without reloading the page.
+   // The input URL is taken from the edit form.
+   config.inputURL = document.getElementById('welcomeInputURL').value;
+   window[name] = new WysiwygEditor(config);
+  });
+ }
+}
+// Release the JavaScript objects and the event listeners used by the WYSIWYG editor to prevent memory leaks.
+function releaseWysiwygEditor(name) {
+ if (window[name]) {
+  window[name].release();
+  window[name] = undefined;
+ }
+}
 </script>
 {/pre}
 #begingroupsection($title,"$msg.groups_home_welcome_link","javascript:editWelcomeBlock('$spaceName','$pageName','groups-members-welcomeblock')", $colour, true)
@@ -193,18 +249,26 @@
 #end
 #set($ok = $doc.use("XWiki.CurrikiWelcomeBlockClass"))
 {pre}
-<form id="wblockform" action="$xwiki.getURL("Groups.SaveWelcomeBlockService")" method="post" onsubmit="new iframe(this,{update:'${divid}'}); return false;">
+<form id="wblockform" action="$xwiki.getURL("Groups.SaveWelcomeBlockService")" method="post" onsubmit="new iframe(this,{onComplete:function(t){afterSubmitEditWelcomeBlock(t,'${divid}')}}); return false;">
 {/pre}
 <p>
 <input type="hidden" name="xpage" value="plain" />
 <input type="hidden" name="space" value="$spaceName" />
 <input type="hidden" name="page" value="$pageName" />
+#if($xwiki.wysiwyg.isSyntaxSupported($wdoc.syntaxId))
+#wysiwyg_inputProperty($wdoc "XWiki.CurrikiWelcomeBlockClass_0_content")
+#wysiwyg_displayConversionError("XWiki.CurrikiWelcomeBlockClass_0_content")
+<input type="hidden" id="welcomeInputURL" value="${wdoc.getExternalURL("view", $wysiwyg_inputURLQueryString)}" disabled="disabled" />
+## In case of a conversion exception the conversion filter sould redirect us back to the edit form (by default it redirects
+## to the referer, which in our case is the page where the edit form is included).
+<input type="hidden" name="xerror" value="$!{request.requestURL}?$!{request.queryString}" />
+#end
 $wdoc.display("content", "edit")
 <br />
 {pre}
 <div class="right mgn-vrt-15">
 <input type="button" class="button button-cancel mgn-rt" value="$msg.get("groups_home_editwelcome_cancel_btt")" onclick="cancelEditWelcomeBlock('${spaceName}','${pageName}','${divid}');" />
-<input type="submit" class="button button-confirm" value="$msg.get("groups_home_editwelcome_submit_btt")" onclick="document.forms['wblockform']['XWiki.CurrikiWelcomeBlockClass_0_content'].value = tinyMCE.getContent();" />
+<input type="submit" class="button button-confirm" value="$msg.get("groups_home_editwelcome_submit_btt")" onclick="beforeSubmitEditWelcomeBlock();" />
 </div>
 {/pre}
 </p>
@@ -570,6 +634,13 @@
 #if($request.method=="POST")
   #set($newspace = $sm.createSpaceFromRequest("Groups_TemplateSpace"))
   #if($newspace)
+    #if("$!request.xwiki2" == "true")
+      ## Force xwiki/2.0 syntax on some pages from the newly created space in order to use the new WYSIWYG editor.
+      #set($wdoc = $xwiki.getDocument("${newspace.space}.WebHome"))
+      $wdoc.setSyntaxId("xwiki/2.0")
+      $wdoc.setContent("{{velocity}}{{html}}${wdoc.content}{{/html}}{{/velocity}}")
+      $wdoc.save("Force xwiki/2.0 syntax to be able to use the new WYSIWYG editor.")
+    #end
     ##$response.sendRedirect($xwiki.getURL("${newspace.space}.WebHome", "view", "firsttime=1"))
     #includeInContext("Groups.CreateNewGroupDone")
     #set($valid = 1)
@@ -591,6 +662,8 @@
   #end
   ##Show the inputs for form
   <form action="CreateNewGroup" class="CreateNewGroup" method="post">
+  ## Store the xwiki2 flag (=force xwiki/2.0 syntax) in an input hidden to prevent loosing it when the form is submitted.
+  <input type="hidden" name="xwiki2" value="$!request.xwiki2" />
   <h1>$msg.get("groups_creategroup_pagename")</h1>
   #titlebar($msg.get("groups_creategroup_about_title"), "", "", "blue")
   <p>
