/**
* JoomGallery Slideshow module
* Copyright (C) 2009 Erftralle
* file: mod_joomslideshow/js/fadeslide.js
* version: 1.5
* contact:
* license http://www.gnu.org/copyleft/gpl.html GNU/GPL or have a look at mod_jgtreeview/LICENSE.TXT
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program;
* if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
*/
function FadeSlide( Name, Interval, StartDelay, ShowImgTitle, EnableLink, IdImg, IdImgLink, IdTitle, IdTitleLink )
{
  this.Name         = Name;

  this.aImg         = new Array();
  this.aURL         = new Array();
  this.aImgTitle    = new Array();

  this.Interval     = Interval;
  this.StartDelay   = StartDelay;
  this.TimeToFade   = 1000;
  this.FadeInterval = 33;
  this.EnableLink   = EnableLink;
  this.ShowImgTitle = ShowImgTitle;
  this.n            = -1; 
  this.ImgCount     = 0;

  this.IdImg        = IdImg;
  this.IdImgLink    = IdImgLink;
  this.IdTitle      = IdTitle;
  this.IdTitleLink  = IdTitleLink;
  this.TimerId      = 0;
}

FadeSlide.prototype.AddImage = function( ImgURL ) {
  this.aImg[ this.aImg.length ] = ImgURL;        
  this.ImgCount = this.aImg.length;
}  

FadeSlide.prototype.AddImageLinkURL = function( ImgLinkURL ) {
  this.aURL[ this.aURL.length ] = ImgLinkURL;
} 
    
FadeSlide.prototype.AddImageTitle = function( ImgTitle ) {
  this.aImgTitle[ this.aImgTitle.length ] = ImgTitle; 
} 

FadeSlide.prototype.ChangeImage = function() {

  this.n = this.n + 1;
  if (this.n < this.ImgCount) {

    var oElement = document.getElementById( this.IdImg );
    oElement.FadeState = -2;
    oElement.FadeTimeLeft = this.TimeToFade;
    oElement.style.opacity = '0';
    oElement.style.filter = 'alpha(opacity = 0)';
    oElement.src = this.aImg[ this.n ];
 
    if( this.EnableLink == 1 ) {
      document.getElementById( this.IdImgLink ).href = this.aURL[ this.n ];
    }
    if( this.ShowImgTitle == 1 ) {
      if( this.EnableLink == 1 ) {
        document.getElementById( this.IdTitleLink ).href = this.aURL[ this.n ];
        document.getElementById( this.IdTitleLink ).innerHTML = this.aImgTitle[ this.n ];
      }
      else {
        document.getElementById( this.IdTitle ).innerHTML = this.aImgTitle[ this.n ];
      }
    }
    // fade in
    this.Fade( this.IdImg ); 
  }
  else {
    this.n = -1;
    this.ChangeImage(); 
  }
}

FadeSlide.prototype.Fade = function( ElementId )
{
  var oElement = document.getElementById( ElementId );
 
  if( oElement.FadeState == 1 || oElement.FadeState == -1 ) {
    // fading is in progress
  }
  else {
    oElement.FadeState = oElement.FadeState == -2 ? 1 : -1;
    oElement.FadeTimeLeft = this.TimeToFade;
  } 
  
  this.TimerId = setTimeout( this.Name + ".AnimateFade(" + new Date().getTime() + ",'" + ElementId + "')" , this.FadeInterval );
}

FadeSlide.prototype.AnimateFade = function( LastTick, ElementId )
{  
  var CurrentTick  = new Date().getTime();
  var ElapsedTicks = CurrentTick - LastTick;
 
  var oElement = document.getElementById( ElementId );
 
  if( oElement.FadeTimeLeft <= ElapsedTicks ) {
    
    // finishing fading 
    oElement.style.opacity = oElement.FadeState == 1 ? '1' : '0';
    oElement.style.filter = 'alpha(opacity = ' + ( oElement.FadeState == 1 ? '100' : '0') + ')';
    oElement.FadeState = oElement.FadeState == 1 ? 2 : -2;
  
    if( oElement.FadeState == -2 ) { 
      // start next Image 
      this.TimerId = setTimeout( this.Name + ".ChangeImage()", this.FadeInterval );
    }
    else {
      // fade out
      this.TimerId = setTimeout( this.Name + ".Fade('" + ElementId + "')", this.Interval );
    }
    return;
  }
 
  oElement.FadeTimeLeft -= ElapsedTicks;
  
  var NewOpacityValue = oElement.FadeTimeLeft / this.TimeToFade;
  
  if( oElement.FadeState == 1 ) {
    NewOpacityValue = 1 - NewOpacityValue;
  }

  oElement.style.opacity = NewOpacityValue;
  oElement.style.filter = 'alpha(opacity = ' + ( NewOpacityValue * 100 ) + ')';

  // continue fading
  this.TimerId = setTimeout( this.Name + ".AnimateFade(" + CurrentTick + ",'" + ElementId + "')" , this.FadeInterval );
}

FadeSlide.prototype.Start = function() {
  this.TimerId = setTimeout( this.Name + ".ChangeImage()", this.StartDelay );
}

FadeSlide.prototype.Pause = function() {
  clearTimeout ( this.TimerId );
}

FadeSlide.prototype.Continue = function() {
  this.Fade( this.IdImg  );
}

