// Diashow auf Basis von Scriptaculous.

// intSystemId:		Eindeutige ID zur Identifizierung der entsprechenden DIV's
// intPicCount:		Anzahl der vorhandenen Elemente
// intSpeed:		Darstellungsdauer eines Elementes in Millisekunden
// intDuration:		Dauer einer Ãœberblendung in Millisekunden
// bitLoop:			Soll die Diashow als Endlosschleife laufen?
// strRedirectURL:	Am ende der Diashow oder bei Klick wird hierher gesprungen
//

function GetRandom( min, max ) {
  if( min > max ) {
  return( -1 );
  }
   if( min == max ) {
   return( min );
  }
  var r = parseInt( Math.random() * ( max+1 ) );
  return( r + min <= max ? r + min : r );
}


function diashow(intSystemId, intPicCount, intSpeed, intDuration, bitLoop, strRedirectURL, intStartPict)
	{
	this.intSystemId = intSystemId;
	this.intPicCount = intPicCount;
	this.floatSpeed = parseFloat(intSpeed + intDuration) / 1000.0;	// Speed in Sekunden umrechnen
	this.floatDuration = parseFloat(intDuration) / 1000.0;			// Duration in Sekunden umrechnen
	this.bitLoop = bitLoop;
	this.strRedirectURL = strRedirectURL;
	this.intStartPict = intStartPict;
	this.intPicShown = 1;											// Das aktuell dargestellte Bild
	this.intPicNext = 2;											// Das als nÃ¤chstes darzustellende Bild
	
		if(this.intStartPict == 0)
		{
		this.intPicNext = GetRandom( 1, this.intPicCount );
		Effect.Appear('diashow_' + this.intSystemId + '_' + this.intPicNext, {duration: 0});
		this.intStartPict = 1;
		}

	
	// Zeigt das nÃ¤chste Bild der Diashow an
	this.show_next_pic = function(objInterval)
		{

		// Die Werte fÃ¼r aktuelles und nÃ¤chstes Bild neu setzen
		this.intPicShown = this.intPicNext;
		this.intPicNext = GetRandom( 1, this.intPicCount ) // this.intPicNext + 1;
		
	if( this.intPicShown != this.intPicNext ) {

		// Das aktuelle Bild ausblenden
		Effect.Fade('diashow_' + this.intSystemId + '_' + this.intPicShown, {duration: this.floatDuration});


		// Das nÃ¤chste Bild einblenden
		Effect.Appear('diashow_' + this.intSystemId + '_' + this.intPicNext, {duration: this.floatDuration});
		
		// Falls wir am Ende sind, auf das erste Bild zurÃ¼ckspringen
		if(this.intPicNext > this.intPicCount)
			{
			// Aber nur, wenn die Diashow in einer Schleife laufen soll
			if(this.bitLoop)
				{
				this.intPicNext = 1;
				}
			else
				{
				// Ansonsten das Intervall beenden
				objInterval.stop();
				
				// Und weiterleiten, falls URL angegeben wurde
				if(this.strRedirectURL != '')
					{
					new PeriodicalExecuter(this.goto_url.bind(this), this.floatSpeed);
					}
				}
			}
			}
		}
	
	// Springt zur hinterlegten URL
	this.goto_url = function()
		{
		if(this.strRedirectURL != '')
			{
			window.location.href = this.strRedirectURL;
			}
		}
	
	// Wenn eine URL Ã¼bergeben wurde...
	if(strRedirectURL != '')
		{
		// ...dem Diashowelement ein Klickevent mit Weiterleitung spendieren
		Event.observe($('diashow_' + this.intSystemId), 'click', this.goto_url.bind(this), true);
		
		// ...und noch einen "Zeigepointer"
		$('diashow_' + this.intSystemId).style.cursor = 'pointer';
		}
	
	// Das Intervall starten, welches fÃ¼r die Ãœberblendungen sorgt
	new PeriodicalExecuter(this.show_next_pic.bind(this), this.floatSpeed);
	
	} // diashow()