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