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