// 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 = 6000;
var fadeInEffect = null;
var fadeOutEffect = null;

//----------------------------------------------------------------------

function initSlideShow( slideContainerId, tnContainerId, tnClassName, imgPath )
{
  var bResult = false;
  if ( slideContainerId && slideContainerId != "" &&
       tnContainerId && tnContainerId != "" &&
       tnClassName && tnClassName != ""  )
  {
    if ( document.getElementById( slideContainerId ) ) {
      pathToSlides = imgPath;

      fadeInEffect = new Spry.Effect.Fade( slideContainerId, { from: "0%", to: "100%", toggle: false, duration: 2500 } );
      fadeOutEffect = new Spry.Effect.Fade( slideContainerId, { from: "100%", to: "0%", toggle: false, duration: 1000 } );
    
      var 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) );
			/*arraySlideId[arraySlideId.length] = arrayImg[i].src.substring( 3, arrayImg[i].src.search(/\.jpg/i) );*/
            
            arrayImg[i].onclick = function() {
              this.id = this.src.substring( this.src.search(/tn_/i)+3, this.src.search(/\.jpg/i) );
			  /*this.id = this.src.substring( 3, this.src.search(/\.jpg/i) );*/

              stopSlideShow();
              currSlideIndex = setPicIndex( this.id );
              // Cache next slide
              document.getElementById( "SlideCache" ).src = pathToSlides + "/" + arraySlideId[currSlideIndex] + slideFileExt;
              runFadeOutEffect();
              runFadeInEffect();
            }
          }
        }
        
        if ( arrayImg.length )
          bResult = true;
      }
    }
  }
  
  return bResult;
}

//----------------------------------------------------------------------


function initGallery( slideContainerId, tnContainerId, tnClassName, imgPath )
{

  var bResult = false;
  if ( slideContainerId && slideContainerId != "" &&
       tnContainerId && tnContainerId != "" &&
       tnClassName && tnClassName != ""  )
  {
       
	if ( document.getElementById( slideContainerId ) ) {
      pathToSlides = imgPath;

      fadeInEffect = new Spry.Effect.Fade( slideContainerId, { from: "0%", to: "100%", toggle: false, duration: 500 } );
      fadeOutEffect = new Spry.Effect.Fade( slideContainerId, { from: "100%", to: "0%", toggle: false, duration: 500 } );
   
      var obj = document.getElementById( tnContainerId );
 
      if ( obj ) {
        arraySlideId = new Array();
        var arrayImg = obj.getElementsByTagName( "a" );
    
        for ( var i=0; i<arrayImg.length; i++ ) {
          if ( arrayImg[i].className == tnClassName ) {
            arraySlideId[arraySlideId.length] = arrayImg[i].name.substring( arrayImg[i].name.search(/tn_/i)+3, arrayImg[i].name.search(/\.jpg/i) );
			
            
            arrayImg[i].onclick = function() {
              this.id = this.name.substring( this.name.search(/tn_/i)+3, this.name.search(/\.jpg/i) );

              stopSlideShow();
              currSlideIndex = setPicIndex( this.id );
              // Cache next slide
              document.getElementById( "SlideCache" ).src = pathToSlides + "/" + arraySlideId[currSlideIndex] + slideFileExt;
              runFadeOutEffect();
              runFadeInEffect();
            }
          }
        }
        
        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 runFadeOutEffect()
{
  if ( fadeOutEffect && !fadeOutEffect.isRunning && fadeInEffect && !fadeInEffect.isRunning )
    fadeOutEffect.start();
}

//----------------------------------------------------------------------

function runFadeInEffect()
{
  if ( fadeOutEffect && fadeOutEffect.isRunning )
    window.setTimeout("runFadeInEffect()", 330);
  else {
    showSlideImage();

    if ( fadeInEffect )
      fadeInEffect.start();
  }
}

//----------------------------------------------------------------------

function showSlideImage()
{
  document.getElementById( slideId ).src = pathToSlides + "/" + arraySlideId[currSlideIndex] + slideFileExt;  
  var DescText = arraySlideId[currSlideIndex].replace(/_/g, " ");
  document.getElementById( 'SlideDesc' ).innerHTML = DescText.substring(3, DescText.length );
}

//----------------------------------------------------------------------

function startSlideShow()
{
  if ( !isSlideShowRunning )
    isSlideShowRunning = true;

  slideTimer = window.setTimeout("autoNav()", slideElpased);

  // Cache next slide
  if ( currSlideIndex+1 < arraySlideId.length )
    document.getElementById( "SlideCache" ).src = pathToSlides + "/" + arraySlideId[currSlideIndex+1] + slideFileExt;
}

//----------------------------------------------------------------------

function stopSlideShow()
{
  if ( isSlideShowRunning ) {
    window.clearTimeout(slideTimer);
    isSlideShowRunning = false;
  }
}

//----------------------------------------------------------------------

function autoNav()
{
  currSlideIndex++;

  if ( currSlideIndex == arraySlideId.length )
    stopSlideShow();
  else {
    runFadeOutEffect();
    //showSlideImage();
    runFadeInEffect();

    startSlideShow(); //next slide
  }
}
