xref: /dokuwiki/lib/scripts/media.js (revision 96a38b8b34ac7ce3f06ecf51c296dbf133a2f558)
1/**
2 * JavaScript functionality for the media management popup
3 *
4 * @author Andreas Gohr <andi@splitbrain.org>
5 * @author Pierre Spring <pierre.spring@caillou.ch>
6 */
7
8var dw_mediamanager = {
9    keepopen: false,
10    hide: false,
11    popup: false,
12    display: false,
13    ext: false,
14    $popup: null,
15
16    // Image insertion opts
17    align: false,
18    link: false,
19    size: false,
20    forbidden_opts: {},
21
22    // File list view type
23    view: false,
24
25    layout_width: 0,
26    layout_height: 0,
27
28    init: function () {
29        var $content, $tree;
30        $content = jQuery('#media__content');
31        $tree    = jQuery('#media__tree');
32
33        dw_mediamanager.prepare_content($content);
34
35        dw_mediamanager.attachoptions();
36        dw_mediamanager.initpopup();
37
38        // add the action to autofill the "upload as" field
39        $content.delegate('#upload__file', 'change', dw_mediamanager.suggest)
40                // Attach the image selector action to all links
41                .delegate('a.select', 'click', dw_mediamanager.select)
42                // Attach deletion confirmation dialog to the delete buttons
43                .delegate('#media__content a.btn_media_delete', 'click',
44                          dw_mediamanager.confirmattach)
45                .delegate('#mediamanager__done_form', 'submit', dw_mediamanager.list);
46
47        $tree.dw_tree({toggle_selector: 'img',
48                       load_data: function (show_sublist, $clicky) {
49                           // get the enclosed link (is always the first one)
50                           var $link = $clicky.parent().find('div.li a.idx_dir');
51
52                           jQuery.post(
53                               DOKU_BASE + 'lib/exe/ajax.php',
54                               $link[0].search.substr(1) + '&call=medians',
55                               show_sublist,
56                               'html'
57                           );
58                       },
59
60                       toggle_display: function ($clicky, opening) {
61                           $clicky.attr('src',
62                                        DOKU_BASE + 'lib/images/' +
63                                        (opening ? 'minus' : 'plus') + '.gif');
64                       }});
65        $tree.delegate('a', 'click', dw_mediamanager.list);
66
67        dw_mediamanager.set_filelist_view(dw_mediamanager.view, false);
68        jQuery('#mediamanager__form_sort').find('input[type=submit]').hide();
69        dw_mediamanager.image_diff();
70        dw_mediamanager.init_ajax_uploader();
71
72        // changing opened tab in the file list panel
73        jQuery('#mediamanager__layout_list').delegate('#mediamanager__tabs_files a', 'click', dw_mediamanager.list)
74            // changing type of the file list view
75            .delegate('#mediamanager__tabs_list a', 'click', dw_mediamanager.list_view)
76            // loading file details
77            .delegate('#mediamanager__file_list a', 'click', dw_mediamanager.details)
78            // search form
79            .delegate('#dw__mediasearch', 'submit', dw_mediamanager.list)
80            // "upload as" field autofill
81            .delegate('#upload__file', 'change', dw_mediamanager.suggest)
82            // sort type selection
83            .delegate('#mediamanager__form_sort select', 'change', dw_mediamanager.list)
84            // uploaded images
85            .delegate('.qq-upload-file a', 'click', dw_mediamanager.details);
86
87        // changing opened tab in the file details panel
88        jQuery('#mediamanager__layout_detail').delegate('#mediamanager__tabs_details a', 'click', dw_mediamanager.details)
89            // "update new version" button
90            .delegate('#mediamanager__btn_update', 'submit', dw_mediamanager.list)
91            // revisions form
92            .delegate('#page__revisions', 'submit', dw_mediamanager.details)
93            .delegate('#page__revisions a', 'click', dw_mediamanager.details)
94            // meta edit form
95            .delegate('#mediamanager__save_meta', 'submit', dw_mediamanager.details)
96            // delete button
97            .delegate('#mediamanager__btn_delete', 'submit', dw_mediamanager.details)
98            // "restore this version" button
99            .delegate('#mediamanager__btn_restore', 'submit', dw_mediamanager.details)
100            // less/more recent buttons in media revisions form
101            .delegate('.btn_newer, .btn_older', 'submit', dw_mediamanager.details);
102
103    },
104
105    /**
106     * build the popup window
107     *
108     * @author Dominik Eckelmann <eckelmann@cosmocode.de>
109     */
110    initpopup: function () {
111        var opts, $insp, $insbtn;
112
113        dw_mediamanager.$popup = jQuery(document.createElement('div'))
114                 .attr('id', 'media__popup_content')
115                 .dialog({autoOpen: false, width: 280, modal: true,
116                          draggable: true, title: LANG.mediatitle,
117                          resizable: false});
118
119        opts = [{id: 'link', label: LANG.mediatarget,
120                 btns: ['lnk', 'direct', 'nolnk', 'displaylnk']},
121                {id: 'align', label: LANG.mediaalign,
122                 btns: ['noalign', 'left', 'center', 'right']},
123                {id: 'size', label: LANG.mediasize,
124                 btns: ['small', 'medium', 'large', 'original']}
125               ];
126
127        jQuery.each(opts, function (_, opt) {
128            var $p, $l;
129            $p = jQuery(document.createElement('p'))
130                 .attr('id', 'media__' + opt.id);
131
132            if (dw_mediamanager.display === "2") {
133                $p.hide();
134            }
135
136            $l = jQuery(document.createElement('label'))
137                 .text(opt.label);
138            $p.append($l);
139
140            jQuery.each(opt.btns, function (i, text) {
141                var $btn, $img;
142                $btn = jQuery(document.createElement('button'))
143                       .addClass('button')
144                       .attr('id', "media__" + opt.id + "btn" + (i + 1))
145                       .attr('title', LANG['media' + text])
146                       .click(bind(dw_mediamanager.setOpt, opt.id));
147
148                $img = jQuery(document.createElement('img'))
149                       .attr('src', DOKU_BASE + 'lib/images/media_' +
150                                    opt.id + '_' + text + '.png');
151
152                $btn.append($img);
153                $p.append($btn);
154            });
155
156            dw_mediamanager.$popup.append($p);
157        });
158
159        // insert button
160        $insp = jQuery(document.createElement('p'));
161        dw_mediamanager.$popup.append($insp);
162
163        $insbtn = jQuery(document.createElement('input'))
164                  .attr('id', 'media__sendbtn')
165                  .attr('type', 'button')
166                  .addClass('button')
167                  .val(LANG.mediainsert);
168        $insp.append($insbtn);
169    },
170
171    /**
172     * Insert the clicked image into the opener's textarea
173     *
174     * @author Andreas Gohr <andi@splitbrain.org>
175     * @author Dominik Eckelmann <eckelmann@cosmocode.de>
176     * @author Pierre Spring <pierre.spring@caillou.ch>
177     */
178    insert: function (id) {
179        var opts, alignleft, alignright, edid, s;
180
181        // set syntax options
182        dw_mediamanager.$popup.dialog('close');
183
184        opts = '';
185        alignleft = '';
186        alignright = '';
187
188        if ({img: 1, swf: 1}[dw_mediamanager.ext] === 1) {
189
190            if (dw_mediamanager.link === '4') {
191                    opts = '?linkonly';
192            } else {
193
194                if (dw_mediamanager.link === "3" && dw_mediamanager.ext === 'img') {
195                    opts = '?nolink';
196                } else if (dw_mediamanager.link === "2" && dw_mediamanager.ext === 'img') {
197                    opts = '?direct';
198                }
199
200                s = parseInt(dw_mediamanager.size, 10);
201
202                if (s && s >= 1 && s < 4) {
203                    opts += (opts.length)?'&':'?';
204                    opts += dw_mediamanager.size + '00';
205                    if (dw_mediamanager.ext === 'swf') {
206                        switch (s) {
207                        case 1:
208                            opts += 'x62';
209                            break;
210                        case 2:
211                            opts += 'x123';
212                            break;
213                        case 3:
214                            opts += 'x185';
215                            break;
216                        }
217                    }
218                }
219                alignleft = dw_mediamanager.align === '2' ? '' : ' ';
220                alignright = dw_mediamanager.align === '4' ? '' : ' ';
221            }
222        }
223        edid = String.prototype.match.call(document.location, /&edid=([^&]+)/);
224        opener.insertTags(edid ? edid[1] : 'wiki__text',
225                          '{{'+alignleft+id+opts+alignright+'|','}}','');
226
227        if(!dw_mediamanager.keepopen) {
228            window.close();
229        }
230        opener.focus();
231        return false;
232    },
233
234    /**
235     * Prefills the wikiname.
236     *
237     * @author Andreas Gohr <andi@splitbrain.org>
238     */
239    suggest: function(){
240        var $file, $name, text;
241
242        $file = jQuery(this);
243        $name = jQuery('#upload__name');
244
245        if ($name.val() != '') return;
246
247        if(!$file.length || !$name.length) {
248            return;
249        }
250
251        text = $file.val();
252        text = text.substr(text.lastIndexOf('/')+1);
253        text = text.substr(text.lastIndexOf('\\')+1);
254        $name.val(text);
255    },
256
257    /**
258     * list the content of a namespace using AJAX
259     *
260     * @author Andreas Gohr <andi@splitbrain.org>
261     * @author Pierre Spring <pierre.spring@caillou.ch>
262     */
263    list: function (event) {
264        var $link, $content, params;
265        $link = jQuery(this);
266
267        event.preventDefault();
268
269        jQuery('div.success, div.info, div.error, div.notify').remove();
270
271        if (document.getElementById('media__content')) {
272            //popup
273            $content = jQuery('#media__content');
274        } else {
275            //fullscreen media manager
276            $content = jQuery('#mediamanager__layout_list');
277
278            if ($link.hasClass('idx_dir')) {
279                //changing namespace
280                jQuery('#mediamanager__layout_detail').empty();
281                jQuery('#media__tree .selected').each(function(){
282                    jQuery(this).removeClass('selected');
283                });
284                $link.addClass('selected');
285            }
286        }
287
288        params = '';
289
290        if ($link[0].search) {
291            params = $link[0].search.substr(1)+'&call=medialist';
292        } else if ($link[0].action) {
293            params = dw_mediamanager.form_params($link)+'&call=medialist';
294        } else if ($link.parents('form')) {
295            params = dw_mediamanager.form_params($link.parents('form'))+'&call=medialist';
296
297            if ($link.parents('form')[0].id == 'mediamanager__form_sort') {
298                DokuCookie.setValue('sort', $link[0].value);
299                params += '&q=' + jQuery('#mediamanager__sort_textfield').val();
300                params += '&mediado=searchlist';
301            }
302        }
303
304        // fetch the subtree
305        dw_mediamanager.update_content($content, params);
306
307        if (document.getElementById('media__content')) {
308            //popup
309            $content = jQuery('#media__content');
310            $content.html('<img src="' + DOKU_BASE + 'lib/images/loading.gif" alt="..." class="load" />');
311        } else {
312            //fullscreen media manager
313            jQuery('.scroll-container', $content).html('<img src="' + DOKU_BASE + 'lib/images/loading.gif" alt="..." class="load" />');
314        }
315    },
316
317     /**
318     * Returns form parameters
319     *
320     * @author Kate Arzamastseva <pshns@ukr.net>
321     */
322    form_params: function ($form) {
323        if (!$form.length) return;
324        var elements = $form.serialize();
325        var action = '';
326        var i = $form[0].action.indexOf('?');
327        if (i >= 0) action = $form[0].action.substr(i+1);
328        return elements+'&'+action;
329    },
330
331     /**
332     * Changes view of media files list
333     *
334     * @author Kate Arzamastseva <pshns@ukr.net>
335     */
336    list_view: function (event) {
337        var $link, $content;
338        $link = jQuery(this);
339
340        event.preventDefault();
341
342        $content = jQuery('#mediamanager__file_list');
343
344        if ($link[0].id == 'mediamanager__link_thumbs') {
345            dw_mediamanager.set_filelist_view('thumbs', true);
346
347        } else if ($link[0].id == 'mediamanager__link_list') {
348            dw_mediamanager.set_filelist_view('list', true);
349        }
350    },
351
352    set_filelist_view: function (type, cookies) {
353        var $content = jQuery('#mediamanager__file_list');
354        if (!type) type = DokuCookie.getValue('view');
355
356        if (type == 'thumbs') {
357            $content.removeClass('mediamanager-list');
358            $content.addClass('mediamanager-thumbs');
359            if (cookies) DokuCookie.setValue('view', 'thumbs');
360            dw_mediamanager.view = 'thumbs';
361
362        } else if (type == 'list') {
363            $content.removeClass('mediamanager-thumbs');
364            $content.addClass('mediamanager-list');
365            if (cookies) DokuCookie.setValue('view', 'list');
366            dw_mediamanager.view = 'list';
367        }
368    },
369
370     /**
371     * Lists the content of the right column (image details) using AJAX
372     *
373     * @author Kate Arzamastseva <pshns@ukr.net>
374     */
375    details: function (event) {
376        var $link, $content, params, update_list;
377        $link = jQuery(this);
378
379        event.preventDefault();
380
381        jQuery('div.success, div.info, div.error, div.notify').remove();
382
383        if ($link[0].id == 'mediamanager__btn_delete' && !confirm(LANG['del_confirm'])) return false;
384        if ($link[0].id == 'mediamanager__btn_restore' && !confirm(LANG['restore_confirm'])) return false;
385
386        $content = jQuery('#mediamanager__layout_detail');
387        params = '';
388
389        if ($link[0].search) {
390            params = $link[0].search.substr(1)+'&call=mediadetails';
391        } else if ($link[0].action) {
392            params = dw_mediamanager.form_params($link)+'&call=mediadetails';
393        } else if ($link.parents('form')) {
394            params = dw_mediamanager.form_params($link.parents('form'))+'&call=mediadetails';
395        }
396
397        update_list = ($link[0].id == 'mediamanager__btn_delete' || $link[0].id == 'mediamanager__btn_restore');
398        dw_mediamanager.update_content($content, params, update_list);
399
400        if (jQuery('.scroll-container', $content).length) {
401            jQuery('.scroll-container', $content).html('<img src="' + DOKU_BASE + 'lib/images/loading.gif" alt="..." class="load" />');
402        } else {
403            jQuery($content).html('<img src="' + DOKU_BASE + 'lib/images/loading.gif" alt="..." class="load" />');
404        }
405    },
406
407    update_content: function ($content, params, update_list) {
408        jQuery.post(
409            DOKU_BASE + 'lib/exe/ajax.php',
410            params,
411            function (data) {
412                jQuery('.ui-resizable').each(function(){
413                    jQuery(this).resizable('destroy');
414                });
415
416                $content.html(data);
417
418                dw_mediamanager.prepare_content($content);
419                dw_mediamanager.updatehide();
420
421                dw_mediamanager.update_resizable();
422                dw_behaviour.revisionBoxHandler();
423                jQuery('#mediamanager__form_sort').find('input[type=submit]').hide();
424                dw_mediamanager.set_filelist_view(dw_mediamanager.view, false);
425                dw_mediamanager.image_diff();
426                dw_mediamanager.init_ajax_uploader();
427
428                if (update_list) {
429                    var $link1, $content1, params1;
430                    $link1 = jQuery('a.files');
431                    params1 = $link1[0].search.substr(1)+'&call=medialist';
432                    $content1 = jQuery('#mediamanager__layout_list');
433                    dw_mediamanager.update_content($content1, params1);
434                    jQuery('.scroll-container', $content1).html('<img src="' + DOKU_BASE + 'lib/images/loading.gif" alt="..." class="load" />');
435                }
436            },
437            'html'
438        );
439    },
440
441    window_resize: function () {
442        if (jQuery('#mediamanager__layout').width() == dw_mediamanager.layout_width) {
443            return;
444        }
445
446        dw_mediamanager.layout_width = jQuery('#mediamanager__layout').width();
447        $r = jQuery("#mediamanager__layout .layout-resizable, #mediamanager__layout .layout");
448
449        var w = 0, wSum = 0, mCount = 0, mArray = [];
450        $r.each(function() {
451            w = jQuery(this).width();
452            if (w == parseFloat(jQuery(this).css("min-width"))) {
453                wSum += w;
454            } else {
455                mArray[mCount] = jQuery(this);
456                mCount++;
457            }
458        });
459
460        if (mCount > 0) {
461            var width = (0.95 * jQuery('#mediamanager__layout').width() - wSum - 30);
462            wSum = 0;
463            for(var i = 0; i < mArray.length; i++) {
464                wSum += mArray[i].width();
465            }
466            for(var i = 0; i < mArray.length; i++) {
467                w = mArray[i].width();
468                w = 100*w / wSum;
469                mArray[i].width(width*w/100);
470            }
471        }
472
473        $r.each(function() {
474            w = jQuery(this).width();
475            w = (100 * w / jQuery('#mediamanager__layout').width());
476            w += "%";
477            jQuery(this).width(w);
478        });
479
480        var windowHeight = jQuery(window).height();
481        var height = windowHeight - 300;
482        if (layout_height < height) {
483            layout_height = height;
484            jQuery('#mediamanager__layout .scroll-container').each(function (i) {
485                jQuery(this).height(height);
486            });
487            $resizable.each(function() {
488                jQuery(this).height(height+100);
489            });
490        }
491
492        dw_mediamanager.opacity_slider();
493        dw_mediamanager.portions_slider();
494    },
495
496    /**
497     * Updates mediamanager layout
498     *
499     * @author Kate Arzamastseva <pshns@ukr.net>
500     */
501    update_resizable: function () {
502        $resizable = jQuery("#mediamanager__layout .layout-resizable");
503
504        $resizable.resizable({ handles: 'e' ,
505            resize: function(event, ui){
506                var w = 0;
507                $resizable.each(function() {
508                    w += jQuery(this).width();
509                });
510                wSum = w + parseFloat(jQuery('#mediamanager__layout_detail').css("min-width"));
511
512                // max width of resizable column
513                var maxWidth = 0.95 * jQuery('#mediamanager__layout').width() - wSum + jQuery(this).width() - 30;
514                $resizable.resizable( "option", "maxWidth", maxWidth );
515
516                // percentage width of the first two columns
517                var wLeft = ( 100*(w+30) / jQuery('#mediamanager__layout').width() );
518
519                // width of the third column
520                var wRight = 95-wLeft;
521                wRight += "%";
522                jQuery('#mediamanager__layout_detail').width(wRight);
523
524                $resizable.each(function() {
525                    w = jQuery(this).width();
526                    w = (100 * w / jQuery('#mediamanager__layout').width());
527                    w += "%";
528                    jQuery(this).width(w);
529                });
530
531                dw_mediamanager.opacity_slider();
532                dw_mediamanager.portions_slider();
533            }
534        });
535
536        var windowHeight = jQuery(window).height();
537        var height = windowHeight - 300;
538        layout_height = height;
539        jQuery('#mediamanager__layout .scroll-container').each(function (i) {
540            jQuery(this).height(height);
541        });
542        $resizable.each(function() {
543            jQuery(this).height(height+100);
544        });
545    },
546
547     /**
548     * Prints 'select' for image difference representation type
549     *
550     * @author Kate Arzamastseva <pshns@ukr.net>
551     */
552    image_diff: function () {
553        if (jQuery('#mediamanager__difftype').length) return;
554
555        $form = jQuery('#mediamanager__form_diffview');
556        if (!$form.length) return;
557
558        $label = jQuery(document.createElement('label'));
559        $label.append('<span>'+LANG.media_diff+'</span>');
560        $select = jQuery(document.createElement('select'))
561         .attr('id', 'mediamanager__difftype')
562         .attr('name', 'difftype')
563         .change(dw_mediamanager.change_diff_type);
564        $select.append(new Option(LANG.media_diff_both, "both"));
565        $select.append(new Option(LANG.media_diff_opacity, "opacity"));
566        $select.append(new Option(LANG.media_diff_portions, "portions"));
567        $label.append($select);
568        $form.append($label);
569
570    	// for IE
571    	var select = document.getElementById('mediamanager__difftype');
572    	select.options[0].text = LANG.media_diff_both;
573    	select.options[1].text = LANG.media_diff_opacity;
574    	select.options[2].text = LANG.media_diff_portions;
575    },
576
577    /**
578     * Handles selection of image difference representation type
579     *
580     * @author Kate Arzamastseva <pshns@ukr.net>
581     */
582    change_diff_type: function () {
583        $select = jQuery('#mediamanager__difftype');
584        $content = jQuery('#mediamanager__diff');
585
586        params = dw_mediamanager.form_params($select.parents('form'))+'&call=mediadiff';
587        jQuery.post(
588            DOKU_BASE + 'lib/exe/ajax.php',
589            params,
590            function (data) {
591                $content.html(data);
592                dw_mediamanager.portions_slider();
593                dw_mediamanager.opacity_slider();
594            },
595            'html'
596        );
597    },
598
599    /**
600     * Sets options for opacity diff slider
601     *
602     * @author Kate Arzamastseva <pshns@ukr.net>
603     */
604    opacity_slider: function () {
605        var $slider = jQuery( "#mediamanager__opacity_slider" );
606        if (!$slider.length) return;
607
608        var $image = jQuery('#mediamanager__diff_opacity_image1 img');
609        if (!$image.length) return;
610        $slider.width($image.width()-20);
611
612        $slider.slider();
613        $slider.slider("option", "min", 0);
614        $slider.slider("option", "max", 0.999);
615        $slider.slider("option", "step", 0.001);
616        $slider.slider("option", "value", 0.5);
617        $slider.bind("slide", function(event, ui) {
618            jQuery('#mediamanager__diff_opacity_image2').css({ opacity: $slider.slider("option", "value")});
619        });
620    },
621
622     /**
623     * Sets options for red line diff slider
624     *
625     * @author Kate Arzamastseva <pshns@ukr.net>
626     */
627    portions_slider: function () {
628        var $image1 = jQuery('#mediamanager__diff_portions_image1 img');
629        var $image2 = jQuery('#mediamanager__diff_portions_image2 img');
630        if (!$image1.length || !$image2.length) return;
631
632        var $div = jQuery("#mediamanager__diff_layout");
633        if (!$div.length) return;
634
635        $div.width('100%');
636        $image2.parent().width('97%');
637        $image1.width('100%');
638        $image2.width('100%');
639
640        if ($image1.width() < $div.width()) {
641            $div.width($image1.width());
642        }
643
644        $image2.parent().width('50%');
645        $image2.width($image1.width());
646        $image1.width($image1.width());
647
648        var $slider = jQuery("#mediamanager__portions_slider");
649        if (!$slider.length) return;
650        $slider.width($image1.width()-20);
651
652        $slider.slider();
653        $slider.slider("option", "min", 0);
654        $slider.slider("option", "max", 97);
655        $slider.slider("option", "step", 1);
656        $slider.slider("option", "value", 50);
657        $slider.bind("slide", function(event, ui) {
658            jQuery('#mediamanager__diff_portions_image2').css({ width: $slider.slider("option", "value")+'%'});
659        });
660    },
661
662    params_toarray: function (str) {
663        var vars = [], hash;
664        var hashes = str.split('&');
665        for(var i = 0; i < hashes.length; i++) {
666            hash = hashes[i].split('=');
667            vars[hash[0]] = hash[1];
668        }
669        return vars;
670    },
671
672    init_ajax_uploader: function () {
673        if (!jQuery('#mediamanager__uploader').length) return;
674        if (jQuery('.qq-upload-list').length) return;
675
676        var params = dw_mediamanager.form_params(jQuery('#dw__upload'))+'&call=mediaupload';
677        params = dw_mediamanager.params_toarray(params);
678
679        var uploader = new qq.FileUploaderExtended({
680            element: document.getElementById('mediamanager__uploader'),
681            action: DOKU_BASE + 'lib/exe/ajax.php',
682            params: params
683        });
684    },
685
686    prepare_content: function ($content) {
687        // hide syntax example
688        $content.find('div.example:visible').hide();
689    },
690
691    /**
692     * shows the popup for a image link
693     */
694    select: function(event){
695        var $link, id, dot, ext;
696
697        event.preventDefault();
698
699        $link = jQuery(this);
700        id = $link.attr('name').substr(2);
701
702        if(!opener){
703            // if we don't run in popup display example
704            // the id's are a bit wierd and jQuery('#ex_wiki_dokuwiki-128.png')
705            // will not be found by Sizzle (the CSS Selector Engine
706            // used by jQuery), hence the document.getElementById() call
707            jQuery(document.getElementById('ex_'+id.replace(/:/g,'_').replace(/^_/,''))).dw_toggle();
708            return;
709        }
710
711        dw_mediamanager.ext = false;
712        dot = id.lastIndexOf(".");
713
714        if (-1 === dot) {
715            dw_mediamanager.insert(id);
716            return;
717        }
718
719        ext = id.substr(dot);
720
721        if ({'.jpg':1, '.jpeg':1, '.png':1, '.gif':1, '.swf':1}[ext] !== 1) {
722            dw_mediamanager.insert(id);
723            return;
724        }
725
726        // remove old callback from the insert button and set the new one.
727        jQuery('#media__sendbtn').unbind().click(bind(dw_mediamanager.insert, id));
728
729        dw_mediamanager.unforbid('ext');
730        if (ext === '.swf') {
731            dw_mediamanager.ext = 'swf';
732            dw_mediamanager.forbid('ext', {link: ['1', '2'],
733                                           size: ['4']});
734        } else {
735            dw_mediamanager.ext = 'img';
736        }
737
738        // Set to defaults
739        dw_mediamanager.setOpt('link');
740        dw_mediamanager.setOpt('align');
741        dw_mediamanager.setOpt('size');
742
743        // toggle buttons for detail and linked image, original size
744        jQuery('#media__linkbtn1, #media__linkbtn2, #media__sizebtn4')
745            .toggle(dw_mediamanager.ext === 'img');
746
747        dw_mediamanager.$popup.dialog('open');
748
749        jQuery('#media__sendbtn').focus();
750    },
751
752    /**
753     * Deletion confirmation dialog to the delete buttons.
754     *
755     * @author Michael Klier <chi@chimeric.de>
756     * @author Pierre Spring <pierre.spring@caillou.ch>
757     */
758    confirmattach: function(e){
759        if(!confirm(LANG.del_confirm + "\n" + jQuery(this).attr('title'))) {
760            e.preventDefault();
761        }
762    },
763
764    /**
765     * Creates checkboxes for additional options
766     *
767     * @author Andreas Gohr <andi@splitbrain.org>
768     * @author Pierre Spring <pierre.spring@caillou.ch>
769     */
770    attachoptions: function(){
771        var $obj, opts;
772
773        $obj = jQuery('#media__opts');
774        if($obj.length === 0) {
775            return;
776        }
777
778        opts = [];
779        // keep open
780        if(opener){
781            opts.push(['keepopen', 'keepopen']);
782        }
783        opts.push(['hide', 'hidedetails']);
784
785        jQuery.each(opts,
786                    function(_, opt) {
787                        var $box, $lbl;
788                        $box = jQuery(document.createElement('input'))
789                                 .attr('type', 'checkbox')
790                                 .attr('id', 'media__' + opt[0])
791                                 .click(bind(dw_mediamanager.toggleOption,
792                                             opt[0]));
793
794                        if(DokuCookie.getValue(opt[0])){
795                            $box.prop('checked', true);
796                            dw_mediamanager[opt[0]] = true;
797                        }
798
799                        $lbl = jQuery(document.createElement('label'))
800                                 .attr('for', 'media__' + opt[0])
801                                 .text(LANG[opt[1]]);
802
803                        $obj.append($box, $lbl, document.createElement('br'));
804                    });
805
806        dw_mediamanager.updatehide();
807    },
808
809    /**
810     * Generalized toggler
811     *
812     * @author Pierre Spring <pierre.spring@caillou.ch>
813     */
814    toggleOption: function (variable) {
815        if (jQuery(this).prop('checked')) {
816            DokuCookie.setValue(variable, 1);
817            dw_mediamanager[variable] = true;
818        } else {
819            DokuCookie.setValue(variable, '');
820            dw_mediamanager[variable] = false;
821        }
822        if (variable === 'hide') {
823            dw_mediamanager.updatehide();
824        }
825    },
826
827    initFlashUpload: function () {
828        var $oform, $oflash;
829        if(!hasFlash(8)) {
830            return;
831        }
832
833        $oform  = jQuery('#dw__upload');
834        $oflash = jQuery('#dw__flashupload');
835
836        if(!$oform.length || !$oflash.length) {
837            return;
838        }
839
840        jQuery(document.createElement('img'))
841            .attr('src', DOKU_BASE+'lib/images/multiupload.png')
842            .attr('title', LANG.mu_btn)
843            .attr('alt', LANG.mu_btn)
844            .css('cursor', 'pointer')
845            .click(
846                function () {
847                    $oform.hide();
848                    $oflash.show();
849                }
850            )
851            .appendTo($oform);
852    },
853
854    /**
855     * Sets the visibility of the image details accordingly to the
856     * chosen hide state
857     *
858     * @author Andreas Gohr <andi@splitbrain.org>
859     */
860    updatehide: function(){
861        jQuery('#media__content div.detail').dw_toggle(!dw_mediamanager.hide);
862    },
863
864    /**
865     * set media insertion option
866     *
867     * @author Dominik Eckelmann <eckelmann@cosmocode.de>
868     */
869    setOpt: function(opt, e){
870        var val, i;
871        if (typeof e !== 'undefined') {
872            val = this.id.substring(this.id.length - 1);
873        } else {
874            val = dw_mediamanager.getOpt(opt);
875        }
876
877        if (val === false) {
878            DokuCookie.setValue(opt,'');
879            dw_mediamanager[opt] = false;
880            return;
881        }
882
883        if (opt === 'link') {
884            if (val !== '4' && dw_mediamanager.link === '4') {
885                dw_mediamanager.unforbid('linkonly');
886                dw_mediamanager.setOpt('align');
887                dw_mediamanager.setOpt('size');
888            } else if (val === '4') {
889                dw_mediamanager.forbid('linkonly', {align: false, size: false});
890            }
891
892            jQuery("#media__size, #media__align").dw_toggle(val !== '4');
893        }
894
895        DokuCookie.setValue(opt, val);
896        dw_mediamanager[opt] = val;
897
898        for (i = 1; i <= 4; i++) {
899            jQuery("#media__" + opt + "btn" + i).removeClass('selected');
900        }
901        jQuery('#media__' + opt + 'btn' + val).addClass('selected');
902    },
903
904    unforbid: function (group) {
905        delete dw_mediamanager.forbidden_opts[group];
906    },
907
908    forbid: function (group, forbids) {
909        dw_mediamanager.forbidden_opts[group] = forbids;
910    },
911
912    allowedOpt: function (opt, val) {
913        var ret = true;
914        jQuery.each(dw_mediamanager.forbidden_opts,
915                    function (_, forbids) {
916                        ret = forbids[opt] !== false &&
917                              jQuery.inArray(val, forbids[opt]) === -1;
918                        return ret;
919                    });
920        return ret;
921    },
922
923    getOpt: function (opt) {
924        var allowed = bind(dw_mediamanager.allowedOpt, opt);
925
926        // Current value
927        if (dw_mediamanager[opt] !== false && allowed(dw_mediamanager[opt])) {
928            return dw_mediamanager[opt];
929        }
930
931        // From cookie
932        if (DokuCookie.getValue(opt) && allowed(DokuCookie.getValue(opt))) {
933            return DokuCookie.getValue(opt);
934        }
935
936        // size default
937        if (opt === 'size' && allowed('2')) {
938            return '2';
939        }
940
941        // Whatever is allowed, and be it false
942        return jQuery.grep(['1', '2', '3', '4'], allowed)[0] || false;
943    }
944};
945
946// moved from helpers.js temporarily here
947/**
948 * Very simplistic Flash plugin check, probably works for Flash 8 and higher only
949 *
950 */
951function hasFlash(version){
952    var ver = 0, axo;
953    try{
954        if(navigator.plugins !== null && navigator.plugins.length > 0){
955           ver = navigator.plugins["Shockwave Flash"].description.split(' ')[2].split('.')[0];
956        }else{
957           axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
958           ver = axo.GetVariable("$version").split(' ')[1].split(',')[0];
959        }
960    }catch(e){ }
961
962    return ver >= version;
963}
964
965jQuery(document).ready(function() {
966    dw_mediamanager.update_resizable();
967    dw_mediamanager.layout_width = jQuery("#mediamanager__layout").width();
968    jQuery(window).resize(dw_mediamanager.window_resize);
969});
970
971jQuery(dw_mediamanager.init);
972