////
//// support code for HAI site
////

//
// required:
//   jquery.js (version 1.2.6+) (see jquery.com)
//   jquery.ifixpng.js
//   jquery.preloadimages.js
//

$(document).ready(function(){

  ////
  //// fix png images for IE6
  ////
  $.ifixpng('/img/spacer.gif');
  $('img[@src$=.png]').ifixpng();


  ////
  //// primary navigation display, highlighting, image preloads, and mouseovers
  ////

  // get body classname and split into section and page
  // body must have class in form "section_page"
  //
  var bodyclasslist = $("body").attr("class").split(/\s+/);
  var sectionname = (typeof bodyclasslist[0] != "undefined" ? bodyclasslist[0] : "none");
  var pagename = (typeof bodyclasslist[1] != "undefined" ? bodyclasslist[1] : "home");

  // hide all subnavigation in non-current section
  //
  $("#divprimarynav ul:not(."+sectionname+")").hide();

  // highlight current link in subnavigation
  //
  $("#divprimarynav ul."+sectionname+" li."+pagename).addClass("current");

  // set "last" class in subnavigation
  //
  $("#divprimarynav ul li:last").addClass("last");

  // switch in the _on link for the current section (only for interior pages)
  //
  if ($("body").is(":not(.home)") && $("body").is(":not(.error)")) {
    var newsrc = $("#divprimarynav p."+sectionname+" a img").attr("src").replace(/_off/, '_on');
    $("#divprimarynav p."+sectionname+" a img").attr("src", newsrc);
  }


  // process all images within divprimarynav - apply mouseover and do preload
  $("#divprimarynav p a img")
    .each( function(i) {
      // set up two versions of image
      var src1 = this.src;
      var src2 = src1.replace(/_off/, '_ro');
      // preload mouseover version
      $.preloadImages.add(src2);
      // save two values in the image itself
      this.src1 = src1;
      this.src2 = src2;
      // respond to mouseover/mouseout
      $(this).hover(
        function () { // mouseover function
          this.src = this.src2;
        },
        function () { // mouseout function
          this.src = this.src1;
        }
      );
    });
    // start preload
    $.preloadImages.start();


   ////
   //// attach click event to "back to top" links
   ////
   $("p.backtotop a")
     .click( function() {
       window.scrollTo(0,0);
       return false;
     });

});
