5bbc1a8d85f739ab91f9fcfad9e4e8331a2905e0
[wolnelektury.git] / apps / wolnelektury_core / static / js / book_text / menu.js
1 (function($){$(function(){
2
3
4 function hide_menu_boxes() {
5     /* Closes any open menu boxes. */
6     $("#menu .active").each(function() {
7         $(this).removeClass("active");
8         $("#" + $(this).attr("data-box")).hide();
9         if ($(this).hasClass('dropdown')) {
10             $($(this).attr('href')).slideUp('fast');
11         }
12     });
13     $("#box-underlay").hide();
14 }
15
16 function release_menu() {
17     /* Exits the menu. It only really disappears on small screens. */
18     hide_menu_boxes();
19     $("body").removeClass("menu-showed");
20 }
21
22 /* Show menu */
23 $('#menu-toggle-on').click(function(e) {
24     e.preventDefault();
25     var body = $("body");
26     /* Just stop hiding the menu. This way, after narrowing the browser,
27      * menu will still disappear normally. */
28     body.removeClass("menu-hidden");
29     /* Menu still not visible? Really open it then. */
30     if (!$("#menu").is(":visible")) {
31         body.addClass("menu-showed");
32     }
33 });
34
35 /* Hide menu */
36 $('#menu-toggle-off').click(function(e) {
37     e.preventDefault();
38     /* Just release the menu. This way, after widening the browser,
39      * menu will still appear normally. */
40     release_menu();
41     /* Menu still visible after releasing it? Really hide it then. */
42     if ($("#menu").is(":visible")) {
43         $("body").addClass("menu-hidden");
44     }
45 });
46
47
48 /* Exit menu by clicking anywhere else. */
49 $("#box-underlay").click(release_menu);
50
51
52 /* Toggle hidden box on click. */
53 $("#menu a").each(function() {
54     var boxid = $(this).attr("data-box");
55     if (boxid) {
56         $("#" + $(this).attr("data-box")).hide();
57
58         $(this).click(function(e) {
59             e.preventDefault();
60             var showing = $(this).hasClass("active");
61             hide_menu_boxes();
62             if (!showing) {
63                 $("body").addClass("menu-showed");
64                 $(this).addClass("active");
65                 $("#box-underlay").show();
66                 $("#" + $(this).attr("data-box")).show();
67             }
68         });
69     }
70     else if ($(this).hasClass('dropdown')) {
71         $(this).click(function(e) {
72             e.preventDefault();
73             var showing = $(this).hasClass("active");
74             hide_menu_boxes();
75             if (!showing) {
76                 $("body").addClass("menu-showed");
77                 $("#sponsors:not(:hidden)").fadeOut();
78                 $(this).addClass("active");
79                 $($(this).attr('href')).slideDown('fast');
80             }
81         });
82     }
83     else if (!$(this).hasClass('button')) {
84         $(this).click(release_menu);
85     }
86 });
87
88
89 /* Show menu item for other versions of text. 
90  * It's only present if there are any. */
91 $("#menu-other").show();
92
93
94 /* Load other version of text. */
95 $(".display-other").click(function(e) {
96     e.preventDefault();
97     release_menu();
98
99     $("#other-text").show();
100     $("body").addClass('with-other-text');
101
102     $.ajax($(this).attr('data-other'), {
103         success: function(text) {
104             $("#other-text-body").html(text);
105             $("#other-text-waiter").hide();
106             $("#other-text-body").show();
107             loaded_text($("#other-text-body"));
108         }
109     });
110 });
111
112
113 /* Remove other version of text. */
114 $(".other-text-close").click(function(e) {
115     release_menu();
116     e.preventDefault();
117     $("#other-text").hide();
118     $("body").removeClass('with-other-text');
119     $("#other-text-body").html("");
120 });
121
122
123 /* Release menu after clicking inside TOC. */
124 $("#toc a").click(release_menu);
125
126
127 if ($('#nota_red').length > 0) {
128     $("#menu-nota_red").show();
129 }
130
131 /* Show themes menu item, if there are any. */
132 if ($('#themes li').length > 0) {
133     $("#menu-themes").show();
134 }
135
136 function loaded_text(text) {
137     /* Attach events to elements inside book texts here.
138      * This way they'll work for the other text when it's loaded. */
139
140     $(".theme-begin", text).click(function(e) {
141         e.preventDefault();
142         if ($(this).css("overflow") == "hidden" || $(this).hasClass('showing')) {
143             $(this).toggleClass("showing");
144         }
145     });
146
147 }
148 loaded_text("#book-text");
149
150
151 })})(jQuery);