dc4c07deea28033ccd8af78b6d421d9b8d0fc7d4
[wolnelektury.git] / wolnelektury / static / js / catalogue.js
1 var LOCALE_TEXTS = {
2         "pl": {
3                 "DELETE_SHELF": "Czy na pewno usunąć półkę",
4                 "HIDE_DESCRIPTION": "Zwiń opis",
5                 "EXPAND_DESCRIPTION": "Rozwiń opis",
6                 "LOADING": "Ładowanie"         
7         },
8     "de": {
9         "DELETE_SHELF": "Translate me!",
10         "HIDE_DESCRIPTION": "Translate me!",
11         "EXPAND_DESCRIPTION": "Translate me!",
12         "LOADING": "Translate me!"             
13     },
14         "fr": {
15                 "DELETE_SHELF": "Translate me!",
16                 "HIDE_DESCRIPTION": "Translate me!",
17                 "EXPAND_DESCRIPTION": "Translate me!",
18                 "LOADING": "Translate me!"                              
19         },
20         "en": {
21                 "DELETE_SHELF": "Translate me!",
22                 "HIDE_DESCRIPTION": "Translate me!",
23                 "EXPAND_DESCRIPTION": "Translate me!",
24                 "LOADING": "Translate me!"              
25         }, 
26         "ru": {
27                 "DELETE_SHELF": "Translate me!",
28                 "HIDE_DESCRIPTION": "Translate me!",
29                 "EXPAND_DESCRIPTION": "Translate me!",
30                 "LOADING": "Translate me!"              
31         },
32         "es": {
33                 "DELETE_SHELF": "Translate me!",
34                 "HIDE_DESCRIPTION": "Translate me!",
35                 "EXPAND_DESCRIPTION": "Translate me!",
36                 "LOADING": "Translate me!"                              
37         },
38     "lt":{
39         "DELETE_SHELF": "Translate me!",
40         "HIDE_DESCRIPTION": "Translate me!",
41         "EXPAND_DESCRIPTION": "Translate me!",
42         "LOADING": "Translate me!"             
43     },
44     "uk":{
45         "DELETE_SHELF": "Translate me!",
46         "HIDE_DESCRIPTION": "Translate me!",
47         "EXPAND_DESCRIPTION": "Translate me!",
48         "LOADING": "Translate me!"             
49     }
50 }
51 var BANNER_TEXTS = [
52     'Przekaż 1% żeby ukryć ten baner.',
53     'Jak dobrze wydać 1% swojego podatku? <strong>Poradnik dla opornych</strong>.',
54     'Wiadomość systemowa: wystąpił błąd brak funduszy. Wykonaj procedurę 1%.',
55     '<strong>FREE!</strong> Wygraj darmowe lektury!',
56     'Confidential business offer. Not scam! 1% for you.',
57     'Biblioteka Wolne Lektury wymaga aktualizacji. Kliknij dalej.',
58     '1000 lektur. <strong>1 procent</strong>.',
59     '1% dla biblioteki lektur szkolnych. 1% dla Twojej biblioteki.',
60     '1% na lektury szkolne.',
61     '1% dla wolności lektur szkolnych.',
62     'Podaruj Jeden Procent na rzecz szkolnej biblioteki internetowej.',
63     '1% podatku dla biblioteki szkolnej Wolne Lektury.',
64     '1% na rzecz darmowego dostępu do szkolnych lektur.',
65     'Żeby czytać teksty a nie skany. Przekaż 1%.',
66     'Czytaj teksty a nie skany. Przekaż 1%',
67     'Motyw artysty w literaturze - 47 cytatów. Pomóż znaleźć następne.',
68     'Twój 1% uwolni więcej lektur.',
69     'Ponad 400 motywów, blisko 10 000 000 cytatów. Pomóż znaleźć następne. Przekaż swój 1%.',
70     'Twój 1% uwolni lektury.',
71     'Rozlicz swój PIT z Wolnymi Lekturami. Skorzystaj z darmowego programu do rozliczania podatków.',
72     'Lektury 2010: Pan Tadeusz, Trylogia.',
73     'Pan Tadeusz też chce być w Internecie! Przekaż 1% swojego podatku.',
74     'Pomóż uwolnić 286 utworów z listy lektur szkolnych. Przekaż swój 1% na Wolne Lektury.'
75 ]
76
77
78 function changeBannerText() {
79     var index = Math.floor(Math.random() * BANNER_TEXTS.length);
80     if (BANNER_TEXTS[index] == $('#onepercent-text').html()) {
81         // try again
82         changeBannerText();
83     } else {
84         $('#onepercent-text').fadeOut('slow', function() {
85             $(this).html(BANNER_TEXTS[index]);
86             $(this).fadeIn('slow');
87         });
88         
89         setTimeout(changeBannerText, 30 * 1000);
90     }
91 }
92
93 function autocomplete_result_handler(event, item) {
94     $(event.target).closest('form').submit();
95 }
96 function serverTime() { 
97     var time = null; 
98     $.ajax({url: '/katalog/zegar/', 
99         async: false, dataType: 'text', 
100         success: function(text) { 
101             time = new Date(text);
102         }, error: function(http, message, exc) {
103             time = new Date(); 
104     }}); 
105     return time; 
106 }
107
108 (function($) {
109     $(function() {
110         
111         $('form input').labelify({labelledClass: 'blur'});
112         
113         target = $('#login-register-window div.target');
114         
115         $('#show-registration-form').click(function() {
116             $('#login-form').hide();
117             $('#registration-form').show();
118         });
119         
120         $('#show-login-form').click(function() {
121             $('#registration-form').hide();
122             $('#login-form').show();
123         });
124         
125         // Fragments
126         $('.fragment-text').each(function() {
127             if ($(this).prev().filter('.fragment-short-text').length) {
128                 $(this).hover(
129                     function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
130                     function() { $(this).css({background: '#FFF'}); }
131                 ).click(function() {
132                     $(this).fadeOut(function() {
133                         $(this).prev().fadeIn();
134                     });
135                     return false;
136                 })
137             }
138         });
139         
140         $('.fragment-short-text').click(function() {
141             $(this).fadeOut(function() { $(this).next().fadeIn() });
142             return false;
143         }).hover(
144             function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
145             function() { $(this).css({background: '#FFF'}); }
146         );
147         
148         $('.show-all-tags').click(function() {
149             $(this).parent().parent().fadeOut(function() { 
150                 $(this).next().fadeIn();
151             });
152             return false;
153         });
154         
155         $('.hide-all-tags').click(function() {
156            $(this).parent().parent().fadeOut(function() {
157                $(this).prev().fadeIn();
158            });
159            return false; 
160         });
161         
162         $('#registration-form').ajaxForm({
163             dataType: 'json',
164             beforeSubmit: function() {
165                 $('#registration-form input[type=submit]')
166                     .attr('disabled', 'disabled')
167                     .after('<img src="/static/img/indicator.gif" style="margin-left: 0.5em"/>');
168             },
169             success: function(response) {
170                 if (response.success) {
171                     location.reload(true);
172                 } else {
173                     $('#registration-form span.error').remove();
174                     $.each(response.errors, function(id, errors) {
175                         $('#id_registration-' + id).before('<span class="error">' + errors[0] + '</span>');
176                     });
177                     $('#registration-form input[type=submit]').removeAttr('disabled');
178                     $('#registration-form img').remove();
179                 }
180             }
181         });
182         
183         $('#login-form').ajaxForm({
184             dataType: 'json',
185             beforeSubmit: function() {
186                 $('#login-form input[type=submit]')
187                     .attr('disabled', 'disabled')
188                     .after('<img src="/static/img/indicator.gif" style="margin-left: 0.5em"/>');
189             },
190             success: function(response) {
191                 if (response.success) {
192                     location.reload(true);
193                 } else {
194                     $('#login-form span.error').remove();
195                     $.each(response.errors, function(id, errors) {
196                         $('#id_login-' + id).before('<span class="error">' + errors[0] + '</span>');
197                     });
198                     $('#login-form input[type=submit]').removeAttr('disabled');
199                     $('#login-form img').remove();
200                 }
201             }
202         });
203         
204         $('#login-register-window').jqm({
205             target: target[0],
206             overlay: 60,
207             trigger: '.login-register-link',
208             onShow: function(hash) {
209                 var offset = $(hash.t).offset();
210                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
211                 $('div.header', hash.w).css({width: $(hash.t).width()});
212                 hash.w.show();
213             }
214         });
215         
216         $('ul.shelf-list li').hover(function() {
217             $(this).css({background: '#EEE', cursor: 'pointer'});
218         }, function() {
219             $(this).css({background: 'transparent'});
220         }).click(function() {
221             location.href = $('a.visit-shelf', this).attr('href');
222         });
223         
224         $('.delete-shelf').click(function() { 
225             var link = $(this);
226             var shelf_name = $('.visit-shelf', link.parent()).text();
227             if (confirm(LOCALE_TEXTS[LANGUAGE_CODE]['DELETE_SHELF']+ ' '+ shelf_name + '?')) {
228                 $.post(link.attr('href'), function(data, textStatus) {
229                     link.parent().remove();
230                 });
231             }
232             return false;
233         });
234         
235         $('#user-shelves-window').jqm({
236             ajax: '@href',
237             target: $('#user-shelves-window div.target')[0],
238             overlay: 60,
239             trigger: '#user-shelves-link',
240             onShow: function(hash) {
241                 var offset = $(hash.t).offset();
242                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
243                 $('div.header', hash.w).css({width: $(hash.t).width()});
244                 hash.w.show();
245             },
246             onLoad: function(hash) { 
247                 $('form', hash.w).ajaxForm({
248                     target: $('#user-shelves-window div.target'),
249                     success: function() { setTimeout(function() { $('#user-shelves-window').jqmHide() }, 1000) }
250                 });
251                 
252                 $('input', hash.w).labelify({labelledClass: 'blur'});
253                 
254                 $('ul.shelf-list li', hash.w).hover(function() {
255                     $(this).css({background: '#EEE', cursor: 'pointer'});
256                 }, function() {
257                     $(this).css({background: 'transparent'});
258                 }).click(function() {
259                     location.href = $('a.visit-shelf', this).attr('href');
260                 });
261                 
262                 $('.delete-shelf').click(function() {
263                     var link = $(this);
264                     var shelf_name = $('.visit-shelf', link.parent()).text();
265                     if (confirm(LOCALE_TEXTS[LANGUAGE_CODE]['DELETE_SHELF'] + ' ' + shelf_name + '?')) {
266                         $.post(link.attr('href'), function(data, textStatus) {
267                             link.parent().remove();
268                         });
269                     }
270                     return false;
271                 });
272             }
273         });
274
275         $('#suggest-window').jqm({
276             ajax: '@href',
277             target: $('#suggest-window div.target')[0],
278             overlay: 60,
279             trigger: '#suggest-link',
280             onShow: function(hash) {
281                 var offset = $(hash.t).offset();
282                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
283                 $('div.header', hash.w).css({width: $(hash.t).width()});
284                 hash.w.show();
285             },
286             onLoad: function(hash) { 
287                 $('form', hash.w).ajaxForm({
288                                         dataType: 'json',
289                     target: $('#suggest-window div.target'),
290                     success: function(response) {
291                                                 if (response.success) {
292                                                         $('#suggest-window div.target').text(response.message);
293                             setTimeout(function() { $('#suggest-window').jqmHide() }, 1000)
294                                                 }
295                                                 else {
296                                                         $('#suggest-form .error').remove();
297                                 $.each(response.errors, function(id, errors) {
298                                     $('#suggest-form #id_' + id).before('<span class="error">' + errors[0] + '</span>');
299                                 });
300                                 $('#suggest-form input[type=submit]').removeAttr('disabled');
301                                                         return false;
302                                                 }
303                             }
304                 });
305             }
306         });
307     
308         $('#books-list .book').hover(
309             function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
310             function() { $(this).css({background: '#FFF'}); }
311         ).click(function() {
312             location.href = $('h2 a', this).attr('href');
313         });
314         
315                 function toggled_by_slide(cont, short_el, long_el, button, short_text, long_text) {
316                         function toggle(cont, short_el, long_el, button, short_text, long_text) {
317                     if (cont.hasClass('short')) {
318                         cont.animate({"height": long_el.attr("cont_h")+'px'}, {duration: "fast" }).removeClass('short');
319                         short_el.hide();
320                         long_el.show();
321                         button.html(long_text);
322                     } else {
323                         cont.animate({"height": short_el.attr("cont_h")+'px'}, {duration: "fast" }).addClass('short');
324                         long_el.hide();
325                         short_el.show();
326                         button.html(short_text);
327                     }
328                         }
329             long_el.attr("cont_h", cont.height()).hide();
330             short_el.show().attr("cont_h", cont.height());
331                         cont.addClass('short');
332                         button.html(short_text);
333                         button.hover(
334                 function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
335                 function() { $(this).css({background: '#EEE'}); }
336                         ).click(function(){
337                                 toggle(cont, short_el, long_el, button, short_text, long_text)
338                         });
339                         cont.hover(
340                 function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
341                 function() { $(this).css({background: '#FFF'}); }
342             ).click(function(){
343                 toggle(cont, short_el, long_el, button, short_text, long_text)
344             })
345                 }
346         toggled_by_slide($('#description'), $('#description-short'), $('#description-long'),  
347           $('#toggle-description p'), 
348           LOCALE_TEXTS[LANGUAGE_CODE]['EXPAND_DESCRIPTION']+' ▼',
349                   LOCALE_TEXTS[LANGUAGE_CODE]['HIDE_DESCRIPTION'] + ' ▲'
350                   );
351
352         $('#toggle-share-shelf').hover(
353             function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
354             function() { $(this).css({background: '#EEE'}); }
355         ).click(function() {
356             if ($('#share-shelf').hasClass('hidden')) {
357                 $('#share-shelf').slideDown('fast').removeClass('hidden');
358             } else {
359                 $('#share-shelf').slideUp('fast').addClass('hidden');
360             }
361         });
362
363         var target = $('#set-window div.target');
364     
365         $('#set-window').jqm({
366             ajax: '@href', 
367             target: target[0],
368             overlay: 60,
369             trigger: 'a.jqm-trigger', 
370             onShow: function(hash) { 
371                 var offset = $(hash.t).offset();
372                 target.html('<p><img src="/static/img/indicator.gif" />'+LOCALE_TEXTS[LANGUAGE_CODE]['DELETE_SHELF']+'</p>');
373                 hash.w.css({position: 'absolute', left: offset.left, top: offset.top}).show() },
374             onLoad: function(hash) { 
375                 try {
376                         $('#createShelfTrigger').click(function(){
377                                 $('#createNewShelf').show();
378                         });
379                 } catch (e){}
380
381                 $('form', hash.w).ajaxForm({
382                     target: target,
383                     success: function() { 
384                         setTimeout(function() { 
385                                         $('#set-window').jqmHide();
386                                    }, 1000)}
387                 });
388             }
389         });
390         
391         $('a.remove-from-shelf').click(function(event) {
392             event.preventDefault();
393             link = $(this);
394             $.post(link.attr('href'), function(data, textStatus) {
395                 link.parent().remove();
396             });
397         });
398                 
399         $('#share-shelf').hide().addClass('hidden');
400                 $('#share-shelf input').focus(function(){this.select();});
401                 
402         $('#user-info').show();
403         changeBannerText();
404         $('#onepercent-banner').show();
405         
406         var formatsDownloaded = false;
407         $('#download-shelf').click(function() {
408             $('#download-shelf-menu').slideDown('fast');
409             
410             if (!formatsDownloaded) {
411                 // Get info about the formats
412                 formatsDownloaded = true;
413                 $.ajax({
414                     url: $('#download-formats-form').attr('data-formats-feed'),
415                     type: 'GET',
416                     dataType: 'json',
417                     complete: function() {
418                         $('#download-formats-form-submit').attr('disabled', null);
419                         $('#download-formats-form-submit-li img').remove();
420                         $('#updating-formats').fadeOut('fast', function() {
421                             $('#formats-updated').fadeIn('fast');
422                         });
423                     },
424                     success: function(data) {
425                         $('#download-formats-form li').each(function() {
426                             var item = $(this);
427                             if (!!item.attr('data-format') && !data[item.attr('data-format')]) {
428                                 item.fadeOut('fast', function() {
429                                     item.remove();
430                                 });
431                             }
432                         });
433                     }
434                 });
435             }
436             return false;
437         });
438         
439         $('#download-formats-form-cancel').click(function() {
440             $('#download-shelf-menu').slideUp('fast');
441             return false;
442         });
443     });
444 })(jQuery)