////
//// support code for MIT NSE site
////

//
// required:
//   jquery.js (version 1.6.2+) (see jquery.com)
//   jquery.equalHeight.js (to make equal height columns)
//   jquery.colorbox.js (for lightbox-style slideshows)
//   jquery.cycle.js (for in-page slideshows, including home page)
//

$(document).ready(function(){

  ////
  //// wrap object tags in div (do this before equal height columns)
  ////
  $("object").each( function() {
    var ht1 = $(this).height();              // usually returns 0

    var ht2 = $(this).attr('height');        // works if height is set
    ht2 = (typeof ht2 == 'string' ? parseInt(ht2) : 0);

    var ht = (ht1 > ht2 ? ht1 : ht2);        // height to use for wrapper

    var divhtml = '<div style="height:'+ht+'px;" class="divobjectwrapper"></div>';
    $(this).wrap(divhtml);
  });


  ////
  //// set links to open hidden content (do this before equal height columns)
  ////
  $("div.openable").each( function() {
    $(this).children().not(".opener").hide();
    $(this).children(".opener").wrapInner("<a href='#more'></a>").find("a").click( function() {
      $(this).parents(".openable").children(".opener").hide();
      $(this).parents(".openable").children().not(".opener").show();
      setEqualHeightColumns(); // readjust columns
      return(false);
    });
  });


  ////
  //// set up equal height columns
  //// note that this could also be done with some rather complicated CSS and HTML; but since it is not critical
  //// that columns be equal height, it's much cleaner and less complicated to do this with javascript
  ////
  function setEqualHeightColumns() {
    $(".equalheights").each(function() {
      // for each container of equal height boxes, adjust heights of all tagged children
      $(this).children(".equalheight").equalHeight();
    });
  }
  // do it once at start
  setEqualHeightColumns();


  ////
  //// set up primary and secondary navigation for current page
  ////
  // get list of body classes
  var bodyclasses = $("body").attr("class").split(/\s+/g);

  // iterate through primary nav items
  $("#divprimarynav li").each(function(i) {
    // check nav classes against body classes to find current section
    var current = false;
    for (j = 0; j < bodyclasses.length; j++) {
      if ($(this).hasClass(bodyclasses[j])) {
        $(this).find("a:first").addClass("active");
        break;
      }
    }
  });

  // iterate through secondary nav items
  $("ul.secondarynav li").each(function(i) {
    // check nav classes against body classes to find current section
    var current = false;
    for (j = 0; j < bodyclasses.length; j++) {
      if ($(this).hasClass(bodyclasses[j])) {
        $(this).find("a:first").addClass("active");
        break;
      }
    }
  });


  ////
  //// back to top links (not used)
  ////
  //$(".backtotop a").click( function() { window.scrollTo(0,0); return false; });

});


$(document).ready(function(){

  ////
  //// set up ColorBox galleries
  ////
  //// This is configured to work with a single link that opens the gallery.
  //// It works with multiple galleries on one page. Here's an example of how to set up the HTML:
  ////    <p id="gallery1" class="cbgallery"><a href="slide1.jpg">slide 1</a> - <a href="slide2.jpg">slide 2</a></p>
  ////    <p><a href="#gallery1" class="cbopener">Start Show</a></p>
  //// The
  ////
  if (typeof $().colorbox != 'undefined') {

    // associate links in each gallery with one another and with colorbox
    // note that if there are multiple galleries, each one must have a different colorbox 'rel' parameter
    $(".cbgallery").each( function(i) {
      var settings = {
          transition: "fade",
          rel: "group"+i,
          slideshow: true,
          slideshowSpeed: 4000,
          slideshowStop: 'pause',
          slideshowStart: 'continue'
      };
      $(this).find("a").colorbox(settings);
    });
    // respond to clicks on gallery opening links
    $("a.cbopener").click(function(e) {
      e.preventDefault();
      var galid = $(this).attr("href");
      $(galid).find("a").eq(0).click();
    });
  }
  else {
    //alert('no colorbox');
  }

});


$(document).ready(function(){

  ////
  //// set up random in-page slideshows throughout the site
  ////
  $("div.slideshow").each( function(i) {

    // get number of slides; return if less than two
    var nslides = $(this).children().length;
    if (nslides < 2) {
      return;
    }

    // pick a random starting slide, if slideshow has the 'randomstart' class
    var nslide = 0;
    if ($(this).hasClass('randomstart')) {
      nslide = Math.floor(Math.random()*nslides);
    }

    // check if slides should advance - if 'noshow' class is set, then we show
    // first slide and never advance after that
    var noshow = ($(this).hasClass('noshow') ? true : false);

    // set up slideshow
    $(this).cycle({
      fx: 'fade',
      speed: 1000,
      timeout: (noshow ? 0 : 4000),
      pause: 1,
      startingSlide: nslide
    });

  });

});


//$(document).ready(function(){
  ////
  //// fix footer position (so it doesn't go off the left side of the screen)
  //// (this was used when footer was fixed position at bottom center of window)
  ////
  //function fixFooterPosition() {
  //  if ($(window).width() < $("#divfooter").width()) {
  //    $("#divfooter").addClass('smallwindow');
  //  }
  //  else {
  //    $("#divfooter").removeClass('smallwindow');
  //  }
  //};
  // note - small delay between adjustments
  //var resizeTimer;
  //$(window).resize(function() {
  //  clearTimeout(resizeTimer);
  //  resizeTimer = setTimeout(fixFooterPosition, 50);
  //});
  // do it once at start
  //fixFooterPosition();
//});

