Fixes #4001: TOC contains parent books, and add links to previous and next book.
[wolnelektury.git] / src / wolnelektury / static / js / book.js
1 $(function() {
2     function scrollToAnchor(anchor) {
3         if (anchor) {
4             var anchor_name = anchor.slice(1);
5             var element = $('a[name="' + anchor_name + '"]');
6             if (element.length > 0) {
7                 $.scrollTo(element, 500, {offset: {top: -50, left: 0}});
8                 foot_elem = $('#footnotes a[name="' + anchor_name + '"]');
9                 if (foot_elem.length > 0) {
10                     $(element).parent().highlightFade('yellow');
11                 }
12                 window.location.hash = anchor;
13             }
14         }
15     }
16
17     $.highlightFade.defaults.speed = 3000;
18     if ($('#themes li').length == 0) {
19         $('#menu li a[href="#themes"]').remove();
20     }
21     if ($('#nota_red').length == 0) {
22         $('#menu li a[href="#nota_red"]').remove();
23     }
24
25     // On page load, scroll to anchor
26     scrollToAnchor(window.location.hash)
27
28     $('#toc, #themes, #book-text').delegate('click', 'a', function(event) {
29         event.preventDefault();
30         $('#menu li a.selected').click();
31         scrollToAnchor($(this).attr('href'));
32     });
33
34     $('#menu li a.menu').toggle(function() {
35         $('#menu li a.selected').click();
36         $(this).addClass('selected');
37         $($(this).attr('href')).slideDown('fast');
38     }, function() {
39         $(this).removeClass('selected');
40         $($(this).attr('href')).slideUp('fast');
41     });
42     
43
44     if (window.getSelection) {
45         $('.theme-begin').click(function() {
46             var selection = window.getSelection();
47             selection.removeAllRanges();
48             var range = document.createRange();
49
50             var e = $(".theme-end[fid='" + $(this).attr('fid') + "']")[0];
51
52             if (e) {
53                 range.setStartAfter(this);
54                 range.setEndBefore(e);
55                 selection.addRange(range);
56             }
57         });
58     }
59
60 });