07fba6bf350601a346ea75a43d98e0718d59fb9a
[bootswatch] / js / bootswatch.js
1 // tooltips
2
3 $('a[rel=tooltip]').tooltip({
4         'placement': 'bottom'
5 });
6
7 // recent news
8
9 function parseRSS(url, callback) {
10   $.ajax({
11     url: 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&callback=?&q=' + encodeURIComponent(url),
12     dataType: 'json',
13     success: function(data) {
14       callback(data.responseData.feed);
15     }
16   });
17 }
18
19 // smooth scroll
20
21 $(document).ready(function() {
22   function filterPath(string) {
23   return string
24     .replace(/^\//,'')
25     .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
26     .replace(/\/$/,'');
27   }
28   var locationPath = filterPath(location.pathname);
29   var scrollElem = scrollableElement('html', 'body');
30  
31   $('a[href^=#]').each(function() {
32     var thisPath = filterPath(this.pathname) || locationPath;
33     if (  locationPath == thisPath
34     && (location.hostname == this.hostname || !this.hostname)
35     && this.hash.replace(/#/,'') ) {
36       var $target = $(this.hash), target = this.hash;
37       if (target) {
38         var targetOffset = $target.offset().top;
39         $(this).click(function(event) {
40           event.preventDefault();
41           $(scrollElem).animate({scrollTop: targetOffset}, 400, function() {
42             location.hash = target;
43           });
44         });
45       }
46     }
47   });
48  
49   // use the first element that is "scrollable"
50   function scrollableElement(els) {
51     for (var i = 0, argLength = arguments.length; i <argLength; i++) {
52       var el = arguments[i],
53           $scrollElement = $(el);
54       if ($scrollElement.scrollTop()> 0) {
55         return el;
56       } else {
57         $scrollElement.scrollTop(1);
58         var isScrollable = $scrollElement.scrollTop()> 0;
59         $scrollElement.scrollTop(0);
60         if (isScrollable) {
61           return el;
62         }
63       }
64     }
65     return [];
66   }
67  
68 });
69
70 // subnav
71
72 (function ($) {
73
74         $(function(){
75
76                 // fix sub nav on scroll
77                 var $win = $(window),
78                                 $body = $('body'),
79                                 $nav = $('.subnav'),
80                                 navHeight = $('.navbar').first().height(),
81                                 subnavHeight = $('.subnav').first().height(),
82                                 subnavTop = $('.subnav').length && $('.subnav').offset().top - navHeight,
83                                 marginTop = parseInt($body.css('margin-top'), 10);
84                                 isFixed = 0;
85
86                 processScroll();
87
88                 $win.on('scroll', processScroll);
89
90                 function processScroll() {
91                         var i, scrollTop = $win.scrollTop();
92
93                         if (scrollTop >= subnavTop && !isFixed) {
94                                 isFixed = 1;
95                                 $nav.addClass('subnav-fixed');
96                                 $body.css('margin-top', marginTop + subnavHeight + 'px');
97                         } else if (scrollTop <= subnavTop && isFixed) {
98                                 isFixed = 0;
99                                 $nav.removeClass('subnav-fixed');
100                                 $body.css('margin-top', marginTop + 'px');
101                         }
102                 }
103
104         });
105
106 })(window.jQuery);