minor fixes
[wolnelektury.git] / wolnelektury / static / js / catalogue.js
1 var BANNER_TEXTS = [
2     'Przekaż 1% żeby ukryć ten baner.',
3     'Jak dobrze wydać 1% swojego podatku? <strong>Poradnik dla opornych</strong>.',
4     'Wiadomość systemowa: wystąpił błąd brak funduszy. Wykonaj procedurę 1%.',
5     '<strong>FREE!</strong> Wygraj darmowe lektury!',
6     'Confidential business offer. Not scam! 1% for you.',
7     'Biblioteka Wolne Lektury wymaga aktualizacji. Kliknij dalej.',
8     '1000 lektur. <strong>1 procent</strong>.',
9     '1% dla biblioteki lektur szkolnych. 1% dla Twojej biblioteki.',
10     '1% na lektury szkolne.',
11     '1% dla wolności lektur szkolnych.',
12     'Podaruj Jeden Procent na rzecz szkolnej biblioteki internetowej.',
13     '1% podatku dla biblioteki szkolnej Wolne Lektury.',
14     '1% na rzecz darmowego dostępu do szkolnych lektur.',
15     'Żeby czytać teksty a nie skany. Przekaż 1%.',
16     'Czytaj teksty a nie skany. Przekaż 1%',
17     'Motyw artysty w literaturze - 47 cytatów. Pomóż znaleźć następne.',
18     'Twój 1% uwolni więcej lektur.',
19     'Ponad 400 motywów, blisko 10 000 000 cytatów. Pomóż znaleźć następne. Przekaż swój 1%.',
20     'Twój 1% uwolni lektury.',
21     'Rozlicz swój PIT z Wolnymi Lekturami. Skorzystaj z darmowego programu do rozliczania podatków.',
22     'Lektury 2010: Pan Tadeusz, Trylogia.',
23     'Pan Tadeusz też chce być w Internecie! Przekaż 1% swojego podatku.',
24     'Pomóż uwolnić 286 utworów z listy lektur szkolnych. Przekaż swój 1% na Wolne Lektury.'
25 ]
26
27
28 function changeBannerText() {
29     var index = Math.floor(Math.random() * BANNER_TEXTS.length);
30     if (BANNER_TEXTS[index] == $('#onepercent-text').html()) {
31         // try again
32         changeBannerText();
33     } else {
34         $('#onepercent-text').fadeOut('slow', function() {
35             $(this).html(BANNER_TEXTS[index]);
36             $(this).fadeIn('slow');
37         });
38         
39         setTimeout(changeBannerText, 30 * 1000);
40     }
41 }
42
43 function autocomplete_result_handler(event, item) {
44     $(event.target).closest('form').submit();
45 }
46 function serverTime() { 
47     var time = null; 
48     $.ajax({url: '/katalog/zegar/', 
49         async: false, dataType: 'text', 
50         success: function(text) { 
51             time = new Date(text);
52         }, error: function(http, message, exc) {
53             time = new Date(); 
54     }}); 
55     return time; 
56 }
57
58 (function($) {
59     $(function() {
60         $('form input').labelify({labelledClass: 'blur'});
61         
62         target = $('#login-register-window div.target');
63         
64         $('#show-registration-form').click(function() {
65             $('#login-form').hide();
66             $('#registration-form').show();
67         });
68         
69         $('#show-login-form').click(function() {
70             $('#registration-form').hide();
71             $('#login-form').show();
72         });
73         
74         // Fragments
75         $('.fragment-text').each(function() {
76             if ($(this).prev().filter('.fragment-short-text').length) {
77                 $(this).hover(
78                     function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
79                     function() { $(this).css({background: '#FFF'}); }
80                 ).click(function() {
81                     $(this).fadeOut(function() {
82                         $(this).prev().fadeIn();
83                     });
84                     return false;
85                 })
86             }
87         });
88         
89         $('.fragment-short-text').click(function() {
90             $(this).fadeOut(function() { $(this).next().fadeIn() });
91             return false;
92         }).hover(
93             function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
94             function() { $(this).css({background: '#FFF'}); }
95         );
96         
97         $('.show-all-tags').click(function() {
98             $(this).parent().parent().fadeOut(function() { 
99                 $(this).next().fadeIn();
100             });
101             return false;
102         });
103         
104         $('.hide-all-tags').click(function() {
105            $(this).parent().parent().fadeOut(function() {
106                $(this).prev().fadeIn();
107            });
108            return false; 
109         });
110         
111         $('#registration-form').ajaxForm({
112             dataType: 'json',
113             beforeSubmit: function() {
114                 $('#registration-form input[type=submit]')
115                     .attr('disabled', 'disabled')
116                     .after('<img src="/static/img/indicator.gif" style="margin-left: 0.5em"/>');
117             },
118             success: function(response) {
119                 if (response.success) {
120                     location.reload(true);
121                 } else {
122                     $('#registration-form span.error').remove();
123                     $.each(response.errors, function(id, errors) {
124                         $('#id_registration-' + id).before('<span class="error">' + errors[0] + '</span>');
125                     });
126                     $('#registration-form input[type=submit]').removeAttr('disabled');
127                     $('#registration-form img').remove();
128                 }
129             }
130         });
131         
132         $('#login-form').ajaxForm({
133             dataType: 'json',
134             beforeSubmit: function() {
135                 $('#login-form input[type=submit]')
136                     .attr('disabled', 'disabled')
137                     .after('<img src="/static/img/indicator.gif" style="margin-left: 0.5em"/>');
138             },
139             success: function(response) {
140                 if (response.success) {
141                     location.reload(true);
142                 } else {
143                     $('#login-form span.error').remove();
144                     $.each(response.errors, function(id, errors) {
145                         $('#id_login-' + id).before('<span class="error">' + errors[0] + '</span>');
146                     });
147                     $('#login-form input[type=submit]').removeAttr('disabled');
148                     $('#login-form img').remove();
149                 }
150             }
151         });
152         
153         $('#login-register-window').jqm({
154             target: target[0],
155             overlay: 60,
156             trigger: '.login-register-link',
157             onShow: function(hash) {
158                 var offset = $(hash.t).offset();
159                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
160                 $('div.header', hash.w).css({width: $(hash.t).width()});
161                 hash.w.show();
162             }
163         });
164         
165         $('ul.shelf-list li').hover(function() {
166             $(this).css({background: '#EEE', cursor: 'pointer'});
167         }, function() {
168             $(this).css({background: 'transparent'});
169         }).click(function() {
170             location.href = $('a.visit-shelf', this).attr('href');
171         });
172         
173         $('.delete-shelf').click(function() { 
174             var link = $(this);
175             var shelf_name = $('.visit-shelf', link.parent()).text();
176             if (confirm('Czy na pewno usunąć półkę ' + shelf_name + '?')) {
177                 $.post(link.attr('href'), function(data, textStatus) {
178                     link.parent().remove();
179                 });
180             }
181             return false;
182         });
183         
184         $('#user-shelves-window').jqm({
185             ajax: '@href',
186             target: $('#user-shelves-window div.target')[0],
187             overlay: 60,
188             trigger: '#user-shelves-link',
189             onShow: function(hash) {
190                 var offset = $(hash.t).offset();
191                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
192                 $('div.header', hash.w).css({width: $(hash.t).width()});
193                 hash.w.show();
194             },
195             onLoad: function(hash) { 
196                 $('form', hash.w).ajaxForm({
197                     target: $('#user-shelves-window div.target'),
198                     success: function() { setTimeout(function() { $('#user-shelves-window').jqmHide() }, 1000) }
199                 });
200                 
201                 $('input', hash.w).labelify({labelledClass: 'blur'});
202                 
203                 $('ul.shelf-list li', hash.w).hover(function() {
204                     $(this).css({background: '#EEE', cursor: 'pointer'});
205                 }, function() {
206                     $(this).css({background: 'transparent'});
207                 }).click(function() {
208                     location.href = $('a.visit-shelf', this).attr('href');
209                 });
210                 
211                 $('.delete-shelf').click(function() {
212                     var link = $(this);
213                     var shelf_name = $('.visit-shelf', link.parent()).text();
214                     if (confirm('Czy na pewno usunąć półkę ' + shelf_name + '?')) {
215                         $.post(link.attr('href'), function(data, textStatus) {
216                             link.parent().remove();
217                         });
218                     }
219                     return false;
220                 });
221             }
222         });
223     
224         $('#books-list .book').hover(
225             function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
226             function() { $(this).css({background: '#FFF'}); }
227         ).click(function() {
228             location.href = $('h2 a', this).attr('href');
229         });
230     
231         $('#toggle-description').hover(
232             function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
233             function() { $(this).css({background: '#EEE'}); }
234         ).click(function() {
235             if ($('#description').hasClass('hidden')) {
236                 $('#description').slideDown('fast').removeClass('hidden');
237                 $.cookie('description-state', 'opened', {path: '/', expires: 30});
238                 $('p', this).html('Zwiń opis ▲');
239             } else {
240                 $('#description').slideUp('fast').addClass('hidden');
241                 $.cookie('description-state', 'closed', {path: '/', expires: 30});
242                 $('p', this).html('Rozwiń opis ▼');
243             }
244         });
245     
246         var target = $('#set-window div.target');
247     
248         $('#set-window').jqm({
249             ajax: '@href', 
250             target: target[0],
251             overlay: 60,
252             trigger: 'a.jqm-trigger', 
253             onShow: function(hash) { 
254                 var offset = $(hash.t).offset();
255                 target.html('<p><img src="/static/img/indicator.gif" /> Ładowanie</p>');
256                 hash.w.css({position: 'absolute', left: offset.left, top: offset.top}).show() },
257             onLoad: function(hash) { 
258                 $('form', hash.w).ajaxForm({
259                     target: target,
260                     success: function() { setTimeout(function() { $('#set-window').jqmHide() }, 1000) }
261                 });
262             }
263         });
264         
265         $('a.remove-from-shelf').click(function(event) {
266             event.preventDefault();
267             link = $(this);
268             $.post(link.attr('href'), function(data, textStatus) {
269                 link.parent().remove();
270             });
271         });
272         
273         if ($.cookie('description-state') == 'closed') {
274             $('#description').hide().addClass('hidden');
275             $('#toggle-description p').html('Rozwiń opis ▼');
276         }
277                 
278         $('#user-info').show();
279         changeBannerText();
280         $('#onepercent-banner').show();
281         
282         var formatsDownloaded = false;
283         $('#download-shelf').click(function() {
284             $('#download-shelf-menu').slideDown('fast');
285             
286             if (!formatsDownloaded) {
287                 // Pobierz dane o formatach
288                 formatsDownloaded = true;
289                 $.ajax({
290                     url: $('#download-formats-form').attr('data-formats-feed'),
291                     type: 'GET',
292                     dataType: 'json',
293                     complete: function() {
294                         $('#download-formats-form-submit').attr('disabled', null);
295                         $('#download-formats-form-submit-li img').remove();
296                         $('#updating-formats').fadeOut('fast', function() {
297                             $('#formats-updated').fadeIn('fast');
298                         });
299                     },
300                     success: function(data) {
301                         $('#download-formats-form li').each(function() {
302                             var item = $(this);
303                             if (!!item.attr('data-format') && !data[item.attr('data-format')]) {
304                                 item.fadeOut('fast', function() {
305                                     item.remove();
306                                 });
307                             }
308                         });
309                     }
310                 });
311             }
312             return false;
313         });
314         
315         $('#download-formats-form-cancel').click(function() {
316             $('#download-shelf-menu').slideUp('fast');
317             return false;
318         });
319     });
320 })(jQuery)