zabawnik on main
[wolnelektury.git] / wolnelektury / static / js / catalogue.js
1 var STATIC = '/static/';
2 var LOCALE_TEXTS = {
3     "pl": {
4         "DELETE_SHELF": "Czy na pewno usunąć półkę",
5         "HIDE_DESCRIPTION": "Zwiń opis",
6         "EXPAND_DESCRIPTION": "Rozwiń opis",
7         "LOADING": "Ładowanie"
8     },
9     "de": {
10         "DELETE_SHELF": "Möchtest du wirklich dieses Bücherregal entfernen?",
11         "HIDE_DESCRIPTION": "Beschreibung zuklappen",
12         "EXPAND_DESCRIPTION": "Beschreibung aufklappen",
13         "LOADING": "Herunterladen"
14     },
15     "fr": {
16         "DELETE_SHELF": "Voulez-vous supprimer l'étagère  définitivement?",
17         "HIDE_DESCRIPTION": "Montrer la description",
18         "EXPAND_DESCRIPTION": "Cacher la description",
19         "LOADING": "Chargement"
20     },
21     "en": {
22         "DELETE_SHELF": "Are you sure you want to delete this shelf?",
23         "HIDE_DESCRIPTION": "Hide",
24         "EXPAND_DESCRIPTION": "Expand",
25         "LOADING": "Loading"
26     },
27     "ru": {
28         "DELETE_SHELF": "Уверены ли вы том, чтобы удалить полку?",
29         "HIDE_DESCRIPTION": "Свернуть описание",
30         "EXPAND_DESCRIPTION": "Раскрыть описание",
31         "LOADING": "Загрузка"
32     },
33     "es": {
34         "DELETE_SHELF": "¿Estás seguro que quieres borrar este estante?",
35         "HIDE_DESCRIPTION": "Esconder la descripción",
36         "EXPAND_DESCRIPTION": "Ampliar la descripción",
37         "LOADING": "Cargando"
38     },
39     "lt":{
40         "DELETE_SHELF": "Ar tikrai nori pašalinti lentną?",
41         "HIDE_DESCRIPTION": "Suvyniok aprašymą ",
42         "EXPAND_DESCRIPTION": "Išplėsk aprašymą",
43         "LOADING": "Krovimas"
44     },
45     "uk":{
46         "DELETE_SHELF": "Ви впевнені, що хочете видалити полицю?",
47         "HIDE_DESCRIPTION": "Сховати опис",
48         "EXPAND_DESCRIPTION": "Показати опис",
49         "LOADING": "Завантажується"
50     }
51 }
52 var BANNER_TEXTS = [
53     'Przekaż 1% żeby ukryć ten baner.',
54     'Jak dobrze wydać 1% swojego podatku? <strong>Poradnik dla opornych</strong>.',
55     'Wiadomość systemowa: wystąpił błąd brak funduszy. Wykonaj procedurę 1%.',
56     '<strong>FREE!</strong> Wygraj darmowe lektury!',
57     'Confidential business offer. Not scam! 1% for you.',
58     'Biblioteka Wolne Lektury wymaga aktualizacji. Kliknij dalej.',
59     '1000 lektur. <strong>1 procent</strong>.',
60     '1% dla biblioteki lektur szkolnych. 1% dla Twojej biblioteki.',
61     '1% na lektury szkolne.',
62     '1% dla wolności lektur szkolnych.',
63     'Podaruj Jeden Procent na rzecz szkolnej biblioteki internetowej.',
64     '1% podatku dla biblioteki szkolnej Wolne Lektury.',
65     '1% na rzecz darmowego dostępu do szkolnych lektur.',
66     'Żeby czytać teksty a nie skany. Przekaż 1%.',
67     'Czytaj teksty a nie skany. Przekaż 1%',
68     'Motyw artysty w literaturze - 47 cytatów. Pomóż znaleźć następne.',
69     'Twój 1% uwolni więcej lektur.',
70     'Ponad 400 motywów, blisko 10 000 000 cytatów. Pomóż znaleźć następne. Przekaż swój 1%.',
71     'Twój 1% uwolni lektury.',
72     'Rozlicz swój PIT z Wolnymi Lekturami. Skorzystaj z darmowego programu do rozliczania podatków.',
73     'Lektury 2010: Pan Tadeusz, Trylogia.',
74     'Pan Tadeusz też chce być w Internecie! Przekaż 1% swojego podatku.',
75     'Pomóż uwolnić 286 utworów z listy lektur szkolnych. Przekaż swój 1% na Wolne Lektury.'
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         $.fn.toggle_slide = function(p) {
112             cont = $(this);
113             short_el = p['short_el'] || $(':first-child', this);
114             long_el = p['long_el'] || short_el.next();
115             button = p['button'];
116             short_text = p['short_text'],
117             long_text = p['long_text'];
118
119             var toggle_fun = function(cont, short_el, long_el, button, short_text, long_text) {
120                 var toggle = function() {
121                     if (cont.hasClass('short')) {
122                         cont.animate({"height": long_el.attr("cont_h")+'px'}, {duration: "fast" }).removeClass('short');
123                         short_el.hide();
124                         long_el.show();
125                         if (button && long_text) button.html(long_text);
126                     } else {
127                         cont.animate({"height": short_el.attr("cont_h")+'px'}, {duration: "fast" }).addClass('short');
128                         long_el.hide();
129                         short_el.show();
130                         if (button && short_text) button.html(short_text);
131                     }
132                     return false;
133                 }
134                 return toggle;
135             }
136             if (long_el.html().length <= short_el.html().length)
137                 return;
138
139             // ensure long element shown first
140             long_el.show();short_el.hide();
141             long_el.attr("cont_h", $(this).height()).hide();
142             short_el.show().attr("cont_h", $(this).height());
143             $(this).addClass('short');
144
145             if (button && short_text) button.html(short_text);
146             if (button) button.hover(
147                 function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
148                 function() { $(this).css({background: '#EEE'}); }
149             ).click(toggle_fun(cont, short_el, long_el, button, short_text, long_text));
150             short_el.hover(
151                 function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
152                 function() { $(this).css({background: '#FFF'}); }
153             ).click(toggle_fun(cont, short_el, long_el, button, short_text, long_text));
154             long_el.hover(
155                 function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
156                 function() { $(this).css({background: '#FFF'}); }
157             ).click(toggle_fun(cont, short_el, long_el, button, short_text, long_text));
158         };
159
160         $('form input').labelify({labelledClass: 'blur'});
161
162         target = $('#login-register-window div.target');
163
164         $('#show-registration-form').click(function() {
165             $('#login-form').hide();
166             $('#registration-form').show();
167         });
168
169         $('#show-login-form').click(function() {
170             $('#registration-form').hide();
171             $('#login-form').show();
172         });
173
174         // Fragments
175         $('.fragment-short-text').each(function() {
176             var fragment = $(this).closest('.fragment');
177             fragment.toggle_slide({
178                 short_el: $(this),
179                 long_el: fragment.find('.fragment-text')
180             })
181         });
182
183         $('.show-all-tags').click(function() {
184             $(this).parent().parent().fadeOut(function() {
185                 $(this).next().fadeIn();
186             });
187             return false;
188         });
189
190         $('.hide-all-tags').click(function() {
191            $(this).parent().parent().fadeOut(function() {
192                $(this).prev().fadeIn();
193            });
194            return false;
195         });
196
197         $('#registration-form').ajaxForm({
198             dataType: 'json',
199             beforeSubmit: function() {
200                 $('#registration-form input[type=submit]')
201                     .attr('disabled', 'disabled')
202                     .after('<img src="/static/img/indicator.gif" style="margin-left: 0.5em"/>');
203             },
204             success: function(response) {
205                 if (response.success) {
206                     location.reload(true);
207                 } else {
208                     $('#registration-form span.error').remove();
209                     $.each(response.errors, function(id, errors) {
210                         $('#id_registration-' + id).before('<span class="error">' + errors[0] + '</span>');
211                     });
212                     $('#registration-form input[type=submit]').removeAttr('disabled');
213                     $('#registration-form img').remove();
214                 }
215             }
216         });
217
218         $('#login-form').ajaxForm({
219             dataType: 'json',
220             beforeSubmit: function() {
221                 $('#login-form input[type=submit]')
222                     .attr('disabled', 'disabled')
223                     .after('<img src="/static/img/indicator.gif" style="margin-left: 0.5em"/>');
224             },
225             success: function(response) {
226                 if (response.success) {
227                     location.reload(true);
228                 } else {
229                     $('#login-form span.error').remove();
230                     $.each(response.errors, function(id, errors) {
231                         $('#id_login-' + id).before('<span class="error">' + errors[0] + '</span>');
232                     });
233                     $('#login-form input[type=submit]').removeAttr('disabled');
234                     $('#login-form img').remove();
235                 }
236             }
237         });
238
239         $('#login-register-window').jqm({
240             target: target[0],
241             overlay: 60,
242             trigger: '.login-register-link',
243             onShow: function(hash) {
244                 var offset = $(hash.t).offset();
245                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
246                 $('div.header', hash.w).css({width: $(hash.t).width()});
247                 hash.w.show();
248             }
249         });
250
251         $('ul.shelf-list li').hover(function() {
252             $(this).css({background: '#EEE', cursor: 'pointer'});
253         }, function() {
254             $(this).css({background: 'transparent'});
255         }).click(function() {
256             location.href = $('a.visit-shelf', this).attr('href');
257         });
258
259         $('.delete-shelf').click(function() {
260             var link = $(this);
261             var shelf_name = $('.visit-shelf', link.parent()).text();
262             if (confirm(LOCALE_TEXTS[LANGUAGE_CODE]['DELETE_SHELF']+ ' '+ shelf_name + '?')) {
263                 $.post(link.attr('href'), function(data, textStatus) {
264                     link.parent().remove();
265                 });
266             }
267             return false;
268         });
269
270         var serverResponse;
271         $('#user-shelves-window').jqm({
272             ajax: '@href',
273             target: $('#user-shelves-window div.target')[0],
274             overlay: 60,
275             trigger: '#user-shelves-link',
276             onShow: function(hash) {
277                 var offset = $(hash.t).offset();
278                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
279                 $('div.header', hash.w).css({width: $(hash.t).width()});
280                 hash.w.show();
281             },
282             onLoad: function(hash) {
283                 $('form', hash.w).ajaxForm({
284                     target: serverResponse,
285                     success: function(serverResponse) {
286                         var newShelf = $.parseJSON(serverResponse);
287                         $('#user-shelves-window div.target').html(newShelf.msg);
288                         setTimeout(function() { $('#user-shelves-window').jqmHide() }, 1000);
289                     }
290                 });
291
292                 $('input', hash.w).labelify({labelledClass: 'blur'});
293
294                 $('ul.shelf-list li', hash.w).hover(function() {
295                     $(this).css({background: '#EEE', cursor: 'pointer'});
296                 }, function() {
297                     $(this).css({background: 'transparent'});
298                 }).click(function() {
299                     location.href = $('a.visit-shelf', this).attr('href');
300                 });
301
302                 $('.delete-shelf').click(function() {
303                     var link = $(this);
304                     var shelf_name = $('.visit-shelf', link.parent()).text();
305                     if (confirm(LOCALE_TEXTS[LANGUAGE_CODE]['DELETE_SHELF'] + ' ' + shelf_name + '?')) {
306                         $.post(link.attr('href'), function(data, textStatus) {
307                             link.parent().remove();
308                         });
309                     }
310                     return false;
311                 });
312             }
313         });
314
315         $('#suggest-window').jqm({
316             ajax: '@href',
317             target: $('#suggest-window div.target')[0],
318             overlay: 60,
319             trigger: '#suggest-link',
320             onShow: function(hash) {
321                 var offset = $(hash.t).offset();
322                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
323                 $('div.header', hash.w).css({width: $(hash.t).width()});
324                 hash.w.show();
325             },
326             onLoad: function(hash) {
327                 $('form', hash.w).ajaxForm({
328                     dataType: 'json',
329                     target: $('#suggest-window div.target'),
330                     success: function(response) {
331                         if (response.success) {
332                             $('#suggest-window div.target').text(response.message);
333                             setTimeout(function() { $('#suggest-window').jqmHide() }, 1000)
334                         }
335                         else {
336                             $('#suggest-form .error').remove();
337                             $.each(response.errors, function(id, errors) {
338                                 $('#suggest-form #id_' + id).before('<span class="error">' + errors[0] + '</span>');
339                             });
340                             $('#suggest-form input[type=submit]').removeAttr('disabled');
341                             return false;
342                         }
343                     }
344                 });
345             }
346         });
347
348         $('#suggest-publishing-window').jqm({
349             ajax: '@data-ajax',
350             target: $('#suggest-publishing-window div.target')[0],
351             overlay: 60,
352             trigger: '#suggest-publishing-link',
353             onShow: function(hash) {
354                 var offset = $(hash.t).offset();
355                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
356                 $('div.header', hash.w).css({width: $(hash.t).width()});
357                 hash.w.show();
358             },
359             onLoad: function(hash) {
360                 $('form', hash.w).each(function() {this.action += '?ajax=1';});
361                 $('form', hash.w).ajaxForm({
362                     dataType: 'json',
363                     target: $('#suggest-publishing-window div.target'),
364                     success: function(response) {
365                         if (response.success) {
366                             $('#suggest-publishing-window div.target').text(response.message);
367                             setTimeout(function() { $('#suggest-publishing-window').jqmHide() }, 1000)
368                         }
369                         else {
370                             $('#suggest-publishing-form .error').remove();
371                             $.each(response.errors, function(id, errors) {
372                                 $('#suggest-publishing-form #id_' + id).before('<span class="error">' + errors[0] + '</span>');
373                             });
374                             $('#suggest-publishing-form input[type=submit]').removeAttr('disabled');
375                             return false;
376                         }
377                     }
378                 });
379             }
380         });
381
382         (function($this) {
383             $form = $('form', $this);
384             $form.each(function() {this.action += '?ajax=1';});
385             $form.ajaxForm({
386                 dataType: 'json',
387                 target: $this,
388                 success: function(response) {
389                     if (response.success) {
390                         $this.text(response.message);
391                     }
392                     else {
393                         $('.error', $form).remove();
394                         $.each(response.errors, function(id, errors) {
395                             $('#id_' + id, $form).before('<span class="error">' + errors[0] + '</span>');
396                         });
397                         $('input[type=submit]', $form).removeAttr('disabled');
398                         return false;
399                     }
400                 }
401             });
402         })($('.block-form'));
403         
404         $('#books-list .book').hover(
405             function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
406             function() { $(this).css({background: '#FFF'}); }
407         ).click(function() {
408             location.href = $('h2 a', this).attr('href');
409         });
410
411         $('#description').each(function(){$(this).toggle_slide({
412             long_el: $('#description-long', this),
413             short_el: $('#description-short', this),
414             button: $(this).nextAll('#toggle-description').first().find('p'),
415             long_text: LOCALE_TEXTS[LANGUAGE_CODE]['HIDE_DESCRIPTION'] + ' ▲',
416             short_text: LOCALE_TEXTS[LANGUAGE_CODE]['EXPAND_DESCRIPTION'] + ' ▼'
417         })});
418
419         $('#toggle-share-shelf').hover(
420             function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
421             function() { $(this).css({background: '#EEE'}); }
422         ).click(function() {
423             if ($('#share-shelf').hasClass('hidden')) {
424                 $('#share-shelf').slideDown('fast').removeClass('hidden');
425             } else {
426                 $('#share-shelf').slideUp('fast').addClass('hidden');
427             }
428         });
429
430         var target = $('#set-window div.target');
431
432         $('#set-window').jqm({
433             ajax: '@href',
434             target: target[0],
435             overlay: 60,
436             trigger: 'a.jqm-trigger',
437             onShow: function(hash) {
438                 var offset = $(hash.t).offset();
439                 target.html('<p><img src="/static/img/indicator.gif" />'+LOCALE_TEXTS[LANGUAGE_CODE]['DELETE_SHELF']+'</p>');
440                 hash.w.css({position: 'absolute', left: offset.left, top: offset.top}).show() },
441             onLoad: function(hash) {
442                 try {
443                         $('#createShelfTrigger').click(function(){
444                             // who cares it's not needed, but I was looking for it
445                             // that's why I want it to stay.. :) 
446                             // var slug = $(hash.t).attr('href').split("/")[3];
447                             $('#createNewShelf').show();
448                         });
449                 } catch (e){}
450                 var serverResponse;
451                 $('form', hash.w).ajaxForm({
452                     target: serverResponse,
453                     success: function(serverResponse) {
454                         var newShelf = $.parseJSON(serverResponse);
455                         // for live shelf adding
456                         if(newShelf.name){
457                             var noIds = $("#putOnShelf ol ul").children('li').length;
458                             $("#putOnShelf ol ul").prepend('<li><label for="id_set_ids_'+ noIds +'"><input name="set_ids" value="'+ newShelf.id +'" id="id_set_ids_'+ noIds +'" type="checkbox" checked="checked"> '+ newShelf.name +' (0)</label></li>');
459                             $("#createNewShelf ol input[name=name]").val("");
460                         }
461                         if(newShelf.after == "close"){
462                             setTimeout(function() {$('#set-window').jqmHide();}, 1000);
463                         }
464                     }
465                 });    
466             }
467         });
468
469         $('a.remove-from-shelf').click(function(event) {
470             event.preventDefault();
471             link = $(this);
472             $.post(link.attr('href'), function(data, textStatus) {
473                 link.parent().remove();
474             });
475         });
476
477         $('#share-shelf').hide().addClass('hidden');
478         $('#share-shelf input').focus(function(){this.select();});
479
480         $('#user-info').show();
481         changeBannerText();
482         $('#onepercent-banner').show();
483
484         var formatsDownloaded = false;
485         $('#download-shelf').click(function() {
486             $('#download-shelf-menu').slideDown('fast');
487
488             if (!formatsDownloaded) {
489                 // Get info about the formats
490                 formatsDownloaded = true;
491                 $.ajax({
492                     url: $('#download-formats-form').attr('data-formats-feed'),
493                     type: 'GET',
494                     dataType: 'json',
495                     complete: function() {
496                         $('#download-formats-form-submit').attr('disabled', null);
497                         $('#download-formats-form-submit-li img').remove();
498                         $('#updating-formats').fadeOut('fast', function() {
499                             $('#formats-updated').fadeIn('fast');
500                         });
501                     },
502                     success: function(data) {
503                         $('#download-formats-form li').each(function() {
504                             var item = $(this);
505                             if (!!item.attr('data-format') && !data[item.attr('data-format')]) {
506                                 item.fadeOut('fast', function() {
507                                     item.remove();
508                                 });
509                             }
510                         });
511                     }
512                 });
513             }
514             return false;
515         });
516
517         $('#download-formats-form-cancel').click(function() {
518             $('#download-shelf-menu').slideUp('fast');
519             return false;
520         });
521
522         $('.sponsor-logos').cycle({timeout: 3000});
523
524         $('.widget-code').focus(
525             function(){
526                 $(this).animate({rows: '11'}, 100, function(){
527                     this.select();
528                     $(this).click(function(){
529                         this.select();
530                     });
531                 })
532                 
533             } 
534         ).blur(
535             function(){
536                 $(this).animate({rows: '1'}, 300, function(){
537                     $(this).unbind('click');
538                 })
539             } 
540         );
541         
542         $('.book-list-index').click(function(){
543             $('.book-list-show-index').hide('slow');
544             if($(this).parent().next('ul:not(:hidden)').length == 0){
545                 $(this).parent().next('ul').toggle('slow');
546             }
547             return false;
548         });
549
550         // player for audiobooks
551  
552         // event handlers for playing different formats
553         $('.audiotabs span').click(function(){
554             $('.audiobook-list').hide();
555             $('.audiotabs .active').removeClass('active');
556             // we don't want to interact with "audiobook" label, just 'format' tabs
557             var $this = $(this);
558             $this.addClass("active");
559             $("#"+$this.attr('data-format')+"-files").show();
560         });
561
562         $('.audiobook-list').hide();
563         if($(".audiotabs .active").length > 0) {
564             $("#"+$(".audiotabs .active").html().toLowerCase()+"-files").show();
565         }
566         
567         /* this will be useful for javascript html player
568         var medias = $('.audiobook-list a');
569         var mp3List = [];
570         var oggList = [];
571         var daisyList = [];
572         var tmpExt;
573         if (medias.length > 0) {
574             // creating sources list for player
575             medias.each(function(index, item) {
576                 tmpExt = item.href.split(".").pop();    
577                 if(tmpExt == "mp3") {
578                     mp3List.push(item.href);
579                 } else if (tmpExt == "ogg") {
580                     oggList.push(item.href);
581                 } else if(tmpExt == "daisy") {
582                     daisyList.push(item.href);
583                 }
584             }); 
585         }*/       
586
587     });
588 })(jQuery)
589