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