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