Add table of contents to documentation pages
[exim-website.git] / templates / web / common.js
1 // Fix the header and navigation at the top of the page
2         (function(){
3                 $('#header').addClass('fixed');
4                 if( $('#header').css('position') === 'fixed' ){
5                         $('.navigation')
6                                 .before( $('.navigation').clone().addClass('spacer') )
7                                 .addClass('fixed');
8                         $('#outer > .right_bar, #outer > .left_bar').addClass('display');
9                 }
10         })();
11
12 // Add branding for mirrors
13         if( document.location.href.match(/^https?:\/\/([^\/]+\.)*exim\.org\//) ){
14                 $('#branding').remove();
15         } else {
16                 $('#branding').ready(function(){
17                         try {
18                                 var doc = $('#branding')[0].contentWindow.document;
19                                 if( doc.title.match(/\b(found|404)\b/i) ){ // Crude but "good enough" check to see if the branding request failed
20                                         $('#branding').remove();
21                                 } else {
22                                         $(doc).find('a').each(function(){
23                                                 if( $(this).attr('title') == '' )
24                                                         $(this).attr('title','Sponsor of this mirror');
25                                                 $(this).css('opacity',0.8)
26                                                         .mouseover(function(){ $(this).css('opacity',1) })
27                                                         .mouseout(function(){ $(this).css('opacity',0.8) });
28                                         });
29                                         $('#branding')
30                                                 .height( $(doc).find('img').height() ? $(doc).find('img').height() + 16 + 'px' : 'auto' )
31                                                 .hide()
32                                                 .css('visibility','visible')
33                                                 .fadeIn(2000);
34                                 }
35                         } catch(e){
36                                 $('#branding').remove();
37                         }
38                 });
39         }
40
41 // Footer
42         (function(){
43                 $('#footer').hide();
44                 setTimeout( function(){ $('#footer').fadeIn('slow') }, 2000 );
45         })();
46
47 // Search box
48         (function(){
49
50                 // Add placeholder functionality to browsers which don't support it
51                         if( !('placeholder' in document.createElement('input')) )
52                                 $('.navigation li.search input.search_field')
53                                         .focus(function(e){
54                                                 if( $(this).val() === ' ' + $(this).attr('placeholder') )
55                                                         $(this).val('').css('color','#000');
56                                         })
57                                         .blur(function(e){
58                                                 if( $(this).val() === ' ' + $(this).attr('placeholder') || $(this).val() === '' )
59                                                         $(this).css('color','#666').val( ' ' + $(this).attr('placeholder') );
60                                         }).blur();
61
62                 // Add rounded borders to search field on Gecko based browsers
63                         if( document.body.style.MozBorderRadius !== undefined )
64                                 $('.search_field_container')
65                                         .addClass('roundit')
66                                         .click(function(){ $(this).find('input').focus() });
67         })();
68
69 // Jump to the right location on the page. Fixed header can cause problems.
70         if( $('#header').css('position') === 'fixed' ){
71
72                 // Jump to the given ID
73                         var jump = function( id ){
74                                 if( $('#'+id).length == 0 ) return false;
75
76                                 document.location.href = document.location.href.replace(/#.+/,'') + '#' + id;
77
78                                 $('html,body').animate({
79                                         scrollTop: $('#'+id).position()['top'] - $('#header').height() - $('.navigation.fixed').height() -5 
80                                 }, 100);
81
82                                 return true;
83                         };
84
85                 var uri         = document.location.pathname;
86                 var uri_end     = uri.replace(/^.*\//,'');
87
88                 // Page load
89                         if( document.location.href.match(/#./) ) jump( document.location.href.replace(/^.*#(.+)$/,'$1') );
90
91                 // Anchor click
92                         $('a').live('click', function(e){
93                                 var href = $(this).attr('href');
94                                 if( !href.match(/^.*#.+$/) ) return true; // No # in the anchor
95
96                                 var href_uri = href.replace(/^([^#]*)(#.*)?/,'$1'); // href without the #
97
98                                 if( href_uri.match(/^([a-z]+:)?\/\//)               ) return true; // Ignore full URLs
99                                 if( href_uri.match(/^[^\/]/) && href_uri != uri_end ) return true; // Ignore relative links to other pages
100                                 if( href_uri.match(/^\//)    && href_uri != uri     ) return true; // Ignore absolute links to other pages
101
102                                 if( jump( href.replace(/^.*#(.+)$/,'$1') ) ) e.preventDefault();
103                         });
104
105                 // For browsers which support it, detect when the hash in the address bar changes
106                         $(window).bind('hashchange',function(e){
107                                 if( jump( document.location.href.replace(/^.*#(.+)$/,'$1') ) ) e.preventDefault();
108                         });
109         }
110