// slideshow.js 
// click thumb to see enlargement slideshow
// sample code:
//    <p><img id="foo" src="img/img_about1.jpg" width="392" height="262" alt="" /></p>
//    <ul>
//       <li class="current">
//          <a href="img/img1.jpg" class="slideshow-thumb-foo"><img src="img/img1_thumb.jpg" alt="" /></a>
//       </li>
//       <li>
//          <a href="img/img2.jpg" class="slideshow-thumb-foo"><img src="img/img2_thumb.jpg" alt="" /></a>
//       </li>
//    </ul>
// code by CN

function isDefined(property) {
  return (typeof property != 'undefined');
}

/**********************************************************************
   Slideshow initialization
 *********************************************************************/

var slideshows = [];

function slideshowInit() {
   if (isDefined(document.getElementsByTagName)) {
      var links = document.getElementsByTagName('a');
      for (var linkIndex = links.length - 1; linkIndex >= 0; linkIndex--) {
         var linkElement = links[linkIndex];
         
         if (linkElement.className.indexOf('slideshow-thumb-') > -1) {
            
            // get id of large image from classname  
            var className = linkElement.className;
            var startOfSlideshowClass = className.indexOf('slideshow-thumb-') + 16;
            var endOfSlideshowClass = className.indexOf(' ', startOfSlideshowClass);
            var largeImageId = (endOfSlideshowClass > -1) ? className.substring(startOfSlideshowClass, endOfSlideshowClass) : className.substring(startOfSlideshowClass);

            // if this is the first member for this slideshow, create entry in slideshow array
            if (!slideshows[largeImageId]) {
               slideshows[largeImageId] = {
                  largeImage : document.getElementById(largeImageId),
                  thumbs     : []
               };
            }
            
            // store a reference to this member in the slideshow entry
            slideshows[largeImageId].thumbs.push(linkElement);

            // create reference to the slideshow entry in member
            linkElement.slideshow = slideshows[largeImageId];
            
            // preload large image
            (new (Image)).src = linkElement.href;
            
            linkElement.onclick = function() {
               
               // show large image
               this.slideshow.largeImage.src = this.href;
               
               // lightbox stuff: rel is really large image url; title is caption
               this.slideshow.largeImage.parentNode.href = this.rel;
               this.slideshow.largeImage.parentNode.title = this.title;
               
               // set all thumbs as not active
               for (var itemIndex = this.slideshow.thumbs.length - 1; itemIndex >= 0; itemIndex--) {
                  var listItem = this.slideshow.thumbs[itemIndex].parentNode;
                  if (listItem.className) {
                     listItem.className = '';
                  }
               }
               
               // set this thumb as active
               this.parentNode.className = 'current';
               
               return false;
            };
         }
      }
   }
}


// call slideshowInit when document finishes loading
if (isDefined(window.addEventListener)) {
   window.addEventListener('load', slideshowInit, false);
}
else if (isDefined(window.attachEvent)) {
   window.attachEvent('onload', slideshowInit);
}

