////
//// support code for MIT Faculty Work Life site
////

//
// required:
//   jquery.js (version 1.2.6+) (see jquery.com)
//   jquery.ifixpng-3.1.2.js
//

$(document).ready(function(){

  ////
  //// fix png images for IE6
  ////
  $.ifixpng('/facultyworklife/img/spacer.gif');
  $('img[src$=.png]').ifixpng();

  ////
  //// random image on home page
  ////
  $('body.home #divband1')
    .each( function() {  // (there is only one of these...)
      // list of photo classes for home page
      homephotos = new Array();
      homephotos[homephotos.length] = "photo01";
      homephotos[homephotos.length] = "photo02";
      homephotos[homephotos.length] = "photo03";
      homephotos[homephotos.length] = "photo04";
      homephotos[homephotos.length] = "photo05";
      homephotos[homephotos.length] = "photo06";
      homephotos[homephotos.length] = "photo07";
      // number of candidate photos
      var nhomephotos = homephotos.length;
      // pick one
      ihomephoto = Math.floor(Math.random()*nhomephotos);
      // insert into page
      $(this).addClass(homephotos[ihomephoto]);
    });

  ////
  //// random tip on home page
  ////
  $('body.home #divtips')
    .each( function() {  // (there is only one of these...)
      // show heading
      $(this).children('h2').addClass('visible');
      // count number of paragraphs
      var np = $(this).children('p').size();
      // show one tip
      if (np > 0) {
        var ip = Math.floor(Math.random()*np);
        $(this).children('p:eq('+ip+')').addClass('visible');
      }
    });

  ////
  //// mark active items in navigation
  ////
  // get body classes in array
  //
  var bodyclasses = $('body').attr('class');
  bodyclasses = bodyclasses.split(/\s+/);
  //
  // test each navigation item for matches against the body classes
  //
  $('#divprimarynav li, #divtoolnav li').each( function(i) {
    for (var i in bodyclasses) {
      if ($(this).hasClass(bodyclasses[i])) {
        $(this).addClass('active-trail');
        break;
      }
    }
  });

  ////
  //// on home page, remove link to home page from site name
  ////
  var imghtml = $('body.home #divsitetitle a').html();
  if (imghtml) {
    $('body.home #divsitetitle').html(imghtml);
  }

  ////
  //// set class for safari so we can make minor adjustment to nav
  ////
  if (jQuery.browser.safari) {
    $('body').addClass('safari');
  }

  ////
  //// clear search on initial activation
  ////
  $('#q').focus( function() {
    if (!this.searchstarted) {
      this.value = "";
      this.searchstarted = true;
    }
  });

});
