1jQuery(function () {
2    var $links = jQuery('a.plugin_qsub_subscribe');
3    if (!$links.length) return;
4
5    /**
6     * Add additional stuff to the dialog
7     *
8     * @param $to {object} jQuery dialog object
9     * @param href {string}
10     */
11    function addmore($to, href) {
12        var $more = jQuery(document.createElement('div'));
13        $more.addClass('more');
14        $more.html(
15            '<p>' + LANG.plugins.quicksubscribe.edit_subscr +
16            '<button class="button">' + LANG.plugins.quicksubscribe.edit_subscr_button +
17            '</button></p>'
18        ).find('button').click(function () {
19            document.location = href;
20        });
21
22        $to.append($more);
23    }
24
25    /**
26     * Handles the whole click processing
27     *
28     * @param e
29     * @returns {boolean}
30     */
31    function clickhandler(e) {
32        // remove any existing popup
33        jQuery('#plugin_qsub__popup').remove();
34
35        // create a new popup
36        var $overlay = jQuery(document.createElement('div'));
37        $overlay.attr({
38            id: 'plugin_qsub__popup',
39            title: LANG.plugins.quicksubscribe.title
40        });
41
42        var $link = jQuery(this);
43
44        if (jQuery(this).hasClass('plugin_qsub_notsubscribed')) {
45            // Handle Subscriptions
46            $overlay.html(
47                '<p>' + LANG.plugins.quicksubscribe.subscr_in_progress + '</p>'
48            ).load(
49                DOKU_BASE + 'lib/exe/ajax.php',
50                {
51                    call: 'plugin_quicksubscribe',
52                    ns: JSINFO.namespace + ':',
53                    'do': 'subscribe'
54                },
55                function (text, status) {
56                    if (status === 'success') {
57                        $link.addClass('plugin_qsub_subscribed');
58                        $link.removeClass('plugin_qsub_notsubscribed');
59                        $link.data('target', JSINFO.namespace + ':');
60                        $link.attr('title', LANG.plugins.quicksubscribe.unsubscribe);
61                    }
62                    addmore($overlay, $link.attr('href'));
63                }
64            );
65        } else {
66            // Handle unsubscriptions
67            $overlay.html(
68                '<p>' + LANG.plugins.quicksubscribe.is_subscr.replace(/%s/, $link.data('target')) +
69                ' ' + LANG.plugins.quicksubscribe.del_subscr +
70                '<button class="button">' +
71                LANG.plugins.quicksubscribe.del_subscr_button +
72                '</button>' + '</p>'
73            ).find('button').click(function () {
74                $overlay.load(
75                    DOKU_BASE + 'lib/exe/ajax.php',
76                    {
77                        call: 'plugin_quicksubscribe',
78                        ns: $link.data('target'),
79                        'do': 'unsubscribe'
80                    },
81                    function (text, status) {
82                        if (status === 'success') {
83                            $link.data('target', '');
84                            $link.removeClass('plugin_qsub_subscribed');
85                            $link.addClass('plugin_qsub_notsubscribed');
86                            $link.attr('title', LANG.plugins.quicksubscribe.subscribe);
87                        }
88                        addmore($overlay, $link.attr('href'));
89                    }
90                );
91            });
92            addmore($overlay, $link.attr('href'));
93        }
94
95        // show the dialog
96        $overlay.dialog();
97        e.preventDefault();
98        e.stopPropagation();
99        return false;
100    }
101
102    // attach dialog creation to any quicksubscribe link
103    $links.each(function () {
104        var $link = jQuery(this);
105        // attach click handler
106        $link.click(clickhandler);
107    });
108});
109