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