/*****************************************************************************************
* vp_slideshow.js  v2.1
*
* Copyright 2003 by Troy Fuqua, VisiblePulse.com Utilities
* Last update: January 18, 2003
*
* Creates image slide show with IE5.5+ transition filters and captions.
* Multiple slides can be created with unique settings for each slide.
*
* See instructions at http://www.visiblepulse.com/
****************************************************************************************/

// Slide Show handler
var SlideShowList = new Array();

function vpSlideShow(imgID, captionHeight, slide1Delay) {
  // Define properties.
  // slide1Delay: 0=normal delay, 1=immediate display
  
  this.url = new Array();
  this.tID = new Array();
  this.tTime = new Array();
  this.cDly = new Array();
  this.caption = new Array();
  this.cbgcolor = new Array();
  this.dwell = new Array();
  this.img = new Array();
  
  this.running = 0;
  this.neverstarted = 1;
  this.slidesloaded = 0;
  this.timer1obj = null;
  this.timer2obj = null;
  this.slide_count = 0;
  this.slide_ptr = -1;
  this.imageID = imgID;
  this.doCaptions = Math.abs(captionHeight);
  this.slide1Delay = slide1Delay;
  
  // Define methods.

  this.addSlide = slideshowAddSlide;
  this.loadSlides = slideshowLoadSlides;

  this.autoLoadStart = slideshowLoadStart;  
  this.stop = slideshowStop;
  this.start = slideshowStart;
  this.playChange = slideshowPlayChange;
  this.stepNextSlide = slideshowStepNext;
  this.stepPrevSlide = slideshowStepPrev;
  this.setSlide = slideshowSetSlide;
  
  // Add to global list
  this.ordinal = SlideShowList.length;
  SlideShowList.push(this);
  
  if (this.doCaptions) {
    if (document.all||document.getElementById) document.write('<div id="'+imgID+'vpdiv"></div>');
    else if (document.layers) document.write('<ilayer id="'+imgID+'IL" height="'+captionHeight+'"><layer id="'+imgID+'L" width="100%"></layer></ilayer>');
  }
}

function slideshowAddSlide(strURL, transID, transTime, captionDelay, strCaption, iDwell) {
// objSlide1.addSlide("image url",transition#, transition_time, "Caption", slide_display_time);
  this.url.push(strURL);
  this.tID.push(transID);
  this.tTime.push(transTime/1000);
  this.cDly.push(captionDelay);
  this.caption.push(strCaption);
  this.dwell.push(iDwell);
  this.slide_count++;
  this.slide_ptr++;
}

function slideshowLoadSlides(runToo) {
  if (this.slide_count==0) {alert("vpSlideShow Error: Can't LoadSlides, no slides have been added. (addSlide)"); return;}
  if (!document.images) return;
  for (var c=0; c<this.slide_count; c++) {
   this.img[c]=new Image();
   this.img[c].src=this.url[c];
  }
  this.slidesloaded=-1;
  if (runToo) {
    if (!this.running) {
      this.running=-1;
      this.timer1obj = setTimeout("SlideShowList["+this.ordinal+"].playChange()",(this.neverstarted?(this.slide1Delay>10?this.slide1Delay:10):(this.dwell[this.slide_ptr])));
      this.neverstarted = 0;
    }
  }
}

function slideshowLoadStart(startDelay) {
  if (!document.images) return;
  if (this.slide_count==0) {alert("vpSlideShow Error: Can't LoadSlides, no slides have been added. (addSlide)"); return;}
  if (!this.running) setTimeout("SlideShowList["+this.ordinal+"].loadSlides(1)",startDelay);
}


function slideshowStart() {
  if (this.slidesloaded==0) {alert("vpSlideShow Error: Can't start, no slides have been loaded. (loadSlides)"); return;}
  if (this.timer1obj) {clearTimeout(this.timer1obj); this.timer1obj = null; }
  if (this.timer2obj) {clearTimeout(this.timer2obj); this.timer2obj = null; }
  if (!this.running) {
    this.running=-1;
    this.timer1obj = setTimeout("SlideShowList["+this.ordinal+"].playChange()",(this.neverstarted?(this.slide1Delay>10?this.slide1Delay:10):(this.dwell[this.slide_ptr])));
    this.neverstarted = 0;
  }
}

function slideshowStop() {
  this.running=0;
  if (this.timer1obj) {clearTimeout(this.timer1obj); this.timer1obj = null; }
  if (this.timer2obj) {clearTimeout(this.timer2obj); this.timer2obj = null; }
}

function slideshowSetSlide(goto_slide,no_delay) {
  if (!document.images) return;
  if (this.slidesloaded==0) return;
  if (goto_slide>=0 && goto_slide<this.slide_count) {
    this.slide_ptr=goto_slide;
    slideshowChangeSlide(this,this.slide_ptr,no_delay);
  }
}

function slideshowStepNext(no_delay) {
  if (!document.images) return;
  if (this.slidesloaded==0) return;
  if (++this.slide_ptr>=this.slide_count) this.slide_ptr=0;
  slideshowChangeSlide(this,this.slide_ptr,no_delay);
  if (this.running) this.timer1obj = setTimeout("SlideShowList["+this.ordinal+"].playChange()",this.dwell[this.slide_ptr]);
}

function slideshowStepPrev(no_delay) {
  if (!document.images) return;
  if (this.slidesloaded==0) return;
  if (--this.slide_ptr<0) this.slide_ptr=(this.slide_count-1);
  slideshowChangeSlide(this,this.slide_ptr,no_delay);
  if (this.running) this.timer1obj = setTimeout("SlideShowList["+this.ordinal+"].playChange()",this.dwell[this.slide_ptr]);
}

function slideshowPlayChange() {
  if (!this.running) return;
  if (!document.images) return;
  if (++this.slide_ptr>=this.slide_count) this.slide_ptr=0;
  slideshowChangeSlide(this,this.slide_ptr,0)
  this.timer1obj = setTimeout("SlideShowList["+this.ordinal+"].playChange()",this.dwell[this.slide_ptr]);
}

function slideshowChangeSlide(pthis,slide_number,no_delay) {
  if (!document.images) return;
  if (pthis.timer1obj) {clearTimeout(pthis.timer1obj); pthis.timer1obj = null; }
  if (pthis.timer2obj) {clearTimeout(pthis.timer2obj); pthis.timer2obj = null; }

//Erase current caption if netscrape
  if (document.layers&&pthis.doCaptions&&!no_delay) vpSetCaption(pthis.imageID,'');

// Show next slide
  theImage=eval('document.images.'+pthis.imageID);
  if (document.all&&!no_delay&&pthis.tID[slide_number]&&pthis.slide1Delay>=0) {
    if (pthis.tID[slide_number]==1) {
      theImage.style.filter="blendTrans(duration="+pthis.tTime[slide_number]+")";
      theImage.filters.blendTrans.apply();
    } else {
      theImage.style.filter="revealTrans(duration="+pthis.tTime[slide_number]+",transition="+(pthis.tID[slide_number]-2)+")";
      theImage.filters.revealTrans.apply();
    }
  }

  theImage.src=pthis.img[slide_number].src;

  if (document.all&&!no_delay&&pthis.tID[slide_number]&&pthis.slide1Delay>=0) {
    if (pthis.tID[slide_number]==1) theImage.filters.blendTrans.play();
    else theImage.filters.revealTrans.play();
  }
  
// Set next caption
  if (pthis.doCaptions) {
    if (no_delay) { vpSetCaption(pthis.imageID,pthis.caption[slide_number]); }
    else {
  
    if (document.all&&pthis.cDly[slide_number]&&!pthis.slide1Delay) {
      if (pthis.cDly[slide_number]>0) pthis.timer2obj = setTimeout("vpSetCaption('"+pthis.imageID+"','')",pthis.cDly[slide_number]/2);
      pthis.timer2obj = setTimeout("vpSetCaption('"+pthis.imageID+"','"+pthis.caption[slide_number]+"')",Math.abs(pthis.cDly[slide_number]));
    } else {
      pthis.timer2obj = setTimeout("vpSetCaption('"+pthis.imageID+"','"+pthis.caption[slide_number]+"')",(pthis.slide1Delay?10:pthis.cDly[slide_number]));
    } }
  }
}


function vpSetCaption(ID,caption) {
  if (document.getElementById) { 
    cobj=document.getElementById(ID+'vpdiv');
    cobj.innerHTML = caption;
    }
  else if (document.all) {
    cobj=eval('document.all'+ID+'vpdiv');
    cobj.innerHTML = caption;
    }
  else if (document.layers) {
    cobj=eval('document.'+ID+'IL.document.'+ID+'L');
    cobj.document.write('<p align="center">');
    cobj.document.write('<table border="0" cellspacing="0" cellpadding="0" height="50" width="360"><tr><td align="center">');
    cobj.document.write(caption);
    cobj.document.write('</td></tr></table>');
    cobj.document.close();
    }
}