// Must set!
var arraySlideId = null;
var pathToSlides = null;

// Override in CFM if necessary...
var slideFileExt = ".jpg";
var slideId = "Slide";
var currSlideIndex = 0;
var isSlideShowRunning = false;
var slideTimer = null;
var slideElpased = 8000;
var slideFadeInEffect = null;
var slideFadeOutEffect = null;
var captionFadeInEffect = null;

//----------------------------------------------------------------------

function initSlideShow( slideContainerId, tnContainerId, tnClassName, imgPath )
{
  var bResult = false;
  var obj = null;

  if ( slideContainerId && slideContainerId != "" && tnContainerId && tnContainerId != "" && tnClassName && tnClassName != ""  ) {
    if ( document.getElementById(slideContainerId) ) {
      pathToSlides = imgPath;

      slideFadeOutEffect = new Spry.Effect.Fade( slideContainerId, { finish: showSlideImage, from: "100%", to: "0%", toggle: false, duration: 600 } );
      slideFadeInEffect = new Spry.Effect.Fade( slideContainerId, { from: "0%", to: "100%", toggle: false, duration: 1800 } );
      
      obj = document.getElementById("SlideCaptionContainer");

      if ( obj )
        captionFadeInEffect = new Spry.Effect.Fade( obj, { from: "0%", to: "100%", toggle: false, duration: 500 } );

      obj = document.getElementById(tnContainerId);

      if ( obj ) {
        arraySlideId = new Array();
        var arrayImg = obj.getElementsByTagName("img");
    
        for ( var i=0; i<arrayImg.length; i++ ) {
          if ( arrayImg[i].className == tnClassName ) {
            arraySlideId[arraySlideId.length] = arrayImg[i].src.substring( arrayImg[i].src.search(/tn_/i)+3, arrayImg[i].src.search(/\.jpg/i) );

            arrayImg[i].onclick = function() {
              if ( isSlideShowRunning )
							  stopSlideShow();
							
              this.id = this.src.substring( this.src.search(/tn_/i)+3, this.src.search(/\.jpg/i) );
              currSlideIndex = setPicIndex( this.id );
							showSlideImage();
            }
          }
        }
        
        if ( arrayImg.length )
          bResult = true;
      }
    }
  }
  
  return bResult;
}

//----------------------------------------------------------------------

function setPicIndex( id )
{
  for ( var i=0; i<arraySlideId.length; i++ ) {
    if ( arraySlideId[i] == id )
      break;
  }
  
  if ( i == arraySlideId.length )
    i = 0;
  
  return i;
}

//----------------------------------------------------------------------

function showSlideImage()
{
	document.getElementById(slideId).src = pathToSlides + "/" + arraySlideId[currSlideIndex] + slideFileExt;
  document.getElementById("SlideCaptionContainer").innerHTML = document.getElementById( "Caption"+currSlideIndex ).innerHTML;

  if ( captionFadeInEffect && isSlideShowRunning )
    captionFadeInEffect.start();
}

//----------------------------------------------------------------------

function stopSlideShow()
{
  if ( isSlideShowRunning ) {
    window.clearTimeout( slideTimer );
    isSlideShowRunning = false;
    
		document.getElementById(slideId).onload = function() {
      document.getElementById("SlideCaptionContainer").innerHTML = document.getElementById( "Caption"+currSlideIndex ).innerHTML;
    }
  }
}

//----------------------------------------------------------------------

function startSlideShow()
{
  if ( !isSlideShowRunning ) {
    isSlideShowRunning = true;
		document.getElementById(slideId).onload = runSlideFadeInEffect;
	}

  slideTimer = window.setTimeout( "autoNav()", slideElpased );
  cacheSlides();
}

//----------------------------------------------------------------------

function autoNav()
{
  currSlideIndex++;

  if ( currSlideIndex == arraySlideId.length )
    stopSlideShow();
  else {
    startSlideShow(); //next slide
    slideFadeOutEffect.start();
  }
}

//----------------------------------------------------------------------

function runSlideFadeInEffect()
{
  if ( slideFadeOutEffect.isRunning || slideFadeInEffect.isRunning )
    window.setTimeout( "runSlideFadeInEffect()", 200 );
  else
    slideFadeInEffect.start();
}

//----------------------------------------------------------------------

function cacheSlides()
{
  if ( currSlideIndex+1 < arraySlideId.length )
    document.getElementById("SlideCache").src = pathToSlides + "/" + arraySlideId[currSlideIndex+1] + slideFileExt;
  else
    window.clearTimeout( slideTimer );
}

//----------------------------------------------------------------------
