1jQuery(function() {
2    var links = 0;
3    // Remove the CSS-only popup and replace it with a jQuery/AJAX popup
4    jQuery('div.dokuwiki a.plugin_davcard_url span.plugin_davcard_popup').each(function() {
5        jQuery(this).addClass('plugin_davcard_nopopup');
6        jQuery(this).removeClass('plugin_davcard_popup');
7        var $link = jQuery(this).parents('a.plugin_davcard_url');
8        if(!$link)
9            return;
10        $link.davcard_popup_id = 'plugin_davcard_popup_'+(links++);
11        $link.mouseover(function () {
12            $link.davcard_timer = window.setTimeout(
13                function () {
14                    dw_davcard__modals.showOverlay($link);
15                    $link.davcard_timer = null;
16                },
17                300
18            );
19        });
20
21        // Attach edit event dialog to link
22        $link.click(
23            function(e)
24            {
25                dw_davcard__modals.attachEditDialog($link);
26                e.preventDefault();
27                return '';
28            }
29        );
30
31        $link.mouseout(function () {
32            $link = jQuery(this);
33            if ($link.davcard_timer)
34                window.clearTimeout($link.davcard_timer);
35            $link.davcard_timer = null;
36        });
37
38    });
39
40    // Attach to addressbook links
41    var addressbookpage = jQuery('#davcardAddressbookList').data('addressbookpage');
42    if(!addressbookpage) return;
43
44    jQuery('div.davcardAddressbookAddNew a').each(function() {
45        var $link = jQuery(this);
46        var href = $link.attr('href');
47        if (!href) return;
48
49        $link.click(
50            function(e)
51            {
52                dw_davcard__modals.id = addressbookpage;
53                dw_davcard__modals.showEditContactDialog(null, false, true);
54                e.preventDefault();
55                return '';
56            }
57        );
58        }
59    );
60
61    jQuery('div.dokuwiki a.plugin_davcard_edit_vcard').each(function() {
62        var $link = jQuery(this);
63        // Attach edit event dialog to link
64        $link.click(
65            function(e)
66            {
67                dw_davcard__modals.attachEditDialog($link);
68                e.preventDefault();
69                return '';
70            }
71        );
72    });
73});
74
75/**
76 * This holds all modal windows that DAVCard uses.
77 */
78var dw_davcard__modals = {
79    $editContactDialog: null,
80    $confirmDialog: null,
81    $loadingDialog: null,
82    page: JSINFO['id'],
83    id: null,
84    uri: null,
85    action: null,
86    completeCb: null,
87    msg: null,
88
89    attachEditDialog : function($link) {
90        dw_davcard__modals.showLoadingDialog();
91        dw_davcard__modals.id = $link.data('davcardid');
92        var write = $link.data('write');
93        jQuery.post(
94            DOKU_BASE + 'lib/exe/ajax.php',
95            {
96                call: 'plugin_davcard',
97                id: $link.data('davcardid'),
98                page: dw_davcard__modals.page,
99                action: 'getContactDetails',
100                params: {
101                    uri: $link.data('davcarduri'),
102                },
103                sectok: JSINFO.plugin.davcard['sectok']
104            },
105            function(data)
106            {
107                var result = data['result'];
108                if(result === true)
109                {
110                    dw_davcard__modals.hideLoadingDialog();
111                    dw_davcard__modals.showEditContactDialog(data['contactdata'], true, write);
112                }
113                else
114                {
115                    dw_davcard__modals.hideLoadingDialog();
116                    dw_davcard__modals.msg = LANG.plugins.davcard['error_loading'];
117                    dw_davcard__modals.showDialog(false);
118                }
119            }
120        );
121    },
122
123    showEditContactDialog : function(entry, edit, write) {
124        if(dw_davcard__modals.$editContactDialog)
125            return;
126
127        var title = '';
128        var dialogButtons = {};
129
130        if(edit)
131        {
132            title = LANG.plugins.davcard['edit_entry'];
133            dialogButtons[LANG.plugins.davcard['edit']] = function() {
134
135                var postArray = { };
136                var addrArray = new Array();
137                var mailArray = new Array();
138                var phoneArray = new Array();
139                var pageid = jQuery("#dw_davcard__addressbook option:selected").val();
140
141                jQuery("tr.dw_davcard__editentry_phone_row").each(function() {
142                    var type = jQuery(this).children("td.dw_davcard__editentry_phone_row_type").data('type');
143                    var phone = jQuery(this).children("td.dw_davcard__editentry_phone_row_number").html();
144                    var phoneRow = { };
145                    phoneRow['type'] = type;
146                    phoneRow['number'] = phone;
147                    phoneArray.push(phoneRow);
148                });
149
150                jQuery("tr.dw_davcard__editentry_email_row").each(function() {
151                    var type = jQuery(this).children("td.dw_davcard__editentry_email_row_data").data('type');
152                    var email = jQuery(this).children("td.dw_davcard__editentry_email_row_data").html();
153                    mailRow = { };
154                    mailRow['type'] = type;
155                    mailRow['mail'] = email;
156                    mailArray.push(mailRow);
157                });
158
159                jQuery("tr.dw_davcard__editentry_addresses_row").each(function() {
160                    var $rowData = jQuery(this).children("td.dw_davcard__editentry_addresses_row_data");
161                    var type = $rowData.data('type');
162                    var city = $rowData.data('city');
163                    var street = $rowData.data('street');
164                    var zipcode = $rowData.data('zipcode');
165                    var country = $rowData.data('country');
166                    var addrRow = { };
167                    addrRow['type'] = type;
168                    addrRow['city'] = city;
169                    addrRow['street'] = street;
170                    addrRow['zipcode'] = zipcode;
171                    addrRow['country'] = country;
172                    addrArray.push(addrRow);
173                });
174
175                postArray['phones'] = phoneArray;
176                postArray['email'] = mailArray;
177                postArray['addresses'] = addrArray;
178                postArray['uri'] = entry['uri'];
179
180                jQuery("input.dw_davcard__editcontact").each(function() {
181                  if(jQuery(this).attr('type') == 'checkbox')
182                  {
183                      postArray[jQuery(this).prop('name')] = jQuery(this).prop('checked') ? 1 : 0;
184                  }
185                  else
186                  {
187                      postArray[jQuery(this).prop('name')] = jQuery(this).val();
188                  }
189                });
190                jQuery('#dw_davcard__ajaxedit').html('<img src="'+DOKU_BASE+'lib/images/throbber.gif" alt="" width="16" height="16" />');
191                jQuery.post(
192                    DOKU_BASE + 'lib/exe/ajax.php',
193                    {
194                        call: 'plugin_davcard',
195                        id: pageid,
196                        page: dw_davcard__modals.page,
197                        action: 'editContact',
198                        params: postArray,
199                        sectok: JSINFO.plugin.davcard['sectok']
200                    },
201                    function(data)
202                    {
203                        var result = data['result'];
204                        var html = data['html'];
205                        jQuery('#dw_davcard__ajaxedit').html(html);
206                        if(result === true)
207                        {
208                            dw_davcard__modals.hideEditContactDialog();
209                            location.reload();
210                        }
211                    }
212                );
213            };
214            dialogButtons[LANG.plugins.davcard['delete']] = function() {
215                dw_davcard__modals.action = 'deleteContact';
216                dw_davcard__modals.uri = entry['uri'];
217                dw_davcard__modals.msg = LANG.plugins.davcard['really_delete_this_entry'];
218                dw_davcard__modals.completeCb = function(data) {
219                    var result = data['result'];
220                    jQuery('#dw_davcard__ajaxedit').html(data['html']);
221                    if(result === true)
222                    {
223                        dw_davcard__modals.hideEditContactDialog();
224                        location.reload();
225                    }
226                };
227                dw_davcard__modals.showDialog(true);
228            };
229        }
230        else
231        {
232            title = LANG.plugins.davcard['create_entry'];
233            dialogButtons[LANG.plugins.davcard['create']] = function() {
234
235                var postArray = { };
236                var addrArray = new Array();
237                var mailArray = new Array();
238                var phoneArray = new Array();
239                var pageid = jQuery("#dw_davcard__addressbook option:selected").val();
240
241                jQuery("tr.dw_davcard__editentry_phone_row").each(function() {
242                    var type = jQuery(this).children("td.dw_davcard__editentry_phone_row_type").data('type');
243                    var phone = jQuery(this).children("td.dw_davcard__editentry_phone_row_number").html();
244                    var phoneRow = { };
245                    phoneRow['type'] = type;
246                    phoneRow['number'] = phone;
247                    phoneArray.push(phoneRow);
248                });
249
250                jQuery("tr.dw_davcard__editentry_email_row").each(function() {
251                    var type = jQuery(this).children("td.dw_davcard__editentry_email_row_data").data('type');
252                    var email = jQuery(this).children("td.dw_davcard__editentry_email_row_data").html();
253                    mailRow = { };
254                    mailRow['type'] = type;
255                    mailRow['mail'] = email;
256                    mailArray.push(mailRow);
257                });
258
259                jQuery("tr.dw_davcard__editentry_addresses_row").each(function() {
260                    var $rowData = jQuery(this).children("td.dw_davcard__editentry_addresses_row_data");
261                    var type = $rowData.data('type');
262                    var city = $rowData.data('city');
263                    var street = $rowData.data('street');
264                    var zipcode = $rowData.data('zipcode');
265                    var country = $rowData.data('country');
266                    var addrRow = { };
267                    addrRow['type'] = type;
268                    addrRow['city'] = city;
269                    addrRow['street'] = street;
270                    addrRow['zipcode'] = zipcode;
271                    addrRow['country'] = country;
272                    addrArray.push(addrRow);
273                });
274
275                postArray['phones'] = phoneArray;
276                postArray['email'] = mailArray;
277                postArray['addresses'] = addrArray;
278
279
280                jQuery("input.dw_davcard__editcontact").each(function() {
281                  if(jQuery(this).attr('type') == 'checkbox')
282                  {
283                      postArray[jQuery(this).prop('name')] = jQuery(this).prop('checked') ? 1 : 0;
284                  }
285                  else
286                  {
287                      postArray[jQuery(this).prop('name')] = jQuery(this).val();
288                  }
289                });
290                jQuery('#dw_davcard__ajaxedit').html('<img src="'+DOKU_BASE+'lib/images/throbber.gif" alt="" width="16" height="16" />');
291                jQuery.post(
292                    DOKU_BASE + 'lib/exe/ajax.php',
293                    {
294                        call: 'plugin_davcard',
295                        id: pageid,
296                        page: dw_davcard__modals.page,
297                        action: 'newContact',
298                        params: postArray,
299                        sectok: JSINFO.plugin.davcard['sectok']
300                    },
301                    function(data)
302                    {
303                        var result = data['result'];
304                        var html = data['html'];
305                        jQuery('#dw_davcard__ajaxedit').html(html);
306                        if(result === true)
307                        {
308                            dw_davcard__modals.hideEditContactDialog();
309                            location.reload();
310                        }
311                    }
312                );
313            };
314        }
315        // Remove create/edit buttons if we are read only
316        if(!write)
317        {
318            delete dialogButtons['create'];
319            delete dialogButtons['edit'];
320        }
321        dialogButtons[LANG.plugins.davcard['cancel']] = function() {
322            dw_davcard__modals.hideEditContactDialog();
323        };
324        dw_davcard__modals.$editContactDialog = jQuery(document.createElement('div'))
325       .dialog({
326           autoOpen: false,
327           draggable: true,
328           // fix for dragging: http://stackoverflow.com/questions/17247486/jquery-ui-dialog-dragging-issues
329           drag: function(event, ui) {
330               var fixPix = jQuery(document).scrollTop();
331               iObj = ui.position;
332               iObj.top = iObj.top - fixPix;
333               jQuery(this).closest(".ui-dialog").css("top", iObj.top + "px");
334           },
335           title: title,
336           resizable: true,
337           buttons: dialogButtons,
338       })
339       .html(
340            '<div><table>' +
341            '<tr><td>' + LANG.plugins.davcard['addressbook'] + '</td><td><select id="dw_davcard__addressbook"></select></td></tr>' +
342            '<tr><td>' + LANG.plugins.davcard['firstname'] + '</td><td><input type="text" id="dw_davcard__firstname_edit" name="firstname" class="dw_davcard__editcontact"></td></tr>' +
343            '<tr><td>' + LANG.plugins.davcard['lastname'] + '</td><td><input type="text" id="dw_davcard__lastname_edit" name="lastname" class="dw_davcard__editcontact"></td></tr>' +
344
345            '<tr><td>' + LANG.plugins.davcard['phone'] + '</td><td><table id="dw_davcard__editentry_phones"><tbody><tr><td><select id="dw_davcard__editentry_phones_select"></select></td><td><input type="text" id="dw_davcard__editentry_phone_edit"></td><td><a href="#" id="dw_davcard__editentry_phone_add">' + LANG.plugins.davcard['add_phone'] + '</a></td></tr></tbody></table></td></tr>' +
346
347            '<tr><td>' + LANG.plugins.davcard['email'] + '</td><td><table id="dw_davcard__editentry_emails"><tbody><tr><td><input type="text" id="dw_davcard__editentry_email_edit"></td><td><a href="#" id="dw_davcard__editentry_email_add">' + LANG.plugins.davcard['add_mail'] + '</a></td></tr></tbody></table></td></tr>' +
348
349            '<tr><td>' + LANG.plugins.davcard['addresses'] + '</td><td><table id="dw_davcard__editentry_addresses"><tbody>' +
350
351            '<tr><td rowspan="5"><select id="dw_davcard__editentry_addresses_select"></select></td><td>' + LANG.plugins.davcard['street'] + '</td><td><input type="text" id="dw_davcard__editentry_addresses_street"></td></tr>' +
352            '<tr><td>' + LANG.plugins.davcard['zipcode'] + '</td><td><input type="text" id="dw_davcard__editentry_addresses_zipcode"></td></tr>' +
353            '<tr><td>' + LANG.plugins.davcard['city'] + '</td><td><input type="text" id="dw_davcard__editentry_addresses_city"></td></tr>' +
354            '<tr><td>' + LANG.plugins.davcard['country'] + '</td><td><input type="text" id="dw_davcard__editentry_addresses_country"></td></tr>' +
355            '<tr><td><a href="#" id="dw_davcard__editentry_addresses_add">' + LANG.plugins.davcard['add_address'] + '</a></td></tr></tbody></table></td></tr>' +
356
357            '</tbody></table></td></tr>' +
358
359            '</table>' +
360            '</div>' +
361            '<div id="dw_davcard__ajaxedit"></div>'
362            )
363       .parent()
364       .attr('id','dw_davcard__edit')
365       .css({'overflow-y': 'auto'})
366       .show()
367       .appendTo('.dokuwiki:first');
368
369        // attach event handlers
370        jQuery('#dw_davcard__edit .ui-dialog-titlebar-close').click(function(){
371          dw_davcard__modals.hideEditContactDialog();
372        });
373
374       // Populate Dropdowns
375       var $dropdown = jQuery('#dw_davcard__editentry_phones_select');
376       $dropdown.append('<option value="work">' + dw_davcard__modals.phoneTypeToString('work') + '</option>');
377       $dropdown.append('<option value="cell">' + dw_davcard__modals.phoneTypeToString('cell') + '</option>');
378       $dropdown.append('<option value="home">' + dw_davcard__modals.phoneTypeToString('home') + '</option>');
379
380       $dropdown = jQuery('#dw_davcard__editentry_addresses_select');
381       $dropdown.append('<option value="work">' + dw_davcard__modals.addressTypeToString('work') + '</option>');
382       $dropdown.append('<option value="home">' + dw_davcard__modals.addressTypeToString('home') + '</option>');
383
384       $dropdown = jQuery('#dw_davcard__addressbook');
385       jQuery('#davcardAddressbookList option').each(function() {
386           var sel = '';
387           if(edit && dw_davcard__modals.id == jQuery(this).val())
388               sel = ' selected="selected"';
389           if(edit || (!edit && (jQuery(this).data('write') == "1" || jQuery(this).data('write') == "true")))
390           {
391               $dropdown.append('<option value="' + jQuery(this).val() + '"' + sel + '>' + jQuery(this).text() + '</option>');
392           }
393       });
394
395       if(edit)
396       {
397           $dropdown.prop('disabled', true);
398       }
399
400
401
402       dw_davcard__modals.attachPhoneDeleteHandlers();
403       dw_davcard__modals.attachMailDeleteHandlers();
404       dw_davcard__modals.attachAddressDeleteHandlers();
405       jQuery('#dw_davcard__editentry_phone_add').on("click", function(e)
406       {
407           e.preventDefault();
408           var number = jQuery('#dw_davcard__editentry_phone_edit').val();
409           if(number == '')
410             return false;
411           var type = jQuery('#dw_davcard__editentry_phones_select option:selected').val();
412           var typeText = jQuery('#dw_davcard__editentry_phones_select option:selected').text();
413           jQuery('#dw_davcard__editentry_phone_edit').val('');
414           dw_davcard__modals.addPhone(type, typeText, number);
415           return false;
416       });
417
418       jQuery('#dw_davcard__editentry_email_add').on("click", function(e)
419       {
420           e.preventDefault();
421           var mail = jQuery('#dw_davcard__editentry_email_edit').val();
422           if(mail == '')
423             return false;
424           jQuery('#dw_davcard__editentry_email_edit').val('');
425           dw_davcard__modals.addMail('internet', mail);
426           return false;
427       });
428
429       jQuery('#dw_davcard__editentry_addresses_add').on("click", function(e)
430       {
431           e.preventDefault();
432           var street = jQuery('#dw_davcard__editentry_addresses_street').val();
433           var zipcode = jQuery('#dw_davcard__editentry_addresses_zipcode').val();
434           var country = jQuery('#dw_davcard__editentry_addresses_country').val();
435           var city = jQuery('#dw_davcard__editentry_addresses_city').val();
436           var type = jQuery('#dw_davcard__editentry_addresses_select option:selected').val();
437           var typeText = jQuery('#dw_davcard__editentry_addresses_select option:selected').text();
438           if(street == '' && zipcode == '' && country == '' && city == '')
439             return false;
440           jQuery('#dw_davcard__editentry_addresses_street').val('');
441           jQuery('#dw_davcard__editentry_addresses_city').val('');
442           jQuery('#dw_davcard__editentry_addresses_country').val('');
443           jQuery('#dw_davcard__editentry_addresses_zipcode').val('');
444           dw_davcard__modals.addAddress(type, typeText, street, zipcode, city, country);
445           return false;
446       });
447       if(edit)
448       {
449           var name = entry['structuredname'].split(';');
450           jQuery('#dw_davcard__lastname_edit').val(name[0]);
451           jQuery('#dw_davcard__firstname_edit').val(name[1]);
452           for(var i=0; i<entry['tel'].length; i++)
453           {
454               var type = 'other';
455               if('type' in entry['tel'][i])
456                   var type = entry['tel'][i]['type'];
457               var val = entry['tel'][i]['number'];
458               var typeText = dw_davcard__modals.phoneTypeToString(type);
459               dw_davcard__modals.addPhone(type, typeText, val);
460           }
461           for(var i=0; i<entry['addr'].length; i++)
462           {
463               var type = 'other';
464               if('type' in entry['addr'][i])
465                 type = entry['addr'][i]['type'];
466               var street = entry['addr'][i]['address'][2];
467               var zipcode = entry['addr'][i]['address'][5];
468               var country = entry['addr'][i]['address'][6];
469               var city = entry['addr'][i]['address'][3];
470               var typeText = dw_davcard__modals.addressTypeToString(type);
471               dw_davcard__modals.addAddress(type, typeText, street, zipcode, city, country);
472           }
473           for(var i=0; i<entry['mail'].length; i++)
474           {
475               var type = 'other';
476               if('type' in entry['mail'][i])
477                 type = entry['mail'][i]['type'];
478               var mail = entry['mail'][i]['mail'];
479               dw_davcard__modals.addMail(type, mail);
480           }
481       }
482
483       // Resize the dialog if it's higher than the window
484       if(jQuery('#dw_davcard__edit').height() > jQuery(window).height())
485       {
486           jQuery('#dw_davcard__edit').height(jQuery(window).height() * 0.9);
487       }
488
489       jQuery('#dw_davcard__edit').position({
490           my: "center",
491           at: "center",
492           of: window
493       });
494    },
495
496    addressTypeToString: function(type)
497    {
498        var ret = '';
499        switch(type)
500        {
501            case 'work':
502                ret = LANG.plugins.davcard['work'];
503            break;
504            case 'home':
505                ret = LANG.plugins.davcard['home'];
506            break;
507            default:
508                ret = LANG.plugins.davcard['other_address'];
509            break;
510        }
511        return ret;
512    },
513
514    phoneTypeToString: function(type)
515    {
516        var ret = '';
517        switch(type)
518        {
519            case 'work':
520                ret = LANG.plugins.davcard['workphone'];
521            break;
522            case 'cell':
523                ret = LANG.plugins.davcard['cellphone'];
524            break;
525            case 'home':
526                ret = LANG.plugins.davcard['phone'];
527            break;
528            default:
529                ret = LANG.plugins.davcard['otherphone'];
530            break;
531        }
532        return ret;
533    },
534
535    addPhone: function(type, typeText, number)
536    {
537        var row = '<tr class="dw_davcard__editentry_phone_row"><td class="dw_davcard__editentry_phone_row_type" data-type="' + type + '">' + typeText + '</td><td class="dw_davcard__editentry_phone_row_number">' + number + '</td><td><a class="deletePhoneNumber" href="#">' + LANG.plugins.davcard['delete'] + '</a></td></tr>';
538        jQuery('#dw_davcard__editentry_phones > tbody:last').append(row);
539        dw_davcard__modals.attachPhoneDeleteHandlers();
540    },
541
542    addMail: function(type, mail) {
543        var row = '<tr class="dw_davcard__editentry_email_row"><td class="dw_davcard__editentry_email_row_data" data-type="' + type + '">' + mail + '</td><td><a class="deleteEmail" href="#">' + LANG.plugins.davcard['delete'] + '</a></td></tr>';
544        jQuery('#dw_davcard__editentry_emails > tbody:last').append(row);
545        dw_davcard__modals.attachMailDeleteHandlers();
546    },
547
548    addAddress: function(type, typeText, street, zipcode, city, country)
549    {
550        var row = '<tr class="dw_davcard__editentry_addresses_row"><td class="dw_davcard__editentry_addresses_row_data" data-type="' + type + '" data-city="' + city + '" data-zipcode="' + zipcode + '" data-country="' + country + '" data-street="' + street + '">' + typeText + '</td><td colspan="2"><strong>' + LANG.plugins.davcard['street'] + ': </strong>' + street + '<br>' +
551        '<strong>' + LANG.plugins.davcard['city'] + ': </strong>' + zipcode + ' ' + city + '<br>' +
552        '<strong>' + LANG.plugins.davcard['country'] + ': </strong>' + country + '<br>' +
553        '<a class="deleteAddress" href="#">' + LANG.plugins.davcard['delete'] + '</a></td></tr>';
554        jQuery('#dw_davcard__editentry_addresses > tbody:last').append(row);
555        dw_davcard__modals.attachAddressDeleteHandlers();
556    },
557
558    attachAddressDeleteHandlers: function()
559    {
560       jQuery("#dw_davcard__editentry_addresses .deleteAddress").on("click", function(e)
561       {
562            e.preventDefault();
563            var tr = jQuery(this).closest('tr');
564            tr.css("background-color", "#FF3700");
565            tr.fadeOut(400, function()
566            {
567                tr.remove();
568            });
569            return false;
570       });
571    },
572
573    attachPhoneDeleteHandlers: function()
574    {
575       jQuery("#dw_davcard__editentry_phones .deletePhoneNumber").on("click", function(e)
576       {
577            e.preventDefault();
578            var tr = jQuery(this).closest('tr');
579            tr.css("background-color", "#FF3700");
580            tr.fadeOut(400, function()
581            {
582                tr.remove();
583            });
584            return false;
585       });
586    },
587
588    attachMailDeleteHandlers: function()
589    {
590       jQuery("#dw_davcard__editentry_emails .deleteEmail").on("click", function(e)
591       {
592            e.preventDefault();
593            var tr = jQuery(this).closest('tr');
594            tr.css("background-color", "#FF3700");
595            tr.fadeOut(400, function()
596            {
597                tr.remove();
598            });
599            return false;
600       });
601    },
602
603    showLoadingDialog: function() {
604        if(dw_davcard__modals.$loadingDialog)
605            return;
606
607        var dialogButtons = {};
608        var title = '';
609        title = LANG.plugins.davcard['loading'];
610        dialogButtons[LANG.plugins.davcard['cancel']] = function() {
611            dw_davcard__modals.hideLoadingDialog();
612        };
613
614        dw_davcard__modals.$loadingDialog = jQuery(document.createElement('div'))
615            .dialog({
616                autoOpen: false,
617                draggable: true,
618                //fix for dragging: http://stackoverflow.com/questions/17247486/jquery-ui-dialog-dragging-issues
619                drag: function(event, ui) {
620                    var fixPix = jQuery(document).scrollTop();
621                    iObj = ui.position;
622                    iObj.top = iObj.top - fixPix;
623                    jQuery(this).closest(".ui-dialog").css("top", iObj.top + "px");
624                },
625                title: title,
626                resizable: true,
627                buttons: dialogButtons,
628            })
629            .html('<div>' + LANG.plugins.davcard['loading'] +
630                '<img src="'+DOKU_BASE+'lib/images/throbber.gif" alt="" width="16" height="16" /></div>'
631            )
632            .parent()
633            .attr('id','dw_davcard__loading')
634            .show()
635            .appendTo('.dokuwiki:first');
636
637            jQuery('#dw_davcard__loading').position({
638                my: "center",
639                at: "center",
640                of: window
641            });
642                 // attach event handlers
643            jQuery('#dw_davcard__loading .ui-dialog-titlebar-close').click(function(){
644                dw_davcard__modals.hideDialog();
645            });
646
647    },
648
649    hideEditContactDialog : function() {
650        dw_davcard__modals.$editContactDialog.empty();
651        dw_davcard__modals.$editContactDialog.remove();
652        dw_davcard__modals.$editContactDialog = null;
653    },
654
655    hideDialog: function() {
656        dw_davcard__modals.$dialog.empty();
657        dw_davcard__modals.$dialog.remove();
658        dw_davcard__modals.$dialog = null;
659    },
660
661    hideLoadingDialog: function() {
662        dw_davcard__modals.$loadingDialog.empty();
663        dw_davcard__modals.$loadingDialog.remove();
664        dw_davcard__modals.$loadingDialog = null;
665    },
666
667    showOverlay: function($link)
668    {
669        if(!$link.davcard_popup)
670        {
671            $link.davcard_popup = dw_page.insituPopup($link, $link.davcard_popup_id);
672            $link.davcard_popup.addClass('plugin_davcard_popup_overlay');
673            $link.davcard_popup.load(
674                DOKU_BASE + 'lib/exe/ajax.php',
675                {
676                    call: 'plugin_davcard',
677                    sectok: JSINFO.plugin.davcard['sectok'],
678                    action: 'getContactAjax',
679                    id: $link.data('davcardid'),
680                    params: {
681                        uri: $link.data('davcarduri'),
682                    }
683                }
684            );
685        }
686        $link.davcard_popup.show();
687    },
688
689    showDialog : function(confirm)
690    {
691        if(dw_davcard__modals.$confirmDialog)
692            return;
693
694        var dialogButtons = {};
695        var title = '';
696        if(confirm)
697        {
698            title = LANG.plugins.davcard['confirmation'];
699
700            dialogButtons[LANG.plugins.davcard['yes']] =  function() {
701                            jQuery.post(
702                                DOKU_BASE + 'lib/exe/ajax.php',
703                                {
704                                    call: 'plugin_davcard',
705                                    id: dw_davcard__modals.id,
706                                    page: dw_davcard__modals.page,
707                                    action: dw_davcard__modals.action,
708                                    params: {
709                                        uri: dw_davcard__modals.uri
710                                    },
711                                    sectok: JSINFO.plugin.davcard['sectok']
712                                },
713                                function(data)
714                                {
715                                    dw_davcard__modals.completeCb(data);
716                                }
717                            );
718                            dw_davcard__modals.hideDialog();
719                    };
720            dialogButtons[LANG.plugins.davcard['cancel']] = function() {
721                            dw_davcard__modals.hideDialog();
722                    };
723        }
724        else
725        {
726            title = LANG.plugins.davcard['info'];
727            dialogButtons[LANG.plugins.davcard['ok']] = function() {
728                 dw_davcard__modals.hideDialog();
729            };
730        }
731        dw_davcard__modals.$dialog = jQuery(document.createElement('div'))
732            .dialog({
733                autoOpen: false,
734                draggable: true,
735                //fix for dragging: http://stackoverflow.com/questions/17247486/jquery-ui-dialog-dragging-issues
736                drag: function(event, ui) {
737                    var fixPix = jQuery(document).scrollTop();
738                    iObj = ui.position;
739                    iObj.top = iObj.top - fixPix;
740                    jQuery(this).closest(".ui-dialog").css("top", iObj.top + "px");
741                },
742                title: title,
743                resizable: true,
744                buttons: dialogButtons,
745            })
746            .html(
747                '<div>' + dw_davcard__modals.msg + '</div>'
748            )
749            .parent()
750            .attr('id','dw_davcard__confirm')
751            .show()
752            .appendTo('.dokuwiki:first');
753
754            jQuery('#dw_davcard__confirm').position({
755                my: "center",
756                at: "center",
757                of: window
758            });
759                 // attach event handlers
760            jQuery('#dw_davcard__confirm .ui-dialog-titlebar-close').click(function(){
761                dw_davcard__modals.hideDialog();
762            });
763    },
764
765};