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