Added "delete" button to book banners if owner of viewed shelf is the current user...
[wolnelektury.git] / wolnelektury / media / js / catalogue.js
1 (function($) {
2     $(function() {
3         $('form input').labelify({labelledClass: 'blur'});
4         
5         target = $('#login-register-window div.target');
6         
7         $('#show-registration-form').click(function() {
8             $('#login-form').hide();
9             $('#registration-form').show();
10         });
11         
12         $('#show-login-form').click(function() {
13             $('#registration-form').hide();
14             $('#login-form').show();
15         });
16         
17         // Fragments
18         $('.fragment-text').each(function() {
19             if ($(this).prev().filter('.fragment-short-text').length) {
20                 $(this).hover(
21                     function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
22                     function() { $(this).css({background: '#FFF'}); }
23                 ).click(function() {
24                     $(this).fadeOut(function() {
25                         $(this).prev().fadeIn()
26                     });
27                 })
28             }
29         });
30         
31         $('.fragment-short-text').click(function() {
32             $(this).fadeOut(function() { $(this).next().fadeIn() });
33         }).hover(
34             function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
35             function() { $(this).css({background: '#FFF'}); }
36         );
37         
38         $('.show-all-tags').click(function() {
39             $(this).parent().parent().fadeOut(function() { 
40                 $(this).next().fadeIn();
41             });
42             return false;
43         });
44         
45         $('#registration-form').ajaxForm({
46             dataType: 'json',
47             beforeSubmit: function() {
48                 $('#registration-form input[type=submit]')
49                     .attr('disabled', 'disabled')
50                     .after('<img src="/media/img/indicator.gif" style="margin-left: 0.5em"/>');
51             },
52             success: function(response) {
53                 if (response.success) {
54                     location.reload(true);
55                 } else {
56                     $('#registration-form span.error').remove();
57                     $.each(response.errors, function(id, errors) {
58                         $('#id_registration-' + id).before('<span class="error">' + errors[0] + '</span>');
59                     });
60                     $('#registration-form input[type=submit]').removeAttr('disabled');
61                     $('#registration-form img').remove();
62                 }
63             }
64         });
65         
66         $('#login-form').ajaxForm({
67             dataType: 'json',
68             beforeSubmit: function() {
69                 $('#login-form input[type=submit]')
70                     .attr('disabled', 'disabled')
71                     .after('<img src="/media/img/indicator.gif" style="margin-left: 0.5em"/>');
72             },
73             success: function(response) {
74                 if (response.success) {
75                     location.reload(true);
76                 } else {
77                     $('#login-form span.error').remove();
78                     $.each(response.errors, function(id, errors) {
79                         $('#id_login-' + id).before('<span class="error">' + errors[0] + '</span>');
80                     });
81                     $('#login-form input[type=submit]').removeAttr('disabled');
82                     $('#login-form img').remove();
83                 }
84             }
85         });
86         
87         $('#login-register-window').jqm({
88             target: target[0],
89             overlay: 60,
90             trigger: '.login-register-link',
91             onShow: function(hash) {
92                 var offset = $(hash.t).offset();
93                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
94                 $('div.header', hash.w).css({width: $(hash.t).width()});
95                 hash.w.show();
96             }
97         });
98         
99         $('ul.shelf-list li').hover(function() {
100             $(this).css({background: '#EEE', cursor: 'pointer'});
101         }, function() {
102             $(this).css({background: 'transparent'});
103         }).click(function() {
104             location.href = $('a.visit-shelf', this).attr('href');
105         });
106         
107         $('.delete-shelf').click(function() { 
108             var link = $(this);
109             var shelf_name = $('.visit-shelf', link.parent()).text();
110             if (confirm('Czy na pewno usunąć półkę ' + shelf_name + '?')) {
111                 $.post(link.attr('href'), function(data, textStatus) {
112                     link.parent().remove();
113                 });
114             }
115             return false;
116         });
117         
118         $('#user-shelves-window').jqm({
119             ajax: '@href',
120             target: $('#user-shelves-window div.target')[0],
121             overlay: 60,
122             trigger: '#user-shelves-link',
123             onShow: function(hash) {
124                 var offset = $(hash.t).offset();
125                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
126                 $('div.header', hash.w).css({width: $(hash.t).width()});
127                 hash.w.show();
128             },
129             onLoad: function(hash) { 
130                 $('form', hash.w).ajaxForm({
131                     target: $('#user-shelves-window div.target'),
132                     success: function() { setTimeout(function() { $('#user-shelves-window').jqmHide() }, 1000) }
133                 });
134                 
135                 $('input', hash.w).labelify({labelledClass: 'blur'});
136                 
137                 $('ul.shelf-list li', hash.w).hover(function() {
138                     $(this).css({background: '#EEE', cursor: 'pointer'});
139                 }, function() {
140                     $(this).css({background: 'transparent'});
141                 }).click(function() {
142                     location.href = $('a.visit-shelf', this).attr('href');
143                 });
144                 
145                 $('.delete-shelf').click(function() {
146                     var link = $(this);
147                     var shelf_name = $('.visit-shelf', link.parent()).text();
148                     if (confirm('Czy na pewno usunąć półkę ' + shelf_name + '?')) {
149                         $.post(link.attr('href'), function(data, textStatus) {
150                             link.parent().remove();
151                         });
152                     }
153                     return false;
154                 });
155             }
156         });
157     
158         $('#books-list .book').hover(
159             function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
160             function() { $(this).css({background: '#FFF'}); }
161         ).click(function() {
162             location.href = $('h2 a', this).attr('href');
163         });
164     
165         $('#toggle-description').hover(
166             function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
167             function() { $(this).css({background: '#EEE'}); }
168         ).click(function() {
169             if ($('#description').hasClass('hidden')) {
170                 $('#description').slideDown('fast').removeClass('hidden');
171                 $('p', this).html('Zwiń opis ▲');
172             } else {
173                 $('#description').slideUp('fast').addClass('hidden');
174                 $('p', this).html('Rozwiń opis ▼');
175             }
176         });
177     
178         var target = $('#set-window div.target');
179     
180         $('#set-window').jqm({
181             ajax: '@href', 
182             target: target[0],
183             overlay: 60,
184             trigger: 'a.jqm-trigger', 
185             onShow: function(hash) { 
186                 var offset = $(hash.t).offset();
187                 target.html('<p><img src="/media/img/indicator.gif" /> Ładowanie</p>');
188                 hash.w.css({position: 'absolute', left: offset.left, top: offset.top}).show() },
189             onLoad: function(hash) { 
190                 $('form', hash.w).ajaxForm({
191                     target: target,
192                     success: function() { setTimeout(function() { $('#set-window').jqmHide() }, 1000) }
193                 });
194             }
195         });
196         
197         $('a.remove-from-shelf').click(function(event) {
198             event.preventDefault();
199             link = $(this);
200             $.post(link.attr('href'), function(data, textStatus) {
201                 link.parent().remove();
202             });
203         });
204
205     });
206 })(jQuery)