////
//// support code for MIT Music Program site
////

//
// required:
//   jquery.js (version 1.2.6+) (see jquery.com)
//   jquery.ifixpng.js
//

$(document).ready(function(){

  ////
  //// preload several images
  ////
  $.preloadImg.add( [
                      '/music/img/go_on.gif',
                      '/music/img/backtotop_on.gif',
                      '/music/img/calendar_on.png',
                      '/music/img/bkg_blue.png',
                      '/music/img/bkg_grayblue.png',
                      '/music/img/bkg_green.png',
                      '/music/img/bkg_orange.png',
                      '/music/img/bkg_pink.png',
                      '/music/img/bkg_yellow.png'
                    ]);
  $.preloadImg.start();


  ////
  //// fix png images for IE6
  ////
  $.ifixpng('/music/img/spacer.gif');
  $('img[src$=.png]').ifixpng();

  ////
  //// set class for safari so we can make minor adjustments if needed
  ////
  //if (jQuery.browser.safari) {
  //    $('body').addClass('safari');
  //}

  ////
  //// swap and restore Go button
  ////
  $('#gosearch').mouseover( function() {
    this.src = this.src.replace(/_off/, '_on');
  });
  $('#gosearch').mouseout( function() {
    this.src = this.src.replace(/_on/, '_off');
  });
  // preload the mouseover image

  ////
  //// swap and restore Back to Top buttons
  ////
  $('p.backtotop a img').mouseover( function() {
    this.src = this.src.replace(/_off/, '_on');
  });
  $('p.backtotop a img').mouseout( function() {
    this.src = this.src.replace(/_on/, '_off');
  });
  //
  // execute back to top with scrollTo
  //
  $('p.backtotop a').click( function() {
    window.scrollTo(0,0);
    return false;
  });

  ////
  //// 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
  //
  $('#divnavigation li').each( function(i) {
    for (var i in bodyclasses) {
      if ($(this).hasClass(bodyclasses[i])) {
        $(this).addClass('active-trail');
        break;
      }
    }
  });

  ////
  //// set up mouseovers for footer links
  ////
  $('#divtoollinks a[title]').each( function() {
    // off text is text content of link
    this.offtext = $(this).text();
    // mouseover text is content of title attribute
    this.rotext = this.title;
    // blank out title
    this.title = '';
    // set up mouseover/mouseout
    $(this).mouseover( function() {
      $(this).text(this.rotext);
    });
    $(this).mouseout( function() {
      $(this).text(this.offtext);
    });
  });

  ////
  //// convert .mp3 links to players
  ////
  // keep count
  var nmp3 = 0;
  // iterate through mp3 links
  $("a[href$='.mp3']")
    .each( function() {
      // increment counter
      ++nmp3;
      // save ref to mp3 file
      var mp3href = this.href;
      // set ID on parent
      var mp3id = 'mp3player'+nmp3
      $(this).parent().attr('id',mp3id);
      //
      // set up Flash movie
      //
      var params = {
                     play:    "true",
                     loop:    "false",
                     menu:    "false",
                     swLiveConnect: "false",
                     wmode:   "transparent",
                     quality: "high",
                     align:   ""
                   };
      var attributes = {
                     id:      mp3id
                   };
      var flashvars = {
                     mp3url:       mp3href,
                     volume:       '80',
                     //autobuffer:   (nmp3 == 1 ? 'true' : 'false'),  // buffer first one on page
                     autobuffer:   'false',
                     preloadpct:   'auto',
                     autoplay:     'false',
                     autoloop:     'false',
                     //debug:    (location.search == "?debug" ? "1" : "0"),
                     xyzzy:    0
                   };
      //var playerwidth = 303;
      //var playerheight = 28;
      var playerwidth = 348;
      var playerheight = 32;
      if (location.search != "?noflash") {
        swfobject.embedSWF("/music/scripts/rjwmp3player4/singlemp3player4-mitmusic.swf", mp3id, playerwidth, playerheight, "8.0.0", false, flashvars, params, attributes);
      }

    });
});
