]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - js/util.js
72750c0271853edc492a68607783bac0a8895f6d
[quix0rs-gnu-social.git] / js / util.js
1 /*
2  * StatusNet - a distributed open-source microblogging tool
3  * Copyright (C) 2008, StatusNet, Inc.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @category  UI interaction
19  * @package   StatusNet
20  * @author    Sarven Capadisli <csarven@status.net>
21  * @author    Evan Prodromou <evan@status.net>
22  * @copyright 2009 StatusNet, Inc.
23  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
24  * @link      http://status.net/
25  */
26
27 var SN = { // StatusNet
28     C: { // Config
29         I: { // Init
30             CounterBlackout: false,
31             MaxLength: 140,
32             PatternUsername: /^[0-9a-zA-Z\-_.]*$/,
33             HTTP20x30x: [200, 201, 202, 203, 204, 205, 206, 300, 301, 302, 303, 304, 305, 306, 307]
34         },
35
36         S: { // Selector
37             Disabled: 'disabled',
38             Warning: 'warning',
39             Error: 'error',
40             Success: 'success',
41             Processing: 'processing',
42             CommandResult: 'command_result',
43             FormNotice: 'form_notice',
44             NoticeDataText: 'notice_data-text',
45             NoticeTextCount: 'notice_text-count',
46             NoticeInReplyTo: 'notice_in-reply-to',
47             NoticeDataAttach: 'notice_data-attach',
48             NoticeDataAttachSelected: 'notice_data-attach_selected',
49             NoticeActionSubmit: 'notice_action-submit',
50             NoticeLat: 'notice_data-lat',
51             NoticeLon: 'notice_data-lon',
52             NoticeLocationId: 'notice_data-location_id',
53             NoticeLocationNs: 'notice_data-location_ns',
54             NoticeGeoName: 'notice_data-geo_name',
55             NoticeDataGeo: 'notice_data-geo',
56             NoticeDataGeoSelected: 'notice_data-geo_selected'
57         }
58     },
59
60     U: { // Utils
61         FormNoticeEnhancements: function(form) {
62             form_id = form.attr('id');
63
64             if (jQuery.data(form[0], 'ElementData') === undefined) {
65                 MaxLength = $('#'+form_id+' #'+SN.C.S.NoticeTextCount).text();
66                 if (typeof(MaxLength) == 'undefined') {
67                      MaxLength = SN.C.I.MaxLength;
68                 }
69                 jQuery.data(form[0], 'ElementData', {MaxLength:MaxLength});
70
71                 SN.U.Counter(form);
72
73                 NDT = $('#'+form_id+' #'+SN.C.S.NoticeDataText);
74
75                 NDT.bind('keyup', function(e) {
76                     SN.U.Counter(form);
77                 });
78
79                 NDT.bind('keydown', function(e) {
80                     SN.U.SubmitOnReturn(e, form);
81                 });
82             }
83             else {
84                 $('#'+form_id+' #'+SN.C.S.NoticeTextCount).text(jQuery.data(form[0], 'ElementData').MaxLength);
85             }
86
87             if ($('body')[0].id != 'conversation') {
88                 $('#'+form_id+' textarea').focus();
89             }
90         },
91
92         SubmitOnReturn: function(event, el) {
93             if (event.keyCode == 13 || event.keyCode == 10) {
94                 el.submit();
95                 event.preventDefault();
96                 event.stopPropagation();
97                 $('#'+el[0].id+' #'+SN.C.S.NoticeDataText).blur();
98                 $('body').focus();
99                 return false;
100             }
101             return true;
102         },
103
104         Counter: function(form) {
105             SN.C.I.FormNoticeCurrent = form;
106             form_id = form.attr('id');
107
108             var MaxLength = jQuery.data(form[0], 'ElementData').MaxLength;
109
110             if (MaxLength <= 0) {
111                 return;
112             }
113
114             var remaining = MaxLength - $('#'+form_id+' #'+SN.C.S.NoticeDataText).val().length;
115             var counter = $('#'+form_id+' #'+SN.C.S.NoticeTextCount);
116
117             if (remaining.toString() != counter.text()) {
118                 if (!SN.C.I.CounterBlackout || remaining === 0) {
119                     if (counter.text() != String(remaining)) {
120                         counter.text(remaining);
121                     }
122                     if (remaining < 0) {
123                         form.addClass(SN.C.S.Warning);
124                     } else {
125                         form.removeClass(SN.C.S.Warning);
126                     }
127                     // Skip updates for the next 500ms.
128                     // On slower hardware, updating on every keypress is unpleasant.
129                     if (!SN.C.I.CounterBlackout) {
130                         SN.C.I.CounterBlackout = true;
131                         SN.C.I.FormNoticeCurrent = form;
132                         window.setTimeout("SN.U.ClearCounterBlackout(SN.C.I.FormNoticeCurrent);", 500);
133                     }
134                 }
135             }
136         },
137
138         ClearCounterBlackout: function(form) {
139             // Allow keyup events to poke the counter again
140             SN.C.I.CounterBlackout = false;
141             // Check if the string changed since we last looked
142             SN.U.Counter(form);
143         },
144
145         FormXHR: function(f) {
146             if (jQuery.data(f[0], "ElementData") === undefined) {
147                 jQuery.data(f[0], "ElementData", {Bind:'submit'});
148                 f.bind('submit', function(e) {
149                     form_id = $(this)[0].id;
150                     $.ajax({
151                         type: 'POST',
152                         dataType: 'xml',
153                         url: $(this)[0].action,
154                         data: $(this).serialize() + '&ajax=1',
155                         beforeSend: function(xhr) {
156                             $('#'+form_id).addClass(SN.C.S.Processing);
157                             $('#'+form_id+' .submit').addClass(SN.C.S.Disabled);
158                             $('#'+form_id+' .submit').attr(SN.C.S.Disabled, SN.C.S.Disabled);
159                         },
160                         error: function (xhr, textStatus, errorThrown) {
161                             alert(errorThrown || textStatus);
162                         },
163                         success: function(data, textStatus) {
164                             if (typeof($('form', data)[0]) != 'undefined') {
165                                 form_new = document._importNode($('form', data)[0], true);
166                                 $('#'+form_id).replaceWith(form_new);
167                                 $('#'+form_new.id).each(function() { SN.U.FormXHR($(this)); });
168                             }
169                             else {
170                                 $('#'+form_id).replaceWith(document._importNode($('p', data)[0], true));
171                             }
172                         }
173                     });
174                     return false;
175                 });
176             }
177         },
178
179         FormNoticeXHR: function(form) {
180             var NDG, NLat, NLon, NLNS, NLID;
181             form_id = form.attr('id');
182             form.append('<input type="hidden" name="ajax" value="1"/>');
183             form.ajaxForm({
184                 dataType: 'xml',
185                 timeout: '60000',
186                 beforeSend: function(formData) {
187                     if ($('#'+form_id+' #'+SN.C.S.NoticeDataText)[0].value.length === 0) {
188                         form.addClass(SN.C.S.Warning);
189                         return false;
190                     }
191                     form.addClass(SN.C.S.Processing);
192                     $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).addClass(SN.C.S.Disabled);
193                     $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).attr(SN.C.S.Disabled, SN.C.S.Disabled);
194
195                     NDG = $('#'+SN.C.S.NoticeDataGeo).attr('checked');
196                     NLat = $('#'+SN.C.S.NoticeLat).val();
197                     NLon = $('#'+SN.C.S.NoticeLon).val();
198                     NLNS = $('#'+SN.C.S.NoticeLocationNs).val();
199                     NLID = $('#'+SN.C.S.NoticeLocationId).val();
200
201                     return true;
202                 },
203                 error: function (xhr, textStatus, errorThrown) {
204                     form.removeClass(SN.C.S.Processing);
205                     $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);
206                     $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled, SN.C.S.Disabled);
207                     $('#'+form_id+' .form_response').remove();
208                     if (textStatus == 'timeout') {
209                         form.append('<p class="form_response error">Sorry! We had trouble sending your notice. The servers are overloaded. Please try again, and contact the site administrator if this problem persists.</p>');
210                     }
211                     else {
212                         if ($('.'+SN.C.S.Error, xhr.responseXML).length > 0) {
213                             form.append(document._importNode($('.'+SN.C.S.Error, xhr.responseXML)[0], true));
214                         }
215                         else {
216                             if (parseInt(xhr.status) === 0 || jQuery.inArray(parseInt(xhr.status), SN.C.I.HTTP20x30x) >= 0) {
217                                 $('#'+form_id).resetForm();
218                                 $('#'+form_id+' #'+SN.C.S.NoticeDataAttachSelected).remove();
219                                 SN.U.FormNoticeEnhancements($('#'+form_id));
220                             }
221                             else {
222                                 form.append('<p class="form_response error">(Sorry! We had trouble sending your notice ('+xhr.status+' '+xhr.statusText+'). Please report the problem to the site administrator if this happens again.</p>');
223                             }
224                         }
225                     }
226                 },
227                 success: function(data, textStatus) {
228                     $('#'+form_id+' .form_response').remove();
229                     var result;
230                     if ($('#'+SN.C.S.Error, data).length > 0) {
231                         result = document._importNode($('p', data)[0], true);
232                         result = result.textContent || result.innerHTML;
233                         form.append('<p class="form_response error">'+result+'</p>');
234                     }
235                     else {
236                         if($('body')[0].id == 'bookmarklet') {
237                             self.close();
238                         }
239
240                         if ($('#'+SN.C.S.CommandResult, data).length > 0) {
241                             result = document._importNode($('p', data)[0], true);
242                             result = result.textContent || result.innerHTML;
243                             form.append('<p class="form_response success">'+result+'</p>');
244                         }
245                         else {
246                             var notices = $('#notices_primary .notices');
247                             if (notices.length > 0) {
248                                 var notice = document._importNode($('li', data)[0], true);
249                                 if ($('#'+notice.id).length === 0) {
250                                     var notice_irt_value = $('#'+SN.C.S.NoticeInReplyTo).val();
251                                     var notice_irt = '#notices_primary #notice-'+notice_irt_value;
252                                     if($('body')[0].id == 'conversation') {
253                                         if(notice_irt_value.length > 0 && $(notice_irt+' .notices').length < 1) {
254                                             $(notice_irt).append('<ul class="notices"></ul>');
255                                         }
256                                         $($(notice_irt+' .notices')[0]).append(notice);
257                                     }
258                                     else {
259                                         notices.prepend(notice);
260                                     }
261                                     $('#'+notice.id).css({display:'none'});
262                                     $('#'+notice.id).fadeIn(2500);
263                                     SN.U.NoticeWithAttachment($('#'+notice.id));
264                                     SN.U.NoticeReplyTo($('#'+notice.id));
265                                     SN.U.FormXHR($('#'+notice.id+' .form_favor'));
266                                 }
267                             }
268                             else {
269                                 result = document._importNode($('title', data)[0], true);
270                                 result_title = result.textContent || result.innerHTML;
271                                 form.append('<p class="form_response success">'+result_title+'</p>');
272                             }
273                         }
274                         $('#'+form_id).resetForm();
275                         $('#'+form_id+' #'+SN.C.S.NoticeDataAttachSelected).remove();
276                         SN.U.FormNoticeEnhancements($('#'+form_id));
277                     }
278                 },
279                 complete: function(xhr, textStatus) {
280                     form.removeClass(SN.C.S.Processing);
281                     $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).removeAttr(SN.C.S.Disabled);
282                     $('#'+form_id+' #'+SN.C.S.NoticeActionSubmit).removeClass(SN.C.S.Disabled);
283
284                     $('#'+SN.C.S.NoticeDataGeo).attr('checked', NDG);
285                     $('#'+SN.C.S.NoticeLat).val(NLat);
286                     $('#'+SN.C.S.NoticeLon).val(NLon);
287                     $('#'+SN.C.S.NoticeLocationNs).val(NLNS);
288                     $('#'+SN.C.S.NoticeLocationId).val(NLID);
289                 }
290             });
291         },
292
293         NoticeReply: function() {
294             if ($('#'+SN.C.S.NoticeDataText).length > 0 && $('#content .notice_reply').length > 0) {
295                 $('#content .notice').each(function() { SN.U.NoticeReplyTo($(this)); });
296             }
297         },
298
299         NoticeReplyTo: function(notice_item) {
300             var notice = notice_item[0];
301             var notice_reply = $('.notice_reply', notice)[0];
302
303             if (jQuery.data(notice_reply, "ElementData") === undefined) {
304                 jQuery.data(notice_reply, "ElementData", {Bind:'submit'});
305                 $(notice_reply).bind('click', function() {
306                     var nickname = ($('.author .nickname', notice).length > 0) ? $($('.author .nickname', notice)[0]) : $('.author .nickname.uid');
307                     SN.U.NoticeReplySet(nickname.text(), $($('.notice_id', notice)[0]).text());
308                     return false;
309                 });
310             }
311         },
312
313         NoticeReplySet: function(nick,id) {
314             if (nick.match(SN.C.I.PatternUsername)) {
315                 var text = $('#'+SN.C.S.NoticeDataText);
316                 if (text.length > 0) {
317                     replyto = '@' + nick + ' ';
318                     text.val(replyto + text.val().replace(RegExp(replyto, 'i'), ''));
319                     $('#'+SN.C.S.FormNotice+' #'+SN.C.S.NoticeInReplyTo).val(id);
320
321                     text[0].focus();
322                     if (text[0].setSelectionRange) {
323                         var len = text.val().length;
324                         text[0].setSelectionRange(len,len);
325                     }
326                 }
327             }
328         },
329
330         NoticeFavor: function() {
331             $('.form_favor').each(function() { SN.U.FormXHR($(this)); });
332             $('.form_disfavor').each(function() { SN.U.FormXHR($(this)); });
333         },
334
335         NoticeRepeat: function() {
336             $('.form_repeat').each(function() {
337                 SN.U.FormXHR($(this));
338                 SN.U.NoticeRepeatConfirmation($(this));
339             });
340         },
341
342         NoticeRepeatConfirmation: function(form) {
343             function NRC() {
344                 form.closest('.notice-options').addClass('opaque');
345                 form.addClass('dialogbox');
346
347                 form.append('<button class="close">&#215;</button>');
348                 form.find('button.close').click(function(){
349                     $(this).remove();
350
351                     form.closest('.notice-options').removeClass('opaque');
352                     form.removeClass('dialogbox');
353                     form.find('.submit_dialogbox').remove();
354                     form.find('.submit').show();
355
356                     return false;
357                 });
358             };
359
360             form.find('.submit').bind('click', function(e) {
361                 e.preventDefault();
362
363                 var submit = form.find('.submit').clone();
364                 submit.addClass('submit_dialogbox');
365                 submit.removeClass('submit');
366                 form.append(submit);
367
368                 $(this).hide();
369
370                 NRC();
371             });
372         },
373
374         NoticeAttachments: function() {
375             $('.notice a.attachment').each(function() {
376                 SN.U.NoticeWithAttachment($(this).closest('.notice'));
377             });
378         },
379
380         NoticeWithAttachment: function(notice) {
381             if ($('.attachment', notice).length === 0) {
382                 return;
383             }
384
385             var notice_id = notice.attr('id');
386
387             $.fn.jOverlay.options = {
388                 method : 'GET',
389                 data : '',
390                 url : '',
391                 color : '#000',
392                 opacity : '0.6',
393                 zIndex : 9999,
394                 center : false,
395                 imgLoading : $('address .url')[0].href+'theme/base/images/illustrations/illu_progress_loading-01.gif',
396                 bgClickToClose : true,
397                 success : function() {
398                     $('#jOverlayContent').append('<button class="close">&#215;</button>');
399                     $('#jOverlayContent button').click($.closeOverlay);
400                 },
401                 timeout : 0,
402                 autoHide : true,
403                 css : {'max-width':'542px', 'top':'5%', 'left':'32.5%'}
404             };
405
406             $('#'+notice_id+' a.attachment').click(function() {
407                 $().jOverlay({url: $('address .url')[0].href+'attachment/' + ($(this).attr('id').substring('attachment'.length + 1)) + '/ajax'});
408                 return false;
409             });
410
411             var t;
412             $("body:not(#shownotice) #"+notice_id+" a.thumbnail").hover(
413                 function() {
414                     var anchor = $(this);
415                     $("a.thumbnail").children('img').hide();
416                     anchor.closest(".entry-title").addClass('ov');
417
418                     if (anchor.children('img').length === 0) {
419                         t = setTimeout(function() {
420                             $.get($('address .url')[0].href+'attachment/' + (anchor.attr('id').substring('attachment'.length + 1)) + '/thumbnail', null, function(data) {
421                                 anchor.append(data);
422                             });
423                         }, 500);
424                     }
425                     else {
426                         anchor.children('img').show();
427                     }
428                 },
429                 function() {
430                     clearTimeout(t);
431                     $("a.thumbnail").children('img').hide();
432                     $(this).closest(".entry-title").removeClass('ov');
433                 }
434             );
435         },
436
437         NoticeDataAttach: function() {
438             NDA = $('#'+SN.C.S.NoticeDataAttach);
439             NDA.change(function() {
440                 S = '<div id="'+SN.C.S.NoticeDataAttachSelected+'" class="'+SN.C.S.Success+'"><code>'+$(this).val()+'</code> <button class="close">&#215;</button></div>';
441                 NDAS = $('#'+SN.C.S.NoticeDataAttachSelected);
442                 if (NDAS.length > 0) {
443                     NDAS.replaceWith(S);
444                 }
445                 else {
446                     $('#'+SN.C.S.FormNotice).append(S);
447                 }
448                 $('#'+SN.C.S.NoticeDataAttachSelected+' button').click(function(){
449                     $('#'+SN.C.S.NoticeDataAttachSelected).remove();
450                     NDA.val('');
451
452                     return false;
453                 });
454             });
455         },
456
457         NoticeLocationAttach: function() {
458             var NLat = $('#'+SN.C.S.NoticeLat).val();
459             var NLon = $('#'+SN.C.S.NoticeLon).val();
460             var NLNS = $('#'+SN.C.S.NoticeLocationNs).val();
461             var NLID = $('#'+SN.C.S.NoticeLocationId).val();
462             var NLN = $('#'+SN.C.S.NoticeGeoName).text();
463             var NDGe = $('#'+SN.C.S.NoticeDataGeo);
464
465             function removeNoticeDataGeo() {
466                 $('label[for='+SN.C.S.NoticeDataGeo+']').removeClass('checked');
467                 $('#'+SN.C.S.NoticeDataGeoSelected).hide();
468
469                 $('#'+SN.C.S.NoticeLat).val('');
470                 $('#'+SN.C.S.NoticeLon).val('');
471                 $('#'+SN.C.S.NoticeLocationNs).val('');
472                 $('#'+SN.C.S.NoticeLocationId).val('');
473                 $('#'+SN.C.S.NoticeDataGeo).attr('checked', false);
474
475                 $.cookie(SN.C.S.NoticeDataGeo, 'disabled');
476             }
477
478             function getJSONgeocodeURL(geocodeURL, data) {
479                 $.getJSON(geocodeURL, data, function(location) {
480                     var lns, lid;
481
482                     if (typeof(location.location_ns) != 'undefined') {
483                         $('#'+SN.C.S.NoticeLocationNs).val(location.location_ns);
484                         lns = location.location_ns;
485                     }
486
487                     if (typeof(location.location_id) != 'undefined') {
488                         $('#'+SN.C.S.NoticeLocationId).val(location.location_id);
489                         lid = location.location_id;
490                     }
491
492                     if (typeof(location.name) == 'undefined') {
493                         NLN_text = position.coords.latitude + ';' + position.coords.longitude;
494                     }
495                     else {
496                         NLN_text = location.name;
497                     }
498
499                     $('#'+SN.C.S.NoticeGeoName)
500                         .replaceWith('<a id="notice_data-geo_name"/>');
501
502                     $('#'+SN.C.S.NoticeGeoName)
503                         .attr('href', location.url)
504                         .text(NLN_text)
505                         .click(function() {
506                             window.open(location.url);
507
508                             return false;
509                         });
510
511                     $('#'+SN.C.S.NoticeLat).val(data.lat);
512                     $('#'+SN.C.S.NoticeLon).val(data.lon);
513                     $('#'+SN.C.S.NoticeLocationNs).val(lns);
514                     $('#'+SN.C.S.NoticeLocationId).val(lid);
515                     $('#'+SN.C.S.NoticeDataGeo).attr('checked', true);
516
517                     var cookieValue = {
518                         'NLat': data.lat,
519                         'NLon': data.lon,
520                         'NLNS': lns,
521                         'NLID': lid,
522                         'NLN': NLN_text,
523                         'NLNU': location.url,
524                         'NDG': true,
525                         'NDGSM': false
526                     };
527                     $.cookie(SN.C.S.NoticeDataGeo, JSON.stringify(cookieValue));
528                 });
529             }
530
531             if (NDGe.length > 0) {
532                 if ($.cookie(SN.C.S.NoticeDataGeo) == 'disabled') {
533                     NDGe.attr('checked', false);
534                 }
535                 else {
536                     NDGe.attr('checked', true);
537                 }
538
539                 var NGW = $('#notice_data-geo_wrap');
540                 var geocodeURL = NGW.attr('title');
541                 NGW.removeAttr('title');
542
543                 $('label[for='+SN.C.S.NoticeDataGeo+']').attr('title', jQuery.trim($('label[for='+SN.C.S.NoticeDataGeo+']').text()));
544
545                 NDGe.change(function() {
546                     var NLN = $('#'+SN.C.S.NoticeGeoName);
547                     if (NLN.length > 0) {
548                         NLN.remove();
549                     }
550
551                     if ($('#'+SN.C.S.NoticeDataGeo).attr('checked') === true || $.cookie(SN.C.S.NoticeDataGeo) === null) {
552                         $('label[for='+SN.C.S.NoticeDataGeo+']').addClass('checked');
553
554                         var S = '<div id="'+SN.C.S.NoticeDataGeoSelected+'" class="'+SN.C.S.Success+'"/>';
555                         var NDGS = $('#'+SN.C.S.NoticeDataGeoSelected);
556
557                         if (NDGS.length > 0) {
558                             NDGS.replaceWith(S);
559                         }
560                         else {
561                             $('#'+SN.C.S.FormNotice).append(S);
562                         }
563
564                         NDGS = $('#'+SN.C.S.NoticeDataGeoSelected);
565                         NDGS.prepend('<span id="'+SN.C.S.NoticeGeoName+'">Geo</span> <button class="minimize">&#95;</button> <button class="close">&#215;</button>');
566
567                         var NLN = $('#'+SN.C.S.NoticeGeoName);
568                         NLN.addClass('processing');
569
570                         $('#'+SN.C.S.NoticeDataGeoSelected+' button.close').click(function(){
571                             removeNoticeDataGeo();
572
573                             $('#'+SN.C.S.NoticeDataGeoSelected).remove();
574
575                             return false;
576                         });
577
578                         $('#'+SN.C.S.NoticeDataGeoSelected+' button.minimize').click(function(){
579                             $('#'+SN.C.S.NoticeDataGeoSelected).hide();
580
581                             var cookieValue = {
582                                 'NLat': $('#'+SN.C.S.NoticeLat).val(),
583                                 'NLon': $('#'+SN.C.S.NoticeLat).val(),
584                                 'NLNS': $('#'+SN.C.S.NoticeLocationNs).val(),
585                                 'NLID': $('#'+SN.C.S.NoticeLocationId).val(),
586                                 'NLN': $('#'+SN.C.S.NoticeGeoName).text(),
587                                 'NLNU': $('#'+SN.C.S.NoticeGeoName).attr('href'),
588                                 'NDG': true,
589                                 'NDGSM': true
590                             };
591                             $.cookie(SN.C.S.NoticeDataGeo, JSON.stringify(cookieValue));
592
593                             return false;
594                         });
595
596                         if ($.cookie(SN.C.S.NoticeDataGeo) === null || $.cookie(SN.C.S.NoticeDataGeo) == 'disabled') {
597                             if (navigator.geolocation) {
598                                 navigator.geolocation.getCurrentPosition(
599                                     function(position) {
600                                         $('#'+SN.C.S.NoticeLat).val(position.coords.latitude);
601                                         $('#'+SN.C.S.NoticeLon).val(position.coords.longitude);
602
603                                         var data = {
604                                             'lat': position.coords.latitude,
605                                             'lon': position.coords.longitude,
606                                             'token': $('#token').val()
607                                         };
608
609                                         getJSONgeocodeURL(geocodeURL, data);
610                                     },
611
612                                     function(error) {
613                                         if (error.PERMISSION_DENIED == 1) {
614                                             removeNoticeDataGeo();
615                                         }
616                                     }
617                                 );
618                             }
619                             else {
620                                 if (NLat.length > 0 && NLon.length > 0) {
621                                     var data = {
622                                         'lat': NLat,
623                                         'lon': NLon,
624                                         'token': $('#token').val()
625                                     };
626
627                                     getJSONgeocodeURL(geocodeURL, data);
628                                 }
629                                 else {
630                                     removeNoticeDataGeo();
631                                     $('#'+SN.C.S.NoticeDataGeo).remove();
632                                     $('label[for='+SN.C.S.NoticeDataGeo+']').remove();
633                                 }
634                             }
635                         }
636                         else {
637                             var cookieValue = JSON.parse($.cookie(SN.C.S.NoticeDataGeo));
638
639                             if (cookieValue.NDGSM === true) {
640                                 $('#'+SN.C.S.NoticeDataGeoSelected).hide();
641                             }
642
643                             $('#'+SN.C.S.NoticeLat).val(cookieValue.NLat);
644                             $('#'+SN.C.S.NoticeLon).val(cookieValue.NLon);
645                             $('#'+SN.C.S.NoticeLocationNs).val(cookieValue.NLNS);
646                             $('#'+SN.C.S.NoticeLocationId).val(cookieValue.NLID);
647                             $('#'+SN.C.S.NoticeDataGeo).attr('checked', cookieValue.NDG);
648
649                             $('#'+SN.C.S.NoticeGeoName)
650                                 .replaceWith('<a id="notice_data-geo_name"/>');
651
652                             $('#'+SN.C.S.NoticeGeoName)
653                                 .attr('href', cookieValue.NLNU)
654                                 .text(cookieValue.NLN)
655                                 .click(function() {
656                                     window.open($(this).attr('href'));
657
658                                     return false;
659                                 });
660                         }
661                     }
662                     else {
663                         removeNoticeDataGeo();
664                     }
665                 }).change();
666             }
667         },
668
669         NewDirectMessage: function() {
670             NDM = $('.entity_send-a-message a');
671             NDM.attr({'href':NDM.attr('href')+'&ajax=1'});
672             NDM.bind('click', function() {
673                 var NDMF = $('.entity_send-a-message form');
674                 if (NDMF.length === 0) {
675                     $(this).addClass('processing');
676                     $.get(NDM.attr('href'), null, function(data) {
677                         $('.entity_send-a-message').append(document._importNode($('form', data)[0], true));
678                         NDMF = $('.entity_send-a-message .form_notice');
679                         SN.U.FormNoticeXHR(NDMF);
680                         SN.U.FormNoticeEnhancements(NDMF);
681                         NDMF.append('<button class="close">&#215;</button>');
682                         $('.entity_send-a-message button').click(function(){
683                             NDMF.hide();
684                             return false;
685                         });
686                         NDM.removeClass('processing');
687                     });
688                 }
689                 else {
690                     NDMF.show();
691                     $('.entity_send-a-message textarea').focus();
692                 }
693                 return false;
694             });
695         }
696     },
697
698     Init: {
699         NoticeForm: function() {
700             if ($('body.user_in').length > 0) {
701                 SN.U.NoticeLocationAttach();
702
703                 $('.'+SN.C.S.FormNotice).each(function() {
704                     SN.U.FormNoticeXHR($(this));
705                     SN.U.FormNoticeEnhancements($(this));
706                 });
707
708                 SN.U.NoticeDataAttach();
709             }
710         },
711
712         Notices: function() {
713             if ($('body.user_in').length > 0) {
714                 SN.U.NoticeFavor();
715                 SN.U.NoticeRepeat();
716                 SN.U.NoticeReply();
717             }
718
719             SN.U.NoticeAttachments();
720         },
721
722         EntityActions: function() {
723             if ($('body.user_in').length > 0) {
724                 $('.form_user_subscribe').each(function() { SN.U.FormXHR($(this)); });
725                 $('.form_user_unsubscribe').each(function() { SN.U.FormXHR($(this)); });
726                 $('.form_group_join').each(function() { SN.U.FormXHR($(this)); });
727                 $('.form_group_leave').each(function() { SN.U.FormXHR($(this)); });
728                 $('.form_user_nudge').each(function() { SN.U.FormXHR($(this)); });
729
730                 SN.U.NewDirectMessage();
731             }
732         }
733     }
734 };
735
736 $(document).ready(function(){
737     if ($('.'+SN.C.S.FormNotice).length > 0) {
738         SN.Init.NoticeForm();
739     }
740     if ($('#content .notices').length > 0) {
741         SN.Init.Notices();
742     }
743     if ($('#content .entity_actions').length > 0) {
744         SN.Init.EntityActions();
745     }
746 });
747