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