Merge branch 'master' of http://github.com/fnp/wolnelektury
[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                 })
85             }
86         });
87         
88         $('.fragment-short-text').click(function() {
89             $(this).fadeOut(function() { $(this).next().fadeIn() });
90         }).hover(
91             function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
92             function() { $(this).css({background: '#FFF'}); }
93         );
94         
95         $('.show-all-tags').click(function() {
96             $(this).parent().parent().fadeOut(function() { 
97                 $(this).next().fadeIn();
98             });
99             return false;
100         });
101         
102         $('.hide-all-tags').click(function() {
103            $(this).parent().parent().fadeOut(function() {
104                $(this).prev().fadeIn();
105            });
106            return false; 
107         });
108         
109         $('#registration-form').ajaxForm({
110             dataType: 'json',
111             beforeSubmit: function() {
112                 $('#registration-form input[type=submit]')
113                     .attr('disabled', 'disabled')
114                     .after('<img src="/static/img/indicator.gif" style="margin-left: 0.5em"/>');
115             },
116             success: function(response) {
117                 if (response.success) {
118                     location.reload(true);
119                 } else {
120                     $('#registration-form span.error').remove();
121                     $.each(response.errors, function(id, errors) {
122                         $('#id_registration-' + id).before('<span class="error">' + errors[0] + '</span>');
123                     });
124                     $('#registration-form input[type=submit]').removeAttr('disabled');
125                     $('#registration-form img').remove();
126                 }
127             }
128         });
129         
130         $('#login-form').ajaxForm({
131             dataType: 'json',
132             beforeSubmit: function() {
133                 $('#login-form input[type=submit]')
134                     .attr('disabled', 'disabled')
135                     .after('<img src="/static/img/indicator.gif" style="margin-left: 0.5em"/>');
136             },
137             success: function(response) {
138                 if (response.success) {
139                     location.reload(true);
140                 } else {
141                     $('#login-form span.error').remove();
142                     $.each(response.errors, function(id, errors) {
143                         $('#id_login-' + id).before('<span class="error">' + errors[0] + '</span>');
144                     });
145                     $('#login-form input[type=submit]').removeAttr('disabled');
146                     $('#login-form img').remove();
147                 }
148             }
149         });
150         
151         $('#login-register-window').jqm({
152             target: target[0],
153             overlay: 60,
154             trigger: '.login-register-link',
155             onShow: function(hash) {
156                 var offset = $(hash.t).offset();
157                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
158                 $('div.header', hash.w).css({width: $(hash.t).width()});
159                 hash.w.show();
160             }
161         });
162         
163         $('ul.shelf-list li').hover(function() {
164             $(this).css({background: '#EEE', cursor: 'pointer'});
165         }, function() {
166             $(this).css({background: 'transparent'});
167         }).click(function() {
168             location.href = $('a.visit-shelf', this).attr('href');
169         });
170         
171         $('.delete-shelf').click(function() { 
172             var link = $(this);
173             var shelf_name = $('.visit-shelf', link.parent()).text();
174             if (confirm('Czy na pewno usunąć półkę ' + shelf_name + '?')) {
175                 $.post(link.attr('href'), function(data, textStatus) {
176                     link.parent().remove();
177                 });
178             }
179             return false;
180         });
181         
182         $('#user-shelves-window').jqm({
183             ajax: '@href',
184             target: $('#user-shelves-window div.target')[0],
185             overlay: 60,
186             trigger: '#user-shelves-link',
187             onShow: function(hash) {
188                 var offset = $(hash.t).offset();
189                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
190                 $('div.header', hash.w).css({width: $(hash.t).width()});
191                 hash.w.show();
192             },
193             onLoad: function(hash) { 
194                 $('form', hash.w).ajaxForm({
195                     target: $('#user-shelves-window div.target'),
196                     success: function() { setTimeout(function() { $('#user-shelves-window').jqmHide() }, 1000) }
197                 });
198                 
199                 $('input', hash.w).labelify({labelledClass: 'blur'});
200                 
201                 $('ul.shelf-list li', hash.w).hover(function() {
202                     $(this).css({background: '#EEE', cursor: 'pointer'});
203                 }, function() {
204                     $(this).css({background: 'transparent'});
205                 }).click(function() {
206                     location.href = $('a.visit-shelf', this).attr('href');
207                 });
208                 
209                 $('.delete-shelf').click(function() {
210                     var link = $(this);
211                     var shelf_name = $('.visit-shelf', link.parent()).text();
212                     if (confirm('Czy na pewno usunąć półkę ' + shelf_name + '?')) {
213                         $.post(link.attr('href'), function(data, textStatus) {
214                             link.parent().remove();
215                         });
216                     }
217                     return false;
218                 });
219             }
220         });
221     
222         $('#books-list .book').hover(
223             function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
224             function() { $(this).css({background: '#FFF'}); }
225         ).click(function() {
226             location.href = $('h2 a', this).attr('href');
227         });
228     
229         $('#toggle-description').hover(
230             function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
231             function() { $(this).css({background: '#EEE'}); }
232         ).click(function() {
233             if ($('#description').hasClass('hidden')) {
234                 $('#description').slideDown('fast').removeClass('hidden');
235                 $.cookie('description-state', 'opened', {path: '/', expires: 30});
236                 $('p', this).html('Zwiń opis ▲');
237             } else {
238                 $('#description').slideUp('fast').addClass('hidden');
239                 $.cookie('description-state', 'closed', {path: '/', expires: 30});
240                 $('p', this).html('Rozwiń opis ▼');
241             }
242         });
243     
244         var target = $('#set-window div.target');
245     
246         $('#set-window').jqm({
247             ajax: '@href', 
248             target: target[0],
249             overlay: 60,
250             trigger: 'a.jqm-trigger', 
251             onShow: function(hash) { 
252                 var offset = $(hash.t).offset();
253                 target.html('<p><img src="/static/img/indicator.gif" /> Ładowanie</p>');
254                 hash.w.css({position: 'absolute', left: offset.left, top: offset.top}).show() },
255             onLoad: function(hash) { 
256                 $('form', hash.w).ajaxForm({
257                     target: target,
258                     success: function() { setTimeout(function() { $('#set-window').jqmHide() }, 1000) }
259                 });
260             }
261         });
262         
263         $('a.remove-from-shelf').click(function(event) {
264             event.preventDefault();
265             link = $(this);
266             $.post(link.attr('href'), function(data, textStatus) {
267                 link.parent().remove();
268             });
269         });
270         
271         if ($.cookie('description-state') == 'closed') {
272             $('#description').hide().addClass('hidden');
273             $('#toggle-description p').html('Rozwiń opis ▼');
274         }
275                 
276         $('#user-info').show();
277         changeBannerText();
278         $('#onepercent-banner').show();
279         
280         var formatsDownloaded = false;
281         $('#download-shelf').click(function() {
282             $('#download-shelf-menu').slideDown('fast');
283             
284             if (!formatsDownloaded) {
285                 // Pobierz dane o formatach
286                 formatsDownloaded = true;
287                 $.ajax({
288                     url: $('#download-formats-form').attr('data-formats-feed'),
289                     type: 'GET',
290                     dataType: 'json',
291                     complete: function() {
292                         $('#download-formats-form-submit').attr('disabled', null);
293                         $('#download-formats-form-submit-li img').remove();
294                         $('#updating-formats').fadeOut('fast', function() {
295                             $('#formats-updated').fadeIn('fast');
296                         });
297                     },
298                     success: function(data) {
299                         $('#download-formats-form li').each(function() {
300                             var item = $(this);
301                             if (!!item.attr('data-format') && !data[item.attr('data-format')]) {
302                                 item.fadeOut('fast', function() {
303                                     item.remove();
304                                 });
305                             }
306                         });
307                     }
308                 });
309             }
310             return false;
311         });
312         
313         $('#download-formats-form-cancel').click(function() {
314             $('#download-shelf-menu').slideUp('fast');
315             return false;
316         });
317     });
318 })(jQuery)