// Adds presentation-level enhancements

$(document).ready( function() {

    // apply treeview
    $("#nav").treeview(
        {
         	animated: "fast",
        	collapsed: true,
        	unique: true,
        	control: "#treecontrol"
        }
    );    


    // add widget to allow user to make navigation scroll with page
    $("#nav").prepend("<span id=\"main-nav-control\" class=\"nav-control\"></span>");
    $("#nav").before("<span id=\"top-nav-control\" class=\"nav-control\">Restore</span>");
    $(".nav-control").click(                
      function() { 
          var offset = $("#nav").offset().top;
          $("#nav").toggleClass("scrollable");
          if ($("#nav").hasClass("scrollable")) {
              $("#nav").css("top",offset);
          } else {
              $("#nav").css("top",0);
          } 
      }
    );

    $(".nav-control").tooltip(
        {
            bodyHandler: function() {
		if ($("#nav").hasClass("scrollable")) {
		    return "<p class=\"help\">Restore navigation</p>"; 
		} else {
		    return "<p class=\"help\">Make navigation scroll with page</p>"; 
		}
	    },
	    showURL: false
	}
    );




   

    // add event listeners to update same-page nav as page is scrolled, but only if the page hasn't
    // been broken by ad-hoc editing
    if ($("#body h2.subsection, #body h3.subsection, #body h4.subsection").size() ==
        $(".toc1>.open li").size()) {
	$(window).scroll(updateNav);
	$(window).resize(updateNav);
	updateNav();
    }



    // and do the same for the scrollable nav control
    $(window).scroll(updateNavControl);
    $(window).resize(updateNavControl);
    $("#nav li").mouseover(updateNavControl); // kludgily catches changes in the size of the nav items
    updateNavControl();

    // add event listeners to briefly flash the target heading when same-page nav is clicked
    $("#nav li.open a, .on-this-page a").click(
         function() {
           var selectedId = $(this).attr("href").substr($(this).attr("href").indexOf("#"));
           $( selectedId ).colorBlend([{fromColor:"#ffffbb", toColor:"white", cycles:1, isFade:false}]); 
         } );

    // briefly flash the target heading, if there is one, at page load 
    if (window.location.hash && window.location.hash!='#body') {
      $( window.location.hash ).colorBlend([{fromColor:"#ffffbb", toColor:"white", cycles:1, isFade:false}]);
    }

    


    // display footnotes as tooltips
    $(".footnote-ref a").tooltip(
        { 
            bodyHandler: function() { 
                return $($(this).attr('href')).html();
            }, 
            showURL: false 
        }
    );

    // display tooltip help on cam-only links & headings
    $(".acs-cam-only>a, .acs-cam-only h2.subsection").tooltip(
        { 
            bodyHandler: function() { 
                return "<p class=\"help\">This section is available to current staff and students of the University of Cambridge.</p>";
            }, 
            showURL: false 
        }
    );

});


  function updateNav() {
    var firstInView = $("#body h2.subsection, #body h3.subsection, #body h4.subsection").filter(  
          function(index) {
              // Returns true when $(this) is the relevant heading for the top-most content 
              // currently being displayed in the viewport.
              // Relies on #nav having position:fixed to get scroll offset.
   
              // fix boundary conditions
              if (index==0) return true;

              return $(this).offset().top - $(this).outerHeight() <= $("#nav").offset().top
        }
     ).size();

    var lastInView = $("#body h2.subsection, #body h3.subsection, #body h4.subsection").filter(  
          function(index) {
              // Returns true when $(this) is the relevant heading for the bottom-most content 
   
              return $(this).offset().top +  $(this).outerHeight() <= $("#nav").offset().top + getInnerHeight();
        }
     ).size();

    $("#nav li").removeClass("in-view");
    $("#nav li.open ul a").slice(firstInView -1 , lastInView).parent().addClass("in-view");


}

function updateNavControl() {
    // only display nav-control if nav too big for page, or if already turned on
    if (!$("#nav").hasClass("scrollable") && $("#nav").height() < getInnerHeight()) {
	$(".nav-control").addClass("hidden");
    } else {
	$(".nav-control").removeClass("hidden");
    }
}
 

function getInnerHeight() {
  var  myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}





