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