////
//// support code for Harpswell Foundation home page
////

//
// required:
//   jquery.js (version 1.2.6+) (see jquery.com)
//   jquery.preloadimages.js
//


//
// constants
//

// time for fades
var fadeTime = 750;
var quickfadeTime = 250;
// time between slide changes
var slideTime = 5000;


//
// variables
//

// number of photos
var nslides = 0;
// index of currently displayed image
var islide = -1;
// timer for slide changes
var timerid = null;

//
// immediately show slide
//  i = slide to show
//  q = true for quick slide change (response to user click)
//
var showSlide = function(i, q) {
  // cancel any pending slide changes
  if (timerid != null) {
    clearTimeout (timerid);
    timerid = null;
  }
  //
  ft = (q ? quickfadeTime : fadeTime);
  // fade out old side
  if (islide >= 0) {
    $('#thumb'+islide).fadeIn(ft);
    $('#photo'+islide).fadeOut(ft);
  }
  // fade in new slide
  islide = i;
  if (islide >= 0) {
    $('#thumb'+islide).fadeOut(ft);
    $('#photo'+islide).fadeIn(ft);
  }
  // start timer for next slide change
  timerid = setTimeout("changeSlide()", fadeTime+slideTime);
}

//
// timed slide change
//
var changeSlide = function() {
  showSlide((islide+1)%nslides, false);
  //timerid = setTimeout("changeSlide()", fadeTime+slideTime);
}


////
//// set everything up
////
$(document).ready(function(){

  //
  // remove initial large photo
  //
  $("#divphoto").empty();

  //
  // walk through thumbnails
  //
  $("#divthumbband img")
    .addClass('clickable')  // make it look clickable
    .each(function(i) {
      //
      // save index in thumbnail img object
      //
      this.ithumb = i;
      //
      // set thumbnail img id
      //
      $(this).attr('id','thumb'+i);
      //
      // add large photo corresponding to the thumbnail
      //
      var photosrc = this.src.replace(/thumb/, 'full');
      $("<img src='"+photosrc+"' width='583p' height='437' alt='' id='photo"+i+"' class='slide' />")
        .appendTo("#divphoto");
      //
      // add click function to thumbnail
      //
      $(this)
        .addClass('clickable')
        .click(function() {
          showSlide(this.ithumb, true);
        });
      //
      // save count of slides
      //
      nslides = i+1;
    });

    //
    // randomly select an image for initial display
    //
    showSlide(Math.floor(Math.random()*nslides), true);

});

