]> git.mxchange.org Git - friendica.git/blob - view/js/main.js
f81d7a42352125231972f63e08518ab12525e513
[friendica.git] / view / js / main.js
1 // @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPLv3-or-later
2 function resizeIframe(obj) {
3         _resizeIframe(obj, 0);
4 }
5
6 function _resizeIframe(obj, desth) {
7         var h = obj.style.height;
8         var ch = obj.contentWindow.document.body.scrollHeight;
9
10         if (h == (ch + 'px')) {
11                 return;
12         }
13         if (desth == ch && ch > 0) {
14                 obj.style.height  = ch + 'px';
15         }
16         setTimeout(_resizeIframe, 100, obj, ch);
17 }
18
19 function openClose(theID) {
20         if (document.getElementById(theID).style.display == "block") {
21                 document.getElementById(theID).style.display = "none"
22         } else {
23                 document.getElementById(theID).style.display = "block"
24         }
25 }
26
27 function openMenu(theID) {
28         var el = document.getElementById(theID);
29
30         if (el) {
31                 el.style.display = "block";
32         }
33 }
34
35 function closeMenu(theID) {
36         var el = document.getElementById(theID)
37
38         if (el) {
39                 el.style.display = "none";
40         }
41 }
42
43 function decodeHtml(html) {
44         var txt = document.createElement("textarea");
45
46         txt.innerHTML = html;
47         return txt.value;
48 }
49
50 var src = null;
51 var prev = null;
52 var livetime = null;
53 var force_update = false;
54 var update_item = 0;
55 var stopped = false;
56 var totStopped = false;
57 var timer = null;
58 var pr = 0;
59 var liking = 0;
60 var in_progress = false;
61 var langSelect = false;
62 var commentBusy = false;
63 var last_popup_menu = null;
64 var last_popup_button = null;
65 var lockLoadContent = false;
66
67 $(function() {
68         $.ajaxSetup({cache: false});
69
70         /* setup comment textarea buttons */
71         /* comment textarea buttons needs some "data-*" attributes to work:
72          *              data-role="insert-formatting" : to mark the element as a formatting button
73          *              data-bbcode="<string>" : name of the bbcode element to insert. insertFormatting() will insert it as "[name][/name]"
74          *              data-id="<string>" : id of the comment, used to find other comment-related element, like the textarea
75          * */
76         $('body').on('click','[data-role="insert-formatting"]', function(e) {
77                 e.preventDefault();
78                 var o = $(this);
79                 var bbcode = o.data('bbcode');
80                 var id = o.data('id');
81                 if (bbcode == "img") {
82                         Dialog.doImageBrowser("comment", id);
83                         return;
84                 }
85
86                 if (bbcode == "imgprv") {
87                         bbcode = "img";
88                 }
89
90                 insertFormatting(bbcode, id);
91         });
92
93         /* event from comment textarea button popups */
94         /* insert returned bbcode at cursor position or replace selected text */
95         $("body").on("fbrowser.image.comment", function(e, filename, bbcode, id) {
96                 $.colorbox.close();
97                 var textarea = document.getElementById("comment-edit-text-" +id);
98                 var start = textarea.selectionStart;
99                 var end = textarea.selectionEnd;
100                 textarea.value = textarea.value.substring(0, start) + bbcode + textarea.value.substring(end, textarea.value.length);
101                 $(textarea).trigger('change');
102         });
103
104         /* setup onoff widgets */
105         $(".onoff input").each(function() {
106                 val = $(this).val();
107                 id = $(this).attr("id");
108                 $("#"+id+"_onoff ." + (val == 0 ? "on":"off")).addClass("hidden");
109         });
110
111         $(".onoff > a").click(function(event) {
112                 event.preventDefault();
113                 var input = $(this).siblings("input");
114                 var val = 1-input.val();
115                 var id = input.attr("id");
116                 $("#"+id+"_onoff ." + (val == 0 ? "on":"off")).addClass("hidden");
117                 $("#"+id+"_onoff ." + (val == 1 ? "on":"off")).removeClass("hidden");
118                 input.val(val);
119         });
120
121         /* popup menus */
122         function close_last_popup_menu() {
123                 if (last_popup_menu) {
124                         last_popup_menu.hide();
125                         last_popup_menu.off('click', function(e) {e.stopPropagation()});
126                         last_popup_button.removeClass("selected");
127                         last_popup_menu = null;
128                         last_popup_button = null;
129                 }
130         }
131         $('a[rel^="#"]').click(function(e) {
132                 e.preventDefault();
133                 var parent = $(this).parent();
134                 var isSelected = (last_popup_button && parent.attr('id') == last_popup_button.attr('id'));
135                 close_last_popup_menu();
136                 if (isSelected) {
137                         return false;
138                 }
139                 menu = $($(this).attr('rel'));
140                 e.preventDefault();
141                 e.stopPropagation();
142                 if (menu.attr('popup') == "false") {
143                         return false;
144                 }
145                 parent.toggleClass("selected");
146                 menu.toggle();
147                 if (menu.css("display") == "none") {
148                         last_popup_menu = null;
149                         last_popup_button = null;
150                 } else {
151                         last_popup_menu = menu;
152                         last_popup_menu.on('click', function(e) {e.stopPropagation()});
153                         last_popup_button = parent;
154                         $('#nav-notifications-menu').perfectScrollbar('update');
155                 }
156                 return false;
157         });
158         $('html').click(function() {
159                 close_last_popup_menu();
160         });
161
162         // fancyboxes
163         $("a.popupbox").colorbox({
164                 'inline' : true,
165                 'transition' : 'elastic',
166                 'maxWidth' : '100%'
167         });
168         $("a.ajax-popupbox").colorbox({
169                 'transition' : 'elastic',
170                 'maxWidth' : '100%'
171         });
172
173         /* notifications template */
174         var notifications_tpl= unescape($("#nav-notifications-template[rel=template]").html());
175         var notifications_all = unescape($('<div>').append($("#nav-notifications-see-all").clone()).html()); //outerHtml hack
176         var notifications_mark = unescape($('<div>').append($("#nav-notifications-mark-all").clone()).html()); //outerHtml hack
177         var notifications_empty = unescape($("#nav-notifications-menu").html());
178
179         /* enable perfect-scrollbars for different elements */
180         $('#nav-notifications-menu, aside').perfectScrollbar();
181
182         /* nav update event  */
183         $('nav').bind('nav-update', function(e, data) {
184                 var invalid = data.invalid || 0;
185                 if (invalid == 1) {
186                         window.location.href=window.location.href
187                 }
188
189                 ['net', 'home', 'intro', 'mail', 'events', 'birthdays', 'notify'].forEach(function(type) {
190                         var number = data[type];
191                         if (number == 0) {
192                                 number = '';
193                                 $('#' + type + '-update').removeClass('show');
194                         } else {
195                                 $('#' + type + '-update').addClass('show');
196                         }
197                         $('#' + type + '-update').text(number);
198                 });
199
200                 var intro = data['intro'];
201                 if (intro == 0) {
202                         intro = ''; $('#intro-update-li').removeClass('show')
203                 } else {
204                         $('#intro-update-li').addClass('show')
205                 }
206
207                 $('#intro-update-li').html(intro);
208
209                 var mail = data['mail'];
210                 if (mail == 0) {
211                         mail = ''; $('#mail-update-li').removeClass('show')
212                 } else {
213                         $('#mail-update-li').addClass('show')
214                 }
215
216                 $('#mail-update-li').html(mail);
217
218                 $(".sidebar-group-li .notify").removeClass("show");
219                 $(data.groups).each(function(key, group) {
220                         var gid = group.id;
221                         var gcount = group.count;
222                         $(".group-"+gid+" .notify").addClass("show").text(gcount);
223                 });
224
225                 $(".forum-widget-entry .notify").removeClass("show");
226                 $(data.forums).each(function(key, forum) {
227                         var fid = forum.id;
228                         var fcount = forum.count;
229                         $(".forum-"+fid+" .notify").addClass("show").text(fcount);
230                 });
231
232                 if (data.notifications.length == 0) {
233                         $("#nav-notifications-menu").html(notifications_empty);
234                 } else {
235                         var nnm = $("#nav-notifications-menu");
236                         nnm.html(notifications_all + notifications_mark);
237
238                         var lastItemStorageKey = "notification-lastitem:" + localUser;
239                         var notification_lastitem = parseInt(localStorage.getItem(lastItemStorageKey));
240                         var notification_id = 0;
241
242                         // Insert notifs into the notifications-menu
243                         $(data.notifications).each(function(key, notif) {
244                                 var text = notif.message.format('<span class="contactname">' + notif.name + '</span>');
245                                 var contact = ('<a href="' + notif.url + '"><span class="contactname">' + notif.name + '</span></a>');
246                                 var seenclass = (notif.seen == 1) ? "notify-seen" : "notify-unseen";
247                                 var html = notifications_tpl.format(
248                                         notif.href,                     // {0}  // link to the source
249                                         notif.photo,                    // {1}  // photo of the contact
250                                         text,                           // {2}  // preformatted text (autor + text)
251                                         notif.date,                     // {3}  // date of notification (time ago)
252                                         seenclass,                      // {4}  // visited status of the notification
253                                         new Date(notif.timestamp*1000), // {5}  // date of notification
254                                         notif.url,                      // {6}  // profile url of the contact
255                                         notif.message.format(contact),  // {7}  // preformatted html (text including author profile url)
256                                         ''                              // {8}  // Deprecated
257                                 );
258                                 nnm.append(html);
259                         });
260
261                         // Desktop Notifications
262                         $(data.notifications.reverse()).each(function(key, e) {
263                                 notification_id = parseInt(e.timestamp);
264                                 if (notification_lastitem !== null && notification_id > notification_lastitem && Number(e.seen) === 0) {
265                                         if (getNotificationPermission() === "granted") {
266                                                 var notification = new Notification(document.title, {
267                                                                                   body: decodeHtml(e.message.replace('&rarr; ', '').format(e.name)),
268                                                                                   icon: e.photo,
269                                                                                 });
270                                                 notification['url'] = e.href;
271                                                 notification.addEventListener("click", function(ev) {
272                                                         window.location = ev.target.url;
273                                                 });
274                                         }
275                                 }
276
277                         });
278                         notification_lastitem = notification_id;
279                         localStorage.setItem(lastItemStorageKey, notification_lastitem)
280
281                         $("img[data-src]", nnm).each(function(i, el) {
282                                 // Add src attribute for images with a data-src attribute
283                                 // However, don't bother if the data-src attribute is empty, because
284                                 // an empty "src" tag for an image will cause some browsers
285                                 // to prefetch the root page of the Friendica hub, which will
286                                 // unnecessarily load an entire profile/ or network/ page
287                                 if ($(el).data("src") != '') {
288                                         $(el).attr('src', $(el).data("src"));
289                                 }
290                         });
291                 }
292
293                 var notif = data['notify'];
294                 if (notif > 0) {
295                         $("#nav-notifications-linkmenu").addClass("on");
296                 } else {
297                         $("#nav-notifications-linkmenu").removeClass("on");
298                 }
299
300                 $(data.sysmsgs.notice).each(function(key, message) {
301                         $.jGrowl(message, {sticky: true, theme: 'notice'});
302                 });
303                 $(data.sysmsgs.info).each(function(key, message) {
304                         $.jGrowl(message, {sticky: false, theme: 'info', life: 5000});
305                 });
306
307                 // Update the js scrollbars
308                 $('#nav-notifications-menu').perfectScrollbar('update');
309         });
310
311         // Asynchronous calls are deferred until the very end of the page load to ease on slower connections
312         window.addEventListener("load", function(){
313                 NavUpdate();
314                 if (typeof acl !== 'undefined') {
315                         acl.get(0, 100);
316                 }
317         });
318
319         // Allow folks to stop the ajax page updates with the pause/break key
320         $(document).keydown(function(event) {
321                 if (event.keyCode == '8') {
322                         var target = event.target || event.srcElement;
323                         if (!/input|textarea/i.test(target.nodeName)) {
324                                 return false;
325                         }
326                 }
327
328                 if (event.keyCode == '19' || (event.ctrlKey && event.which == '32')) {
329                         event.preventDefault();
330                         if (stopped == false) {
331                                 stopped = true;
332                                 if (event.ctrlKey) {
333                                         totStopped = true;
334                                 }
335                                 $('#pause').html('<img src="images/pause.gif" alt="pause" style="border: 1px solid black;" />');
336                         } else {
337                                 unpause();
338                         }
339                 } else if (!totStopped) {
340                         unpause();
341                 }
342         });
343
344         // Scroll to the next/previous thread when pressing J and K
345         $(document).keydown(function (event) {
346                 var threads = $('.thread_level_1');
347                 if ((event.keyCode === 74 || event.keyCode === 75) && !$(event.target).is('textarea, input')) {
348                         var scrollTop = $(window).scrollTop();
349                         if (event.keyCode === 75) {
350                                 threads = $(threads.get().reverse());
351                         }
352                         threads.each(function(key, item) {
353                                 var comparison;
354                                 var top = $(item).offset().top - 100;
355                                 if (event.keyCode === 74) {
356                                         comparison = top > scrollTop + 1;
357                                 } else if (event.keyCode === 75) {
358                                         comparison = top < scrollTop - 1;
359                                 }
360                                 if (comparison) {
361                                         $('html, body').animate({scrollTop: top}, 200);
362                                         return false;
363                                 }
364                         });
365                 }
366         });
367
368         // Set an event listener for infinite scroll
369         if (typeof infinite_scroll !== 'undefined') {
370                 $(window).scroll(function(e) {
371                         if ($(document).height() != $(window).height()) {
372                                 // First method that is expected to work - but has problems with Chrome
373                                 if ($(window).scrollTop() > ($(document).height() - $(window).height() * 1.5))
374                                         loadScrollContent();
375                         } else {
376                                 // This method works with Chrome - but seems to be much slower in Firefox
377                                 if ($(window).scrollTop() > (($("section").height() + $("header").height() + $("footer").height()) - $(window).height() * 1.5)) {
378                                         loadScrollContent();
379                                 }
380                         }
381                 });
382         }
383 });
384
385 function NavUpdate() {
386         if (!stopped) {
387                 var pingCmd = 'ping?format=json' + ((localUser != 0) ? '&f=&uid=' + localUser : '');
388                 $.get(pingCmd, function(data) {
389                         if (data.result) {
390                                 // send nav-update event
391                                 $('nav').trigger('nav-update', data.result);
392
393                                 // start live update
394                                 ['network', 'profile', 'community', 'notes', 'display', 'contact'].forEach(function (src) {
395                                         if ($('#live-' + src).length) {
396                                                 liveUpdate(src);
397                                         }
398                                 });
399                                 if ($('#live-photos').length) {
400                                         if (liking) {
401                                                 liking = 0;
402                                                 window.location.href = window.location.href;
403                                         }
404                                 }
405                         }
406                 });
407         }
408         timer = setTimeout(NavUpdate, updateInterval);
409 }
410
411 function updateConvItems(data) {
412         // add a new thread
413         $('.toplevel_item',data).each(function() {
414                 var ident = $(this).attr('id');
415
416                 // Add new top-level item.
417                 if ($('#' + ident).length == 0 && profile_page == 1) {
418                         $('#' + prev).after($(this));
419
420                 // Replace already existing thread.
421                 } else {
422                         // Find out if the hidden comments are open, so we can keep it that way
423                         // if a new comment has been posted
424                         var id = $('.hide-comments-total', this).attr('id');
425                         if (typeof id != 'undefined') {
426                                 id = id.split('-')[3];
427                                 var commentsOpen = $("#collapsed-comments-" + id).is(":visible");
428                         }
429
430                         $('#' + ident).replaceWith($(this));
431
432                         if (typeof id != 'undefined') {
433                                 if (commentsOpen) {
434                                         showHideComments(id);
435                                 }
436                         }
437                 }
438                 prev = ident;
439         });
440
441         $('.like-rotator').hide();
442         if (commentBusy) {
443                 commentBusy = false;
444                 $('body').css('cursor', 'auto');
445         }
446         /* autocomplete @nicknames */
447         $(".comment-edit-form  textarea").editor_autocomplete(baseurl+"/acl");
448         /* autocomplete bbcode */
449         $(".comment-edit-form  textarea").bbco_autocomplete('bbcode');
450 }
451
452 function liveUpdate(src) {
453         if ((src == null) || stopped || !profile_uid) {
454                 $('.like-rotator').hide(); return;
455         }
456
457         if (($('.comment-edit-text-full').length) || in_progress) {
458                 if (livetime) {
459                         clearTimeout(livetime);
460                 }
461                 livetime = setTimeout(function() {liveUpdate(src)}, 5000);
462                 return;
463         }
464
465         if (livetime != null) {
466                 livetime = null;
467         }
468         prev = 'live-' + src;
469
470         in_progress = true;
471
472         if ($(document).scrollTop() == 0) {
473                 force_update = true;
474         }
475
476         var orgHeight = $("section").height();
477
478         var udargs = ((netargs.length) ? '/' + netargs : '');
479         var update_url = 'update_' + src + udargs + '&p=' + profile_uid + '&page=' + profile_page + '&force=' + ((force_update) ? 1 : 0) + '&item=' + update_item;
480
481         $.get(update_url,function(data) {
482                 in_progress = false;
483                 force_update = false;
484                 update_item = 0;
485
486                 $('.wall-item-body', data).imagesLoaded(function() {
487                         updateConvItems(data);
488
489                         document.dispatchEvent(new Event('postprocess_liveupdate'));
490
491                         // Update the scroll position.
492                         $(window).scrollTop($(window).scrollTop() + $("section").height() - orgHeight);
493                 });
494         });
495 }
496
497 function imgbright(node) {
498         $(node).removeClass("drophide").addClass("drop");
499 }
500
501 function imgdull(node) {
502         $(node).removeClass("drop").addClass("drophide");
503 }
504
505 // Since our ajax calls are asynchronous, we will give a few
506 // seconds for the first ajax call (setting like/dislike), then
507 // run the updater to pick up any changes and display on the page.
508 // The updater will turn any rotators off when it's done.
509 // This function will have returned long before any of these
510 // events have completed and therefore there won't be any
511 // visible feedback that anything changed without all this
512 // trickery. This still could cause confusion if the "like" ajax call
513 // is delayed and NavUpdate runs before it completes.
514
515 function dolike(ident,verb) {
516         unpause();
517         $('#like-rotator-' + ident.toString()).show();
518         $.get('like/' + ident.toString() + '?verb=' + verb, NavUpdate);
519         liking = 1;
520         force_update = true;
521         update_item = ident.toString();
522 }
523
524 function dosubthread(ident) {
525         unpause();
526         $('#like-rotator-' + ident.toString()).show();
527         $.get('subthread/' + ident.toString(), NavUpdate);
528         liking = 1;
529 }
530
531 function dostar(ident) {
532         ident = ident.toString();
533         $('#like-rotator-' + ident).show();
534         $.get('starred/' + ident, function(data) {
535                 if (data.match(/1/)) {
536                         $('#starred-' + ident).addClass('starred');
537                         $('#starred-' + ident).removeClass('unstarred');
538                         $('#star-' + ident).addClass('hidden');
539                         $('#unstar-' + ident).removeClass('hidden');
540                 } else {
541                         $('#starred-' + ident).addClass('unstarred');
542                         $('#starred-' + ident).removeClass('starred');
543                         $('#star-' + ident).removeClass('hidden');
544                         $('#unstar-' + ident).addClass('hidden');
545                 }
546                 $('#like-rotator-' + ident).hide();
547         });
548 }
549
550 function doignore(ident) {
551         ident = ident.toString();
552         $('#like-rotator-' + ident).show();
553         $.get('ignored/' + ident, function(data) {
554                 if (data.match(/1/)) {
555                         $('#ignored-' + ident).addClass('ignored');
556                         $('#ignored-' + ident).removeClass('unignored');
557                         $('#ignore-' + ident).addClass('hidden');
558                         $('#unignore-' + ident).removeClass('hidden');
559                 } else {
560                         $('#ignored-' + ident).addClass('unignored');
561                         $('#ignored-' + ident).removeClass('ignored');
562                         $('#ignore-' + ident).removeClass('hidden');
563                         $('#unignore-' + ident).addClass('hidden');
564                 }
565                 $('#like-rotator-' + ident).hide();
566         });
567 }
568
569 function getPosition(e) {
570         var cursor = {x:0, y:0};
571
572         if (e.pageX || e.pageY) {
573                 cursor.x = e.pageX;
574                 cursor.y = e.pageY;
575         } else {
576                 if (e.clientX || e.clientY) {
577                         cursor.x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
578                         cursor.y = e.clientY + (document.documentElement.scrollTop  || document.body.scrollTop)  - document.documentElement.clientTop;
579                 } else if (e.x || e.y) {
580                         cursor.x = e.x;
581                         cursor.y = e.y;
582                 }
583         }
584         return cursor;
585 }
586
587 var lockvisible = false;
588
589 function lockview(event,id) {
590         event = event || window.event;
591         cursor = getPosition(event);
592         if (lockvisible) {
593                 lockviewhide();
594         } else {
595                 lockvisible = true;
596                 $.get('lockview/' + id, function(data) {
597                         $('#panel').html(data);
598                         $('#panel').css({'left': cursor.x + 5 , 'top': cursor.y + 5});
599                         $('#panel').show();
600                 });
601         }
602 }
603
604 function lockviewhide() {
605         lockvisible = false;
606         $('#panel').hide();
607 }
608
609 function post_comment(id) {
610         unpause();
611         commentBusy = true;
612         $('body').css('cursor', 'wait');
613         $("#comment-preview-inp-" + id).val("0");
614         $.post(
615                 "item",
616                 $("#comment-edit-form-" + id).serialize(),
617                 function(data) {
618                         if (data.success) {
619                                 $("#comment-edit-wrapper-" + id).hide();
620                                 $("#comment-edit-text-" + id).val('');
621                                 var tarea = document.getElementById("comment-edit-text-" + id);
622                                 if (tarea) {
623                                         commentClose(tarea,id);
624                                 }
625                                 if (timer) {
626                                         clearTimeout(timer);
627                                 }
628                                 timer = setTimeout(NavUpdate,10);
629                                 force_update = true;
630                                 update_item = id;
631                         }
632                         if (data.reload) {
633                                 window.location.href=data.reload;
634                         }
635                 },
636                 "json"
637         );
638         return false;
639 }
640
641 function preview_comment(id) {
642         $("#comment-preview-inp-" + id).val("1");
643         $("#comment-edit-preview-" + id).show();
644         $.post(
645                 "item",
646                 $("#comment-edit-form-" + id).serialize(),
647                 function(data) {
648                         if (data.preview) {
649                                 $("#comment-edit-preview-" + id).html(data.preview);
650                                 $("#comment-edit-preview-" + id + " a").click(function() {return false;});
651                         }
652                 },
653                 "json"
654         );
655         return true;
656 }
657
658 function showHideComments(id) {
659         if ($("#collapsed-comments-" + id).is(":visible")) {
660                 $("#collapsed-comments-" + id).hide();
661                 $("#hide-comments-" + id).html(window.showMore);
662         } else {
663                 $("#collapsed-comments-" + id).show();
664                 $("#hide-comments-" + id).html(window.showFewer);
665         }
666 }
667
668 function preview_post() {
669         $("#jot-preview").val("1");
670         $("#jot-preview-content").show();
671         $.post(
672                 "item",
673                 $("#profile-jot-form").serialize(),
674                 function(data) {
675                         if (data.preview) {
676                                 $("#jot-preview-content").html(data.preview);
677                                 $("#jot-preview-content" + " a").click(function() {return false;});
678                                 document.dispatchEvent(new Event('postprocess_liveupdate'));
679                         }
680                 },
681                 "json"
682         );
683         $("#jot-preview").val("0");
684         return true;
685 }
686
687 function unpause() {
688         // unpause auto reloads if they are currently stopped
689         totStopped = false;
690         stopped = false;
691         $('#pause').html('');
692 }
693
694 // load more network content (used for infinite scroll)
695 function loadScrollContent() {
696         if (lockLoadContent) {
697                 return;
698         }
699         lockLoadContent = true;
700
701         $("#scroll-loader").fadeIn('normal');
702
703         // the page number to load is one higher than the actual
704         // page number
705         infinite_scroll.pageno+=1;
706
707         match = $("span.received").last();
708         if (match.length > 0) {
709                 received = match[0].innerHTML;
710         } else {
711                 received = "0000-00-00 00:00:00";
712         }
713
714         match = $("span.created").last();
715         if (match.length > 0) {
716                 created = match[0].innerHTML;
717         } else {
718                 created = "0000-00-00 00:00:00";
719         }
720
721         match = $("span.commented").last();
722         if (match.length > 0) {
723                 commented = match[0].innerHTML;
724         } else {
725                 commented = "0000-00-00 00:00:00";
726         }
727
728         match = $("span.id").last();
729         if (match.length > 0) {
730                 id = match[0].innerHTML;
731         } else {
732                 id = "0";
733         }
734
735         // get the raw content from the next page and insert this content
736         // right before "#conversation-end"
737         $.get(infinite_scroll.reload_uri + '&mode=raw&last_received=' + received + '&last_commented=' + commented + '&last_created=' + created + '&last_id=' + id + '&page=' + infinite_scroll.pageno, function(data) {
738                 $("#scroll-loader").hide();
739                 if ($(data).length > 0) {
740                         $(data).insertBefore('#conversation-end');
741                         lockLoadContent = false;
742                 } else {
743                         $("#scroll-end").fadeIn('normal');
744                 }
745
746                 document.dispatchEvent(new Event('postprocess_liveupdate'));
747         });
748 }
749
750 function bin2hex(s) {
751         // Converts the binary representation of data to hex
752         //
753         // version: 812.316
754         // discuss at: http://phpjs.org/functions/bin2hex
755         // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
756         // +   bugfixed by: Onno Marsman
757         // +   bugfixed by: Linuxworld
758         // *     example 1: bin2hex('Kev');
759         // *     returns 1: '4b6576'
760         // *     example 2: bin2hex(String.fromCharCode(0x00));
761         // *     returns 2: '00'
762         var v,i, f = 0, a = [];
763         s += '';
764         f = s.length;
765
766         for (i = 0; i<f; i++) {
767                 a[i] = s.charCodeAt(i).toString(16).replace(/^([\da-f])$/,"0$1");
768         }
769
770         return a.join('');
771 }
772
773 function groupChangeMember(gid, cid, sec_token) {
774         $('body .fakelink').css('cursor', 'wait');
775         $.get('group/' + gid + '/' + cid + "?t=" + sec_token, function(data) {
776                         $('#group-update-wrapper').html(data);
777                         $('body .fakelink').css('cursor', 'auto');
778         });
779 }
780
781 function profChangeMember(gid,cid) {
782         $('body .fakelink').css('cursor', 'wait');
783         $.get('profperm/' + gid + '/' + cid, function(data) {
784                         $('#prof-update-wrapper').html(data);
785                         $('body .fakelink').css('cursor', 'auto');
786         });
787 }
788
789 function contactgroupChangeMember(gid,cid) {
790         $('body').css('cursor', 'wait');
791         $.get('contactgroup/' + gid + '/' + cid, function(data) {
792                         $('body').css('cursor', 'auto');
793         });
794 }
795
796 function checkboxhighlight(box) {
797         if ($(box).is(':checked')) {
798                 $(box).addClass('checkeditem');
799         } else {
800                 $(box).removeClass('checkeditem');
801         }
802 }
803
804 function notifyMarkAll() {
805         $.get('notify/mark/all', function(data) {
806                 if (timer) {
807                         clearTimeout(timer);
808                 }
809                 timer = setTimeout(NavUpdate,1000);
810                 force_update = true;
811         });
812 }
813
814 /**
815  * sprintf in javascript
816  *      "{0} and {1}".format('zero','uno');
817  **/
818 String.prototype.format = function() {
819         var formatted = this;
820         for (var i = 0; i < arguments.length; i++) {
821                 var regexp = new RegExp('\\{'+i+'\\}', 'gi');
822                 formatted = formatted.replace(regexp, arguments[i]);
823         }
824         return formatted;
825 };
826 // Array Remove
827 Array.prototype.remove = function(item) {
828         to=undefined; from=this.indexOf(item);
829         var rest = this.slice((to || from) + 1 || this.length);
830         this.length = from < 0 ? this.length + from : from;
831         return this.push.apply(this, rest);
832 };
833
834 function previewTheme(elm) {
835         theme = $(elm).val();
836         $.getJSON('pretheme?f=&theme=' + theme,function(data) {
837                         $('#theme-preview').html('<div id="theme-desc">' + data.desc + '</div><div id="theme-version">' + data.version + '</div><div id="theme-credits">' + data.credits + '</div><a href="' + data.img + '"><img src="' + data.img + '" width="320" height="240" alt="' + theme + '" /></a>');
838         });
839
840 }
841
842 // notification permission settings in localstorage
843 // set by settings page
844 function getNotificationPermission() {
845         if (window["Notification"] === undefined) {
846                 return null;
847         }
848
849         if (Notification.permission === 'granted') {
850                 var val = localStorage.getItem('notification-permissions');
851                 if (val === null) {
852                         return 'denied';
853                 }
854                 return val;
855         } else {
856                 return Notification.permission;
857         }
858 }
859
860 /**
861  * Show a dialog loaded from an url
862  * By defaults this load the url in an iframe in colorbox
863  * Themes can overwrite `show()` function to personalize it
864  */
865 var Dialog = {
866         /**
867          * Show the dialog
868          *
869          * @param string url
870          * @return object colorbox
871          */
872         show : function (url) {
873                 var size = Dialog._get_size();
874                 return $.colorbox({href: url, iframe:true,innerWidth: size.width+'px',innerHeight: size.height+'px'})
875         },
876
877         /**
878          * Show the Image browser dialog
879          *
880          * @param string name
881          * @param string id (optional)
882          * @return object
883          *
884          * The name will be used to build the event name
885          * fired by image browser dialog when the user select
886          * an image. The optional id will be passed as argument
887          * to the event handler
888          */
889         doImageBrowser : function (name, id) {
890                 var url = Dialog._get_url("image",name,id);
891                 return Dialog.show(url);
892         },
893
894         /**
895          * Show the File browser dialog
896          *
897          * @param string name
898          * @param string id (optional)
899          * @return object
900          *
901          * The name will be used to build the event name
902          * fired by file browser dialog when the user select
903          * a file. The optional id will be passed as argument
904          * to the event handler
905          */
906         doFileBrowser : function (name, id) {
907                 var url = Dialog._get_url("file",name,id);
908                 return Dialog.show(url);
909         },
910
911         _get_url : function(type, name, id) {
912                 var hash = name;
913                 if (id !== undefined) {
914                         hash = hash + "-" + id;
915                 }
916                 return baseurl + "/fbrowser/"+type+"/?mode=minimal#"+hash;
917         },
918
919         _get_size: function() {
920                 return {
921                         width: window.innerWidth-50,
922                         height: window.innerHeight-100
923                 };
924         }
925 }
926 // @license-end