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