/*
funktionen des Slideshow-objekts:

startSlides()   : startet die slideshow
stopSlides()    : stoppt die slideshow (wenn die aktuelle überblendung zu ende ist)
toggleSlides()  : startet oder stoppt die slideshow, je nach dem, ob sie schon läuft
nextPic()       : blendet zum nächsten bild über (geht nur, wenn slideshow gestoppt ist)
prevPic()       : blendet zum vorigen bild über (geht nur, wenn slideshow gestoppt ist)
*/

// diese zeile sorgt dafür, dass die daten von allen slideshows
// ins cookie geschrieben werden, wenn das aktuelle dokument
// ge-unloadet wird:
window.onunload = putSlideshowDataToCookie;

// alle Slideshow.nFadeTimeout millisekunden wird die animate-funktion aufgerufen.
// kleinere werte sorgen für glatteren ablauf, brauchen aber auch mehr performance.
// 50ms enstpricht: 1s / 50ms = 20 fps
Slideshow.nFadeTimeout = 50;

// hier werden alle slideshows drin gespeichert. NICHT ÄNDERN!
Slideshow.arInstances = new Array();

function Slideshow(sName, nFadeTime, nStayTime, bLoadFromCookie)
{
  this.sName = sName;
  this.nIntervalStay = nStayTime;
  this.nFadePerStep = 1.0 / (nFadeTime / Slideshow.nFadeTimeout);
  this.arPics = document.getElementsByName(this.sName);

  if (bLoadFromCookie)
  {
    var ar = getSlideshowDataFromCookie(this.sName);
    if (ar)
    {
      var n = parseFloat(ar[0]);
      if (!isNaN(n))
        this.fCurOpacity = n;
      n = parseFloat(ar[1]);
      if (!isNaN(n))
        this.nPicOut = n;
      n = parseFloat(ar[2]);
      if (!isNaN(n))
        this.nPicIn = n;
      n = parseFloat(ar[3]);
      if (!isNaN(n))
        this.bIsFading = (1 == n);
      if ((this.nPicIn >= this.arPics.length) || (this.nPicOut >= this.arPics.length))
      {
        this.nPicOut = 0;
        this.nPicIn = 1;
      }
    }
    Slideshow.arInstances[Slideshow.arInstances.length] = this;
  }

  for (var i = 1; i < this.arPics.length; i++)
    this.setOpacity(this.arPics[i], 0.0);
}

Slideshow.prototype = 
{
  sName : "",
  nPicIn : 1,
  nPicOut : 0,
  arPics : null,
  fCurOpacity : 0.0,
  nFadePerStep : 0.01,
  nIntervalStay : 0,
  timerID : 0,
  bIsFading : false,
  bRunning : false,
  onbeginfade : null,
  onendfade : null,

  getData : function()
  {
    var n = (this.bIsFading) ? 1 : 0;
    return this.sName + ":" + this.fCurOpacity + ":" + this.nPicOut + ":" + this.nPicIn + ":" + n;
  },

  toggleSlides : function()
  {
    if (this.bRunning)
      this.stopSlides();
    else
      this.startSlides();
    return this.bRunning;
  },

  startSlides : function()
  {
    if (this.arPics.length < 2)   // must have at least two pics!
      return false;
    if (0 == this.nFadePerStep)   // doesn't fade
      return false;

    this.bRunning = true;

    // show current picture
    var pic = this.arPics[this.nPicOut];
    this.setOpacity(pic,1.0 - this.fCurOpacity);

    // prepare next pic for fadein
    pic = this.arPics[this.nPicIn];
    this.setOpacity(pic,this.fCurOpacity);

    // start timer
    var me = this;
    if (this.bIsFading)
      this.beginFade();
    else
      this.timerID = window.setInterval(function(){me.animate();},this.nIntervalStay);
    return this.timerID;
  },

  stopSlides : function()
  {
    this.bRunning = false;
  },

  nextPic : function()
  {
    if (this.bRunning || this.bIsFading)
      return;
    this.beginFade();
  },

  prevPic : function()
  {
    if (this.bRunning || this.bIsFading)
      return;
    this.nPicIn -= 2;
    if (this.nPicIn < 0)
      this.nPicIn += this.arPics.length;
    this.beginFade();
  },

  setOpacity : function(el,n)
  {
    if (0.0 == n)
    {
      el.style.visibility = "hidden";
    }
    else
    {
      el.style.visibility = "visible";
      el.style.opacity = n;
      el.style.filter="alpha(opacity=" + Math.round(n * 100) + ")";
      el.style.khtmlOpacity = n;
      el.style.mozOpacity = n;
    }
  },

  beginFade : function()
  {
    if (this.onbeginfade)
      this.onbeginfade(this);
    var me = this;
    this.bIsFading = true;
    this.timerID = window.setInterval(function(){me.animate();},Slideshow.nFadeTimeout);
  },

  animate : function()
  {
    if (!this.bIsFading)
    {
      window.clearInterval(this.timerID);
      this.timerID = 0;
      if (!this.bRunning)
        return;
      this.beginFade();
      return;
    }

    var picOut = this.arPics[this.nPicOut];
    var picIn = this.arPics[this.nPicIn];
    this.fCurOpacity += this.nFadePerStep;
    if (this.fCurOpacity >= 1.0)
    {
      // new picture is now full visible

      window.clearInterval(this.timerID);
      this.timerID = 0;
      this.fCurOpacity = 0.0;
      this.setOpacity(picOut, 0.0);
      // switch pics
      this.nPicOut = this.nPicIn;
      picOut = picIn;

      this.nPicIn++;
      if (this.nPicIn == this.arPics.length)
        this.nPicIn = 0;
      picIn = this.arPics[this.nPicIn];

      var me = this;
      this.bIsFading = false;
      if (this.onendfade)
        this.onendfade(this);
      if (this.bRunning)
        this.timerID = window.setInterval(function(){me.animate();},this.nIntervalStay);
    }
    this.setOpacity(picIn, this.fCurOpacity);
    this.setOpacity(picOut, 1.0 - this.fCurOpacity);
  }
}

function getSlideshowDataFromCookie(sSlideshowName)
{
  try
  {
    var arShows = document.cookie.split("|");
    for (var i = 0; i < arShows.length; i++)
    {
      var ar = arShows[i].split(":");
      if (ar[0] == sSlideshowName)
      {
        ar.splice(0,1);
        if (4 != ar.length)
          return false;
        return ar;
      }
    }
  }
  catch(e)
  {}
  return false;
}

function putSlideshowDataToCookie()
{
  var s = "";
  for (var i = 0; i < Slideshow.arInstances.length; i++)
  {
    s += Slideshow.arInstances[i].getData() + "|";
  }
  document.cookie = s;
}

