bef1f13d6528f9121ce65af5bf3626e3dc7c872e
[wolnelektury.git] / src / annoy / static / annoy / book_text.js
1 (function($) {
2     $(function() {
3
4
5         var fold = $(window).scrollTop() + $(window).height();
6
7         var inserts = [];
8         $("#annoy-stubs .dynamic-insert").each(function() {inserts.push($(this));});
9
10         var $intermissions = $("#annoy-stubs .annoy-banner_book-text-intermission");
11         if ($intermissions.length) {
12             var which = 0;
13             $("#book-text a + h2").each(function(i, e) {
14                 console.log(i);
15                 if (i) {
16                     $($intermissions[which]).clone().insertBefore($(this)).show();
17                     which = (which + 1) % $intermissions.length;
18                 }
19             });
20
21             if ($("#footnotes").length) {
22                 $($intermissions[which]).clone().insertBefore($("#footnotes")).show();
23             } else {
24                 $($intermissions[which]).clone().appendTo($("#book-text")).show();
25             }
26         };
27
28         if (inserts) {
29             var underFold = false;
30             var counter = 0;
31             $(".paragraph, .stanza").each(function() {
32                 var p = $(this);
33                 if (p.prev().hasClass('anchor')) p = p.prev();
34                 if (!underFold) {
35                     if (p.offset().top > fold) {
36                         underFold = true;
37                     }
38                 }
39                 if (underFold) {
40                     if (inserts[0].attr('data-paragraphs') == counter) {
41                         insert = inserts.shift();
42                         insert.insertBefore(p);
43                     }
44                     counter += 1;
45                 }
46                 return inserts.length > 0;
47             });
48         };
49
50         
51     });
52 })(jQuery);