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