]> git.mxchange.org Git - friendica.git/blob - js/main.js
d3cce33040a6d8fc53b0b635fbfa1eb3e19644ab
[friendica.git] / js / main.js
1   function resizeIframe(obj) {
2     obj.style.height = 0;
3     obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
4   }
5
6   function openClose(theID) {
7     if(document.getElementById(theID).style.display == "block") { 
8       document.getElementById(theID).style.display = "none" 
9     }
10     else { 
11       document.getElementById(theID).style.display = "block" 
12     } 
13   }
14
15   function openMenu(theID) {
16       document.getElementById(theID).style.display = "block" 
17   }
18
19   function closeMenu(theID) {
20       document.getElementById(theID).style.display = "none" 
21   }
22
23
24
25         var src = null;
26         var prev = null;
27         var livetime = null;
28         var force_update = false;
29         var stopped = false;
30         var totStopped = false;
31         var timer = null;
32         var pr = 0;
33         var liking = 0;
34         var in_progress = false;
35         var langSelect = false;
36         var commentBusy = false;
37         var last_popup_menu = null;
38         var last_popup_button = null;
39
40         $(function() {
41                 $.ajaxSetup({cache: false});
42
43                 /* setup tooltips *//*
44                 $("a,.tt").each(function(){
45                         var e = $(this);
46                         var pos="bottom";
47                         if (e.hasClass("tttop")) pos="top";
48                         if (e.hasClass("ttbottom")) pos="bottom";
49                         if (e.hasClass("ttleft")) pos="left";
50                         if (e.hasClass("ttright")) pos="right";
51                         e.tipTip({defaultPosition: pos, edgeOffset: 8});
52                 });*/
53                 
54                 /* setup comment textarea buttons */
55                 
56                 $('[data-role="insert-formatting"]').on('click', function(e) {
57                         e.preventDefault();
58                         var o = $(this);
59                         var comment = o.data('comment');
60                         var bbcode  = o.data('bbcode');
61                         var id = o.data('id');
62                         if (bbcode=="img") {
63                                 $.colorbox({href: baseurl + "/fbrowser/image/?mode=minimal#comment-"+id, iframe:true,innerWidth:'500px',innerHeight:'400px'})
64                                 return; 
65                         }
66                         
67                         insertFormatting(comment, bbcode, id);
68                 });
69
70                 /* event from comment textarea button popups */
71                 /* insert returned bbcode at cursor position or replace selected text */
72                 $("body").on("fbrowser.image.comment", function(e, filename, bbcode, id) {
73                         console.log("on", id);
74                         $.colorbox.close();
75                         var textarea = document.getElementById("comment-edit-text-" +id);
76                         var start = textarea.selectionStart;
77                         var end = textarea.selectionEnd;
78                         textarea.value = textarea.value.substring(0, start) + bbcode + textarea.value.substring(end, textarea.value.length);
79                 });
80         
81                 
82                 
83                 /* setup onoff widgets */
84                 $(".onoff input").each(function(){
85                         val = $(this).val();
86                         id = $(this).attr("id");
87                         $("#"+id+"_onoff ."+ (val==0?"on":"off")).addClass("hidden");
88                         
89                 });
90                 $(".onoff > a").click(function(event){
91                         event.preventDefault(); 
92                         var input = $(this).siblings("input");
93                         var val = 1-input.val();
94                         var id = input.attr("id");
95                         $("#"+id+"_onoff ."+ (val==0?"on":"off")).addClass("hidden");
96                         $("#"+id+"_onoff ."+ (val==1?"on":"off")).removeClass("hidden");
97                         input.val(val);
98                         //console.log(id);
99                 });
100                 
101                 /* setup field_richtext */
102                 setupFieldRichtext();
103
104                 /* popup menus */
105                 function close_last_popup_menu() {
106                         if(last_popup_menu) {
107                                 last_popup_menu.hide();
108                                 last_popup_button.removeClass("selected");
109                                 last_popup_menu = null;
110                                 last_popup_button = null;
111                         }
112                 }
113                 $('a[rel^=#]').click(function(e){
114                         e.preventDefault();
115                         var parent = $(this).parent();
116                         var isSelected = (last_popup_button && parent.attr('id') == last_popup_button.attr('id'));
117                         close_last_popup_menu();
118                         if(isSelected) return false;
119                         menu = $( $(this).attr('rel') );
120                         e.preventDefault();
121                         e.stopPropagation();
122                         if (menu.attr('popup')=="false") return false;
123                         parent.toggleClass("selected");
124                         menu.toggle();
125                         if (menu.css("display") == "none") {
126                                 last_popup_menu = null;
127                                 last_popup_button = null;
128                         } else {
129                                 last_popup_menu = menu;
130                                 last_popup_button = parent;
131                         }
132                         return false;
133                 });
134                 $('html').click(function() {
135                         close_last_popup_menu();
136                 });
137                 
138                 // fancyboxes
139                 $("a.popupbox").colorbox({
140                         'inline' : true,
141                         'transition' : 'elastic'
142                 });
143                 
144
145                 /* notifications template */
146                 var notifications_tpl= unescape($("#nav-notifications-template[rel=template]").html());
147                 var notifications_all = unescape($('<div>').append( $("#nav-notifications-see-all").clone() ).html()); //outerHtml hack
148                 var notifications_mark = unescape($('<div>').append( $("#nav-notifications-mark-all").clone() ).html()); //outerHtml hack
149                 var notifications_empty = unescape($("#nav-notifications-menu").html());
150                 
151                 /* nav update event  */
152                 $('nav').bind('nav-update', function(e,data){;
153                         var invalid = $(data).find('invalid').text();
154                         if(invalid == 1) { window.location.href=window.location.href }
155
156                         var net = $(data).find('net').text();
157                         if(net == 0) { net = ''; $('#net-update').removeClass('show') } else { $('#net-update').addClass('show') }
158                         $('#net-update').html(net);
159
160                         var home = $(data).find('home').text();
161                         if(home == 0) { home = '';  $('#home-update').removeClass('show') } else { $('#home-update').addClass('show') }
162                         $('#home-update').html(home);
163                         
164                         var intro = $(data).find('intro').text();
165                         if(intro == 0) { intro = '';  $('#intro-update').removeClass('show') } else { $('#intro-update').addClass('show') }
166                         $('#intro-update').html(intro);
167
168                         var mail = $(data).find('mail').text();
169                         if(mail == 0) { mail = '';  $('#mail-update').removeClass('show') } else { $('#mail-update').addClass('show') }
170                         $('#mail-update').html(mail);
171                         
172                         var intro = $(data).find('intro').text();
173                         if(intro == 0) { intro = '';  $('#intro-update-li').removeClass('show') } else { $('#intro-update-li').addClass('show') }
174                         $('#intro-update-li').html(intro);
175
176                         var mail = $(data).find('mail').text();
177                         if(mail == 0) { mail = '';  $('#mail-update-li').removeClass('show') } else { $('#mail-update-li').addClass('show') }
178                         $('#mail-update-li').html(mail);
179
180
181                         var allevents = $(data).find('all-events').text();
182                         if(allevents == 0) { allevents = ''; $('#allevents-update').removeClass('show') } else { $('#allevents-update').addClass('show') }
183                         $('#allevents-update').html(allevents);
184
185                         var alleventstoday = $(data).find('all-events-today').text();
186                         if(alleventstoday == 0) { $('#allevents-update').removeClass('notif-allevents-today') } else { $('#allevents-update').addClass('notif-allevents-today') }
187
188                         var events = $(data).find('events').text();
189                         if(events == 0) { events = ''; $('#events-update').removeClass('show') } else { $('#events-update').addClass('show') }
190                         $('#events-update').html(events);
191
192                         var eventstoday = $(data).find('events-today').text();
193                         if(eventstoday == 0) { $('#events-update').removeClass('notif-events-today') } else { $('#events-update').addClass('notif-events-today') }
194
195                         var birthdays = $(data).find('birthdays').text();
196                         if(birthdays == 0) {birthdays = ''; $('#birthdays-update').removeClass('show') } else { $('#birthdays-update').addClass('show') }
197                         $('#birthdays-update').html(birthdays);
198
199                         var birthdaystoday = $(data).find('birthdays-today').text();
200                         if(birthdaystoday == 0) { $('#birthdays-update').removeClass('notif-birthdays-today') } else { $('#birthdays-update').addClass('notif-birthdays-today') }
201
202
203                         var eNotif = $(data).find('notif')
204                         
205                         if (eNotif.children("note").length==0){
206                                 $("#nav-notifications-menu").html(notifications_empty);
207                         } else {
208                                 nnm = $("#nav-notifications-menu");
209                                 nnm.html(notifications_all + notifications_mark);
210                                 //nnm.attr('popup','true');
211
212                                 var notification_lastitem = parseInt(localStorage.getItem("notification-lastitem"));
213                                 var notification_id = 0;
214                                 eNotif.children("note").each(function(){
215                                         e = $(this);
216                                         var text = e.text().format("<span class='contactname'>"+e.attr('name')+"</span>");
217                                         var seenclass = (e.attr('seen')==1)?"notify-seen":"notify-unseen";
218                                         var html = notifications_tpl.format(e.attr('href'),
219                                                 e.attr('photo'),                    // {0}
220                                                 text,                               // {1}
221                                                 e.attr('date'),                     // {2}
222                                                 seenclass,                          // {3}
223                                                 new Date(e.attr('timestamp')*1000)  // {4}
224                                         );
225                                         nnm.append(html);
226                                 });
227                                 $(eNotif.children("note").get().reverse()).each(function(){
228                                         e = $(this);
229                                         notification_id = parseInt(e.attr('timestamp'));
230                                         if (notification_lastitem!== null && notification_id > notification_lastitem) {
231                                                 if (getNotificationPermission()==="granted") {
232                                                         var notification = new Notification(document.title, {
233                                                                                           body: e.text().replace('&rarr; ','').format(e.attr('name')),
234                                                                                           icon: e.attr('photo'),
235                                                                                          });
236                                                         notification['url'] = e.attr('href');
237                                                         notification.addEventListener("click", function(ev){
238                                                                 window.location = ev.target.url;
239                                                         });
240                                                 }
241                                         }
242                                         
243                                 });
244                                 notification_lastitem = notification_id;
245                                 localStorage.setItem("notification-lastitem", notification_lastitem)
246
247                                 $("img[data-src]", nnm).each(function(i, el){
248                                         // Add src attribute for images with a data-src attribute
249                                         // However, don't bother if the data-src attribute is empty, because
250                                         // an empty "src" tag for an image will cause some browsers
251                                         // to prefetch the root page of the Friendica hub, which will
252                                         // unnecessarily load an entire profile/ or network/ page
253                                         if($(el).data("src") != '') $(el).attr('src', $(el).data("src"));
254                                 });
255                         }
256
257                         notif = eNotif.attr('count');
258                         if (notif>0){
259                                 $("#nav-notifications-linkmenu").addClass("on");
260                         } else {
261                                 $("#nav-notifications-linkmenu").removeClass("on");
262                         }
263                         if(notif == 0) { notif = ''; $('#notify-update').removeClass('show') } else { $('#notify-update').addClass('show') }
264                         $('#notify-update').html(notif);
265                         
266                         var eSysmsg = $(data).find('sysmsgs');
267                         eSysmsg.children("notice").each(function(){
268                                 text = $(this).text();
269                                 $.jGrowl(text, { sticky: true, theme: 'notice' });
270                         });
271                         eSysmsg.children("info").each(function(){
272                                 text = $(this).text();
273                                 $.jGrowl(text, { sticky: false, theme: 'info', life: 5000 });
274                         });
275                         
276                 });
277
278                 NavUpdate(); 
279                 // Allow folks to stop the ajax page updates with the pause/break key
280                 $(document).keydown(function(event) {
281                         if(event.keyCode == '8') {
282                                 var target = event.target || event.srcElement;
283                                 if (!/input|textarea/i.test(target.nodeName)) {
284                                         return false;
285                                 }
286                         }
287                         if(event.keyCode == '19' || (event.ctrlKey && event.which == '32')) {
288                                 event.preventDefault();
289                                 if(stopped == false) {
290                                         stopped = true;
291                                         if (event.ctrlKey) {
292                                                 totStopped = true;
293                                         }
294                                         $('#pause').html('<img src="images/pause.gif" alt="pause" style="border: 1px solid black;" />');
295                                 } else {
296                                         unpause();
297                                 }
298                         } else {
299                                 if (!totStopped) {
300                                         unpause();
301                                 }
302                         }
303                 });
304                 
305                 
306         });
307
308         function NavUpdate() {
309
310                 if(! stopped) {
311                         var pingCmd = 'ping' + ((localUser != 0) ? '?f=&uid=' + localUser : '');
312                         $.get(pingCmd,function(data) {
313                                 $(data).find('result').each(function() {
314                                         // send nav-update event
315                                         $('nav').trigger('nav-update', this);
316                                         
317                                         
318                                         // start live update
319
320                                         if($('#live-network').length)   { src = 'network'; liveUpdate(); }
321                                         if($('#live-profile').length)   { src = 'profile'; liveUpdate(); }
322                                         if($('#live-community').length) { src = 'community'; liveUpdate(); }
323                                         if($('#live-notes').length)     { src = 'notes'; liveUpdate(); }
324                                         if($('#live-display').length)     { src = 'display'; liveUpdate(); }
325 /*                                      if($('#live-display').length) {
326                                                 if(liking) {
327                                                         liking = 0;
328                                                         window.location.href=window.location.href 
329                                                 }
330                                         }*/
331                                         if($('#live-photos').length) { 
332                                                 if(liking) {
333                                                         liking = 0;
334                                                         window.location.href=window.location.href 
335                                                 }
336                                         }
337
338                                         
339                                         
340                                         
341                                 });
342                         }) ;
343                 }
344                 timer = setTimeout(NavUpdate,updateInterval);
345         }
346
347         function liveUpdate() {
348                 if((src == null) || (stopped) || (! profile_uid)) { $('.like-rotator').hide(); return; }
349                 if(($('.comment-edit-text-full').length) || (in_progress)) {
350                         if(livetime) {
351                                 clearTimeout(livetime);
352                         }
353                         livetime = setTimeout(liveUpdate, 5000);
354                         return;
355                 }
356                 if(livetime != null)
357                         livetime = null;
358
359                 prev = 'live-' + src;
360
361                 in_progress = true;
362
363                 if ($(document).scrollTop() == 0)
364                         force_update = true;
365
366                 var udargs = ((netargs.length) ? '/' + netargs : '');
367                 var update_url = 'update_' + src + udargs + '&p=' + profile_uid + '&page=' + profile_page + '&force=' + ((force_update) ? 1 : 0);
368
369                 $.get(update_url,function(data) {
370                         in_progress = false;
371                         force_update = false;
372                         //                      $('.collapsed-comments',data).each(function() {
373                         //      var ident = $(this).attr('id');
374                         //      var is_hidden = $('#' + ident).is(':hidden');
375                         //      if($('#' + ident).length) {
376                         //              $('#' + ident).replaceWith($(this));
377                         //              if(is_hidden)
378                         //                      $('#' + ident).hide();
379                         //      }
380                         //});
381
382                         // add a new thread
383                         $('.toplevel_item',data).each(function() {
384                                 var ident = $(this).attr('id');
385
386                                 if($('#' + ident).length == 0 && profile_page == 1) {
387                                         $('img',this).each(function() {
388                                                 $(this).attr('src',$(this).attr('dst'));
389                                         });
390                                         $('#' + prev).after($(this));
391                                 }
392                                 else {
393                                         // Find out if the hidden comments are open, so we can keep it that way
394                                         // if a new comment has been posted
395                                         var id = $('.hide-comments-total', this).attr('id');
396                                         if(typeof id != 'undefined') {
397                                                 id = id.split('-')[3];
398                                                 var commentsOpen = $("#collapsed-comments-" + id).is(":visible");
399                                         }
400
401                                         $('img',this).each(function() {
402                                                 $(this).attr('src',$(this).attr('dst'));
403                                         });
404                                         //vScroll = $(document).scrollTop();
405                                         $('html').height($('html').height());
406                                         $('#' + ident).replaceWith($(this));
407
408                                         if(typeof id != 'undefined') {
409                                                 if(commentsOpen) showHideComments(id);
410                                         }
411                                         $('html').height('auto');
412                                         //$(document).scrollTop(vScroll);
413                                 }
414                                 prev = ident;
415                         });
416
417                         // reset vars for inserting individual items
418
419                         /*                      prev = 'live-' + src;
420
421                         $('.wall-item-outside-wrapper',data).each(function() {
422                                 var ident = $(this).attr('id');
423
424                                 if($('#' + ident).length == 0 && prev != 'live-' + src) {
425                                                 $('img',this).each(function() {
426                                                         $(this).attr('src',$(this).attr('dst'));
427                                                 });
428                                                 $('#' + prev).after($(this));
429                                 }
430                                 else { 
431                                         $('#' + ident + ' ' + '.wall-item-ago').replaceWith($(this).find('.wall-item-ago')); 
432                                         if($('#' + ident + ' ' + '.comment-edit-text-empty').length)
433                                                 $('#' + ident + ' ' + '.wall-item-comment-wrapper').replaceWith($(this).find('.wall-item-comment-wrapper'));
434                                         $('#' + ident + ' ' + '.hide-comments-total').replaceWith($(this).find('.hide-comments-total'));
435                                         $('#' + ident + ' ' + '.wall-item-like').replaceWith($(this).find('.wall-item-like'));
436                                         $('#' + ident + ' ' + '.wall-item-dislike').replaceWith($(this).find('.wall-item-dislike'));
437                                         $('#' + ident + ' ' + '.my-comment-photo').each(function() {
438                                                 $(this).attr('src',$(this).attr('dst'));
439                                         });
440                                 }
441                                 prev = ident; 
442                         });
443                         */
444                         $('.like-rotator').hide();
445                         if(commentBusy) {
446                                 commentBusy = false;
447                                 $('body').css('cursor', 'auto');
448                         }
449                         /* autocomplete @nicknames */
450                         $(".comment-edit-form  textarea").contact_autocomplete(baseurl+"/acl");
451
452                         // setup videos, since VideoJS won't take care of any loaded via AJAX
453                         if(typeof videojs != 'undefined') videojs.autoSetup();
454                 });
455         }
456
457         function imgbright(node) {
458                 $(node).removeClass("drophide").addClass("drop");
459         }
460
461         function imgdull(node) {
462                 $(node).removeClass("drop").addClass("drophide");
463         }
464
465         // Since our ajax calls are asynchronous, we will give a few 
466         // seconds for the first ajax call (setting like/dislike), then 
467         // run the updater to pick up any changes and display on the page.
468         // The updater will turn any rotators off when it's done. 
469         // This function will have returned long before any of these
470         // events have completed and therefore there won't be any
471         // visible feedback that anything changed without all this
472         // trickery. This still could cause confusion if the "like" ajax call
473         // is delayed and NavUpdate runs before it completes.
474
475         function dolike(ident,verb) {
476                 unpause();
477                 $('#like-rotator-' + ident.toString()).show();
478                 $.get('like/' + ident.toString() + '?verb=' + verb, NavUpdate );
479                 liking = 1;
480                 force_update = true;
481         }
482
483         function dosubthread(ident) {
484                 unpause();
485                 $('#like-rotator-' + ident.toString()).show();
486                 $.get('subthread/' + ident.toString(), NavUpdate );
487                 liking = 1;
488         }
489
490
491         function dostar(ident) {
492                 ident = ident.toString();
493                 $('#like-rotator-' + ident).show();
494                 $.get('starred/' + ident, function(data) {
495                         if(data.match(/1/)) {
496                                 $('#starred-' + ident).addClass('starred');
497                                 $('#starred-' + ident).removeClass('unstarred');
498                                 $('#star-' + ident).addClass('hidden');
499                                 $('#unstar-' + ident).removeClass('hidden');
500                         }
501                         else {
502                                 $('#starred-' + ident).addClass('unstarred');
503                                 $('#starred-' + ident).removeClass('starred');
504                                 $('#star-' + ident).removeClass('hidden');
505                                 $('#unstar-' + ident).addClass('hidden');
506                         }
507                         $('#like-rotator-' + ident).hide();
508                 });
509         }
510
511         function doignore(ident) {
512                 ident = ident.toString();
513                 $('#like-rotator-' + ident).show();
514                 $.get('ignored/' + ident, function(data) {
515                         if(data.match(/1/)) {
516                                 $('#ignored-' + ident).addClass('ignored');
517                                 $('#ignored-' + ident).removeClass('unignored');
518                                 $('#ignore-' + ident).addClass('hidden');
519                                 $('#unignore-' + ident).removeClass('hidden');
520                         }
521                         else {
522                                 $('#ignored-' + ident).addClass('unignored');
523                                 $('#ignored-' + ident).removeClass('ignored');
524                                 $('#ignore-' + ident).removeClass('hidden');
525                                 $('#unignore-' + ident).addClass('hidden');
526                         }
527                         $('#like-rotator-' + ident).hide();
528                 });
529         }
530
531         function getPosition(e) {
532                 var cursor = {x:0, y:0};
533                 if ( e.pageX || e.pageY  ) {
534                         cursor.x = e.pageX;
535                         cursor.y = e.pageY;
536                 }
537                 else {
538                         if( e.clientX || e.clientY ) {
539                                 cursor.x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
540                                 cursor.y = e.clientY + (document.documentElement.scrollTop  || document.body.scrollTop)  - document.documentElement.clientTop;
541                         }
542                         else {
543                                 if( e.x || e.y ) {
544                                         cursor.x = e.x;
545                                         cursor.y = e.y;
546                                 }
547                         }
548                 }
549                 return cursor;
550         }
551
552         var lockvisible = false;
553
554         function lockview(event,id) {
555                 event = event || window.event;
556                 cursor = getPosition(event);
557                 if(lockvisible) {
558                         lockviewhide();
559                 }
560                 else {
561                         lockvisible = true;
562                         $.get('lockview/' + id, function(data) {
563                                 $('#panel').html(data);
564                                 $('#panel').css({ 'left': cursor.x + 5 , 'top': cursor.y + 5});
565                                 $('#panel').show();
566                         });
567                 }
568         }
569
570         function lockviewhide() {
571                 lockvisible = false;
572                 $('#panel').hide();
573         }
574
575         function post_comment(id) {
576                 unpause();
577                 commentBusy = true;
578                 $('body').css('cursor', 'wait');
579                 $("#comment-preview-inp-" + id).val("0");
580                 $.post(  
581              "item",  
582              $("#comment-edit-form-" + id).serialize(),
583                         function(data) {
584                                 if(data.success) {
585                                         $("#comment-edit-wrapper-" + id).hide();
586                                         $("#comment-edit-text-" + id).val('');
587                                 var tarea = document.getElementById("comment-edit-text-" + id);
588                                         if(tarea)
589                                                 commentClose(tarea,id);
590                                         if(timer) clearTimeout(timer);
591                                         timer = setTimeout(NavUpdate,10);
592                                         force_update = true;
593                                 }
594                                 if(data.reload) {
595                                         window.location.href=data.reload;
596                                 }
597                         },
598                         "json"  
599          );  
600          return false;  
601         }
602
603
604         function preview_comment(id) {
605                 $("#comment-preview-inp-" + id).val("1");
606                 $("#comment-edit-preview-" + id).show();
607                 $.post(  
608              "item",  
609              $("#comment-edit-form-" + id).serialize(),
610                         function(data) {
611                                 if(data.preview) {
612                                                 
613                                         $("#comment-edit-preview-" + id).html(data.preview);
614                                         $("#comment-edit-preview-" + id + " a").click(function() { return false; });
615                                 }
616                         },
617                         "json"  
618          );  
619          return true;  
620         }
621
622
623
624         function showHideComments(id) {
625                 if( $("#collapsed-comments-" + id).is(":visible")) {
626                         $("#collapsed-comments-" + id).hide();
627                         $("#hide-comments-" + id).html(window.showMore);
628                 }
629                 else {
630                         $("#collapsed-comments-" + id).show();
631                         $("#hide-comments-" + id).html(window.showFewer);
632                 }
633         }
634
635
636
637         function preview_post() {
638                 $("#jot-preview").val("1");
639                 $("#jot-preview-content").show();
640                 tinyMCE.triggerSave();
641                 $.post(  
642                         "item",  
643                         $("#profile-jot-form").serialize(),
644                         function(data) {
645                                 if(data.preview) {                      
646                                         $("#jot-preview-content").html(data.preview);
647                                         $("#jot-preview-content" + " a").click(function() { return false; });
648                                 }
649                         },
650                         "json"  
651                 );  
652                 $("#jot-preview").val("0");
653                 return true;  
654         }
655
656
657         function unpause() {
658                 // unpause auto reloads if they are currently stopped
659                 totStopped = false;
660                 stopped = false;
661             $('#pause').html('');
662         }
663                 
664
665     function bin2hex(s){  
666         // Converts the binary representation of data to hex    
667         //   
668         // version: 812.316  
669         // discuss at: http://phpjs.org/functions/bin2hex  
670         // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)  
671         // +   bugfixed by: Onno Marsman  
672         // +   bugfixed by: Linuxworld  
673         // *     example 1: bin2hex('Kev');  
674         // *     returns 1: '4b6576'  
675         // *     example 2: bin2hex(String.fromCharCode(0x00));  
676         // *     returns 2: '00'  
677         var v,i, f = 0, a = [];  
678         s += '';  
679         f = s.length;  
680           
681         for (i = 0; i<f; i++) {  
682             a[i] = s.charCodeAt(i).toString(16).replace(/^([\da-f])$/,"0$1");  
683         }  
684           
685         return a.join('');  
686     }  
687
688         function groupChangeMember(gid, cid, sec_token) {
689                 $('body .fakelink').css('cursor', 'wait');
690                 $.get('group/' + gid + '/' + cid + "?t=" + sec_token, function(data) {
691                                 $('#group-update-wrapper').html(data);
692                                 $('body .fakelink').css('cursor', 'auto');                              
693                 });
694         }
695
696         function profChangeMember(gid,cid) {
697                 $('body .fakelink').css('cursor', 'wait');
698                 $.get('profperm/' + gid + '/' + cid, function(data) {
699                                 $('#prof-update-wrapper').html(data);
700                                 $('body .fakelink').css('cursor', 'auto');                              
701                 });
702         }
703
704         function contactgroupChangeMember(gid,cid) {
705                 $('body').css('cursor', 'wait');
706                 $.get('contactgroup/' + gid + '/' + cid, function(data) {
707                                 $('body').css('cursor', 'auto');
708                 });
709         }
710
711
712 function checkboxhighlight(box) {
713   if($(box).is(':checked')) {
714         $(box).addClass('checkeditem');
715   }
716   else {
717         $(box).removeClass('checkeditem');
718   }
719 }
720
721 function notifyMarkAll() {
722         $.get('notify/mark/all', function(data) {
723                 if(timer) clearTimeout(timer);
724                 timer = setTimeout(NavUpdate,1000);
725                 force_update = true;
726         });
727 }
728
729
730 // code from http://www.tinymce.com/wiki.php/How-to_implement_a_custom_file_browser
731 function fcFileBrowser (field_name, url, type, win) {
732     /* TODO: If you work with sessions in PHP and your client doesn't accept cookies you might need to carry
733        the session name and session ID in the request string (can look like this: "?PHPSESSID=88p0n70s9dsknra96qhuk6etm5").
734        These lines of code extract the necessary parameters and add them back to the filebrowser URL again. */
735
736
737     var cmsURL = baseurl+"/fbrowser/"+type+"/";
738
739     tinyMCE.activeEditor.windowManager.open({
740         file : cmsURL,
741         title : 'File Browser',
742         width : 420,  // Your dimensions may differ - toy around with them!
743         height : 400,
744         resizable : "yes",
745         inline : "yes",  // This parameter only has an effect if you use the inlinepopups plugin!
746         close_previous : "no"
747     }, {
748         window : win,
749         input : field_name
750     });
751     return false;
752   }
753
754 function setupFieldRichtext(){
755         tinyMCE.init({
756                 theme : "advanced",
757                 mode : "specific_textareas",
758                 editor_selector: "fieldRichtext",
759                 plugins : "bbcode,paste, inlinepopups",
760                 theme_advanced_buttons1 : "bold,italic,underline,undo,redo,link,unlink,image,forecolor,formatselect,code",
761                 theme_advanced_buttons2 : "",
762                 theme_advanced_buttons3 : "",
763                 theme_advanced_toolbar_location : "top",
764                 theme_advanced_toolbar_align : "center",
765                 theme_advanced_blockformats : "blockquote,code",
766                 theme_advanced_resizing : true,
767                 paste_text_sticky : true,
768                 entity_encoding : "raw",
769                 add_unload_trigger : false,
770                 remove_linebreaks : false,
771                 //force_p_newlines : false,
772                 //force_br_newlines : true,
773                 forced_root_block : 'div',
774                 convert_urls: false,
775                 content_css: baseurl+"/view/custom_tinymce.css",
776                 theme_advanced_path : false,
777                 file_browser_callback : "fcFileBrowser",
778         });
779 }
780
781
782 /** 
783  * sprintf in javascript 
784  *      "{0} and {1}".format('zero','uno'); 
785  **/
786 String.prototype.format = function() {
787     var formatted = this;
788     for (var i = 0; i < arguments.length; i++) {
789         var regexp = new RegExp('\\{'+i+'\\}', 'gi');
790         formatted = formatted.replace(regexp, arguments[i]);
791     }
792     return formatted;
793 };
794 // Array Remove
795 Array.prototype.remove = function(item) {
796   to=undefined; from=this.indexOf(item);
797   var rest = this.slice((to || from) + 1 || this.length);
798   this.length = from < 0 ? this.length + from : from;
799   return this.push.apply(this, rest);
800 };
801
802 function previewTheme(elm) {
803         theme = $(elm).val();
804         $.getJSON('pretheme?f=&theme=' + theme,function(data) {
805                         $('#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>');
806         });
807
808 }
809
810 // notification permission settings in localstorage
811 // set by settings page
812 function getNotificationPermission() {
813         if (window["Notification"] === undefined) {
814                 return null;
815         }
816     if (Notification.permission === 'granted') {
817         var val = localStorage.getItem('notification-permissions');
818                 if (val === null) return 'denied';
819                 return val;
820     } else {
821         return Notification.permission;
822     }
823 }