1function getElementsByClassName(className, tag, elm){
2	var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
3	tag = tag || "*";
4	elm = elm || document;
5	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
6	var returnElements = [];
7	var current;
8	var length = elements.length;
9	for(var i=0; i<length; i++){
10		current = elements[i];
11		if(testClass.test(current.className)){
12			returnElements.push(current);
13		}
14	}
15	return returnElements;
16}
17function wysiwyg_hide(id) {
18    /* hide that id */
19    el = document.getElementById(id);
20    newClass = el.className;
21    alreadyHas = /wysiwyg_hidden/.test(newClass);
22    if (alreadyHas) { return; }
23    newClass += " wysiwyg_hidden";
24    el.className = newClass;
25    return;
26}
27
28function wysiwyg_show(id) {
29    /* show that id */
30    el = document.getElementById(id);
31    newClass = el.className;
32    newClass = newClass.replace(/ wysiwyg_hidden/, "");
33    newClass = newClass.replace(/wysiwyg_hidden/, "");
34    newClass = newClass.replace(/wysiwyg_initially_hidden/, "");
35    el.className = newClass;
36    return;
37}
38function __wysiwyg_showEdit()
39{
40    buttons = getElementsByClassName("wysiwyg_edit_button");
41    for(i=0;i<buttons.length;++i) {
42        button = buttons[i];
43        newClass = button.className;
44        newClass = newClass.replace(/ wysiwyg_hidden/, "");
45        newClass = newClass.replace(/wysiwyg_hidden/, "");
46        button.className = newClass;
47    }
48}
49function __wysiwyg_hideEdit()
50{
51    buttons = getElementsByClassName("wysiwyg_edit_button");
52    for(i=0;i<buttons.length;++i) {
53        button = buttons[i];
54        newClass = button.className;
55        newClass += " wysiwyg_hidden";
56        button.className = newClass;
57    }
58}
59function _wysiwyg_showEditButtons(ajax) {
60    success = ajax.response;
61    if (success == 'success') {
62        __wysiwyg_showEdit();
63    }
64    return;
65}
66function wysiwyg_editButtons() {
67    var ajax = new sack(DOKU_BASE + 'lib/plugins/wysiwyg/ajax.php');
68    ajax.encodeURIString = false;
69    ajax.setVar('action','checkPerms');
70    ajax.setVar('id',ID);
71    ajax.onCompletion = function() { _wysiwyg_showEditButtons(ajax); };
72    ajax.runAJAX();
73    return;
74}
75
76var wysiwyg_pending = Array;
77var wysiwyg_has_inited = false;
78var wysiwyg_oldBodyPadding = "0px";
79
80function wysiwyg_show_view(name) {
81    wysiwyg_hide("wysiwyg_editor_"+name);
82    wysiwyg_hide("wysiwyg_adobe_style_toolbar");
83    wysiwyg_show("wysiwyg_view_"+name);
84    document.body.style.paddingTop = wysiwyg_oldBodyPadding;
85    return;
86}
87
88function wysiwyg_show_editor(name) {
89    wysiwyg_hide('wysiwyg_view_'+name);
90    wysiwyg_show("wysiwyg_adobe_style_toolbar");
91    wysiwyg_show('wysiwyg_editor_'+name);
92    wysiwyg_oldBodyPadding = document.body.style.paddingTop;
93    // <IE7 lacks display:fixed
94    if (!((navigator.appVersion.indexOf('MSIE 5')>-1)||(navigator.appVersion.indexOf('MSIE 6')>-1)) )
95        { document.body.style.paddingTop = "80px"; }
96    // gecko sets this to 0 if initially display-ed: none;
97    toolbar = document.getElementById('wysiwyg_adobe_style_toolbar');
98    toolChild = toolbar.firstChild;
99    if (toolChild.nextSibling != null) { toolChild.nextSibling.height = 76; }
100    else { toolChild.height = 76; }
101    // set focus
102    oEditor = FCKeditorAPI.GetInstance(name);
103    try {
104        oEditor.MakeEditable();
105    }
106    catch (e) {
107    }
108    oEditor.Focus();
109    return;
110}
111
112function wysiwyg_confirm_save(name) {
113    var confirmed = true;
114    // confirmed = confirm("Overwrite?");
115    return confirmed;
116}
117
118/* called by the quit button */
119function wysiwyg_quit(name) {
120    oEditor = FCKeditorAPI.GetInstance(name);
121    var confirmed = true;
122    if ( oEditor.IsDirty() ) { confirmed = confirm("You will lose your changes."); }
123    if (!confirmed) { return false; }
124    var ajax = new sack(DOKU_BASE + 'lib/plugins/wysiwyg/ajax.php');
125    ajax.encodeURIString = false;
126    ajax.setVar('action','quit');
127    ajax.setVar('name',name);
128    ajax.setVar('id',ID);
129    ajax.runAJAX();
130
131    var html = wysiwyg_pending[name];
132    oEditor.SetHTML(html);
133    wysiwyg_show_view(name);
134    __wysiwyg_showEdit();
135    return true;
136}
137
138function wysiwyg_getArgs(  ) {
139    var args = new Object(  );
140    var query = location.search.substring(1);
141    var pairs = query.split("&");
142    for(var i = 0; i < pairs.length; i++) {
143        var pos = pairs[i].indexOf('=');
144        if (pos == -1) { continue; }
145        var argname = pairs[i].substring(0,pos);
146        var value = pairs[i].substring(pos+1);
147        args[argname] = unescape(value);
148    }
149    return args;     // Return the object
150}
151function wysiwyg_save_complete(name) {
152    wysiwyg_show_view(name);
153    __wysiwyg_showEdit();
154    return;
155}
156function wysiwyg_send_to_page(html, name) {
157    html = '<div class="wysiwyg_view_' + name + ' wysiwyg_view">' + html;
158    html += '<a onmouseover="wysiwyg_highlight(\'' +name +'\');" onmouseout="wysiwyg_unhighlight(\'' +name +'\');" onclick="wysiwyg_edit(\'' + name + '\');" class="wysiwyg_button">edit</a></div>';
159    page_blocks = getElementsByClass('wysiwyg_view_' + name);
160    for (i=0;i<page_blocks.length;i++) {
161        page_blocks[i].innerHTML = html;
162    }
163    return true;
164}
165
166function wysiwyg_send_to_server(html, name) {
167    var ajax = new sack(DOKU_BASE + 'lib/plugins/wysiwyg/ajax.php');
168    wysiwyg_send_to_page(html, name);
169    ajax.encodeURIString = false;
170    html = escape(html);
171    ajax.setVar('action','save');
172    ajax.setVar('name',name);
173    ajax.setVar('html',html);
174    ajax.setVar('id',ID);
175    ajax.onCompletion = function() { wysiwyg_save_complete(name, ajax); };
176    ajax.runAJAX();
177}
178
179/* called by the save button */
180function wysiwyg_save(name) {
181    if(!wysiwyg_confirm_save(name)) { return false; }
182    var oEditor = FCKeditorAPI.GetInstance(name);
183    var html = oEditor.GetXHTML();
184    wysiwyg_send_to_server(html, name);
185    // disable oEditor.
186    return false;
187}
188function _wysiwyg_edit(name, ajax) {
189    success = ajax.response;
190    if (success != 'success') {
191        if (success == 'noperms') {
192            alert("You don't have permission to edit this page.");
193        }
194        else if(success.substr(0,6) == 'locked') {
195            alert(success.substr(7) +" is already editing this page.");
196        }
197        else {
198            alert("Some unknown error. Annoy your system admin.");
199        }
200        return;
201    }
202    __wysiwyg_hideEdit();
203    oEditor = FCKeditorAPI.GetInstance(name);
204    var html = oEditor.GetXHTML();
205    wysiwyg_pending[name] = html;
206
207    // show name editor
208    wysiwyg_show_editor(name);
209}
210/* called by the edit button */
211function wysiwyg_edit(name) {
212    var ajax = new sack(DOKU_BASE + 'lib/plugins/wysiwyg/ajax.php');
213    ajax.encodeURIString = false;
214    ajax.setVar('action','edit');
215    ajax.setVar('id',ID);
216    ajax.onCompletion = function() { _wysiwyg_edit(name, ajax); };
217    ajax.runAJAX();
218}
219// called when FCKeditor is done starting..
220function FCKeditor_OnComplete( editorInstance ){
221    //editorInstance.EditorDocument.designMode = "on";
222    if (!wysiwyg_has_inited) {
223        wysiwyg_editButtons();
224        wysiwyg_has_inited = true;
225    }
226    return true;
227}
228
229function _wysiwyg_has (name) {
230    for(i=0;i<__wysiwyg_started.length;i++) {
231        if (__wysiwyg_started[i] == name) { return true; }
232    }
233    return false;
234}
235
236function wysiwyg_highlight(name) {
237    var theDiv = "wysiwyg_view_"+name;
238    el = document.getElementById(theDiv);
239    newClass = el.className;
240    alreadyHas = /wysiwyg_highlighted/.test(newClass);
241    if (alreadyHas) { return; }
242    newClass += " wysiwyg_highlighted";
243    el.className = newClass;
244    return;
245}
246
247function wysiwyg_unhighlight(name) {
248    el = document.getElementById("wysiwyg_view_"+name);
249    newClass = el.className;
250    newClass = newClass.replace(/ wysiwyg_highlighted/, "");
251    newClass = newClass.replace(/wysiwyg_highlighted/, "");
252    el.className = newClass;
253    return;
254}
255