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