3 * http://rxa.li/documenter
5 * Copyright 2011, Xaver Birsak
9 //if Cufon replace headings
10 if(typeof Cufon == 'function') Cufon.replace('h1, h2, h3, h4, h5, h6');
12 $(document).ready(function() {
14 sections = new Array(),
17 sidebar = $('#documenter_sidebar'),
18 nav = $('#documenter_nav'),
19 logo = $('#documenter_logo'),
20 navanchors = nav.find('a'),
22 hash = location.hash || null;
23 iDeviceNotOS4 = (navigator.userAgent.match(/iphone|ipod|ipad/i) && !navigator.userAgent.match(/OS 5/i)) || false,
24 badIE = $('html').prop('class').match(/ie(6|7|8)/)|| false;
26 //handle external links (new window)
27 $('a[href^=http]').bind('click',function(){
28 window.open($(this).attr('href'));
32 //IE 8 and lower doesn't like the smooth pagescroll
36 $('a[href^=#]').bind('click touchstart',function(){
37 hash = $(this).attr('href');
38 $.scrollTo.window().queue([]).stop();
43 //if a hash is set => go to it
45 setTimeout(function(){
52 //We need the position of each section until the full page with all images is loaded
53 win.bind('load',function(){
55 var sectionselector = 'section';
57 //Documentation has subcategories
58 if(nav.find('ol').length){
59 sectionselector = 'section, h4';
61 //saving some information
62 $(sectionselector).each(function(i,e){
66 pos: _this.offset().top
72 //iPhone, iPod and iPad don't trigger the scroll event
74 nav.find('a').bind('click',function(){
75 setTimeout(function(){
76 win.trigger('scroll');
85 sectionscount = sections.length;
87 //bind the handler to the scroll event
88 win.bind('scroll',function(event){
89 clearInterval(timeout);
90 //should occur with a delay
91 timeout = setTimeout(function(){
92 //get the position from the very top in all browsers
93 pos = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
95 //iDeviceNotOS4s don't know the fixed property so we fake it
97 sidebar.css({height:document.height});
98 logo.css({'margin-top':pos});
100 //activate Nav element at the current position
103 }).trigger('scroll');
107 //the function is called when the hash changes
108 function hashchange(){
109 goTo(location.hash, false);
112 //scroll to a section and set the hash
113 function goTo(hash,changehash){
114 win.unbind('hashchange', hashchange);
115 hash = hash.replace(/!\//,'');
116 win.stop().scrollTo(hash,duration,{
120 if(changehash !== false){
122 location.href = (l.protocol+'//'+l.host+l.pathname+'#!/'+hash.substr(1));
124 win.bind('hashchange', hashchange);
128 //activate current nav element
129 function activateNav(pos){
131 current, next, parent, isSub, hasSub;
132 win.unbind('hashchange', hashchange);
133 for(var i=sectionscount;i>0;i--){
134 if(sections[i-1].pos <= pos+offset){
135 navanchors.removeClass('current');
136 current = navanchors.eq(i-1);
137 current.addClass('current');
139 parent = current.parent().parent();
140 next = current.next();
142 hasSub = next.is('ol');
143 isSub = !parent.is('#documenter_nav');
145 nav.find('ol:visible').not(parent).slideUp('fast');
147 parent.prev().addClass('current');
148 parent.stop().slideDown('fast');
150 next.stop().slideDown('fast');
152 win.bind('hashchange', hashchange);