var SrgListRotator =
{
	/**************************************************
	 * Array keeping the rotators of the current page *
	 **************************************************/
	CurrentRotators: null,

	/**************************************************************************
	 * Create a new list-rotator                                              *
	 *                                                                        *
	 * Parameters:                                                            *
	 * - sKey:                  Key of the rotator, must be unique on a page. *
	 * - sUlId:                 Id of the list to use as container.           *
	 * - iWidth:                Width of the rotator in px.                   *
	 * - iHeight:               Height of the rotator in px.                  *
	 * - iDisplayTime:          Timespan between fading in and out in ms.     *
	 * - iHideTime:             Timespan between news, display white, in ms.  *
	 * - iStepInterval:         Interval between opacity change steps.        *
	 * - dStepOpacityChange:    Opacity change step in percent.               *
	 **************************************************************************/
	Register: function(sKey, sListId, iWidth, iHeight, iDisplayTime, iHideTime, iStepInterval, dStepOpacityChange)
	{
		//--- Init array if null ---
		if(this.CurrentRotators == null)
		{
			this.CurrentRotators = new Array();
		}
		//--- Append rotator ---
		this.CurrentRotators[this.CurrentRotators.length] =
			{
				CurrentIndex: 0,
				CurrentOpacity: 0,
				CurrentTimeoutCursor: -1,
				CurrentFadingIn: true,
				Key: sKey,
				ListId: sListId,
				Width: iWidth,
				Height: iHeight,
				DisplayTime: iDisplayTime,
				HideTime: iHideTime,
				StepInterval: iStepInterval,
				StepOpacityChange: dStepOpacityChange
			}
		//--- Read back from array ---
		var oCurrentRotator = this.CurrentRotators[this.CurrentRotators.length - 1];
		//--- Get the list ---
		var oList = document.getElementById(sListId);
		if(typeof (oList) == 'object' && oList != null)
		{
			oList.onmouseover = new Function('SrgListRotator.OnRollOver("' + sKey + '");');
			oList.onmouseout = new Function('SrgListRotator.OnRollOut("' + sKey + '");');
			oList.style.width = iWidth + 'px';
			oList.style.height = iHeight + 'px';
			oList.style.overflow = 'hidden';
			oList.style.display = 'block';
			//--- Loop list items --> Set all invisible except the 1st ---
			var aListItems = oList.getElementsByTagName('li');
			for(var i = 0; i < aListItems.length; i++)
			{
				//--- Set dimension ---
				aListItems[i].style.width = iWidth + 'px';
				aListItems[i].style.height = iHeight + 'px';
				aListItems[i].style.display = 'block';
				aListItems[i].style.paddingBottom = 0;
				if(i > 0)
				{
					aListItems[i].style.marginTop = '-' + iHeight + 'px';
					aListItems[i].style.visibility = 'hidden';
				}
				//--- Set initial opacity for item ---
				SrgGlobal.SetOpacity(aListItems[i], 0);
			}
			//--- Start rotation interval ---
			oCurrentRotator.CurrentTimeoutCursor = window.setTimeout('SrgListRotator.Step("' + sKey + '");', iStepInterval);
		}
		else
		{
			alert("List element with ID '" + sListId + "' for list rotator '" + sKey + "' was not found!");
		}
	},
	
	/*********************************************
	 * Stop and delete an existing list rotator. *
	 *********************************************/
	Unregister: function(sKey)
	{
		//--- Get rotator ---
		var oCurrentRotator = this.GetRotatorByKey(sKey);
		if(oCurrentRotator!=null)
		{
			//--- Stop rotation ---
			if(oCurrentRotator.CurrentTimeoutCursor > -1)
			{
				window.clearTimeout(oCurrentRotator.CurrentTimeoutCursor);
				oCurrentRotator.CurrentTimeoutCursor = -1;
			}
			//--- Get the list ---
			var oList = document.getElementById(oCurrentRotator.ListId);
			if(typeof(oList)=='object' && oList!=null)
			{
				oList.onmouseover = null;
				oList.onmouseout = null;
				oList.style.width = 'auto';
				oList.style.height = 'auto';
				oList.style.overflow = 'visible';
				//--- Loop list items --> Set all visible ---
				var aListItems = oList.getElementsByTagName('li');
				for(var i=0; i<aListItems.length; i++)
				{
					aListItems[i].style.width = 'auto';
					aListItems[i].style.height = 'auto';
					aListItems[i].style.marginTop = 'auto';
					aListItems[i].style.paddingBottom = '1em';
					aListItems[i].style.visibility = 'visible';
					//--- Set default opacity for item ---
					SrgGlobal.SetOpacity(aListItems[i], 100);
				}
			}
			//--- Remove the rotator from array ---
			this.RemoveRotatorByKey(sKey);
		}
	},

	/*********************************
	 * Stops the rotator on rollover *
	 *********************************/
	OnRollOver: function(sKey)
	{
		//--- Get rotator ---
		var oCurrentRotator = this.GetRotatorByKey(sKey);
		if(oCurrentRotator != null)
		{
			//--- Get the list and it's items ---
			var oList = document.getElementById(oCurrentRotator.ListId);
			var aListItems = oList.getElementsByTagName('li');
			if(oList != null && aListItems != null)
			{
				//--- Stop rotation ---
				if(oCurrentRotator.CurrentTimeoutCursor > -1)
				{
					window.clearTimeout(oCurrentRotator.CurrentTimeoutCursor);
					oCurrentRotator.CurrentTimeoutCursor = -1;
				}
				//--- Perform a step and stop... ---
				oCurrentRotator.CurrentOpacity = 100;
				this.Step(sKey, true);
			}
		}
	},

	/***********************************
	 * Restarts the rotator on rollout *
	 ***********************************/
	OnRollOut: function(sKey)
	{
		//--- Get it steppin' again ---
		this.Step(sKey);
	},

	/****************************************************************************
	 * Is called for each rotation and each opacity-change step using timeouts. *
	 ****************************************************************************/
	Step: function(sKey, bStopAfter)
	{
		var oCurrentRotator = this.GetRotatorByKey(sKey);
		if(oCurrentRotator != null)
		{
			//--- Clear current timeout cursor to avoid confusion ---
			oCurrentRotator.CurrentTimeoutCursor = -1;
			//--- Get the list and it's items ---
			var oList = document.getElementById(oCurrentRotator.ListId);
			var aListItems = oList.getElementsByTagName('li');
			if(oList != null && aListItems != null)
			{
				//--- Get current list-item ---
				var oCurrentItem = aListItems[oCurrentRotator.CurrentIndex];
				oCurrentItem.style.visibility = 'visible';
				//--- Calculate opacity change ---
				if(oCurrentRotator.CurrentFadingIn)
				{
					oCurrentRotator.CurrentOpacity += oCurrentRotator.StepOpacityChange;
				}
				else
				{
					oCurrentRotator.CurrentOpacity -= oCurrentRotator.StepOpacityChange;
				}
				//--- Set opacity ---
				SrgGlobal.SetOpacity(oCurrentItem, oCurrentRotator.CurrentOpacity);
				//--- Check if we're finished fading in ---
				if(oCurrentRotator.CurrentFadingIn && oCurrentRotator.CurrentOpacity >= 100)
				{
					//--- Stop fading in, start fading out. ---
					oCurrentRotator.CurrentFadingIn = false;
					//--- Set timeout for the next rotation ---
					if(!bStopAfter)
					{
						oCurrentRotator.CurrentTimeoutCursor = window.setTimeout('SrgListRotator.Step("' + sKey + '");', oCurrentRotator.DisplayTime);
					}
				}
				//--- Check if we're finished fading out ---
				else if(!oCurrentRotator.CurrentFadingIn && oCurrentRotator.CurrentOpacity <= 0)
				{
					//--- Stop fading out, start fading in the next item. ---
					oCurrentRotator.CurrentFadingIn = true;
					//--- Step to the next item ---
					oCurrentRotator.CurrentIndex = (oCurrentRotator.CurrentIndex+1<aListItems.length) ? oCurrentRotator.CurrentIndex+1 : 0;
					//--- Set timeout for the next rotation ---
					if(!bStopAfter)
					{
						oCurrentRotator.CurrentTimeoutCursor = window.setTimeout('SrgListRotator.Step("' + sKey + '");', oCurrentRotator.HideTime);
					}
				}
				else
				{
					if(!bStopAfter)
					{
						oCurrentRotator.CurrentTimeoutCursor = window.setTimeout('SrgListRotator.Step("' + sKey + '");', oCurrentRotator.StepInterval);
					}
				}
			}
		}
	},
	
	/******************************************************************************
	 * Returns the cursor for the rotator with the passed key or -1 if not found. *
	 ******************************************************************************/
	GetRotatorCursorByKey: function(sKey)
	{
		//--- Get the current rotator ---
		for(var i = 0; i < this.CurrentRotators.length; i++)
		{
			if(this.CurrentRotators[i].Key.toLowerCase() == sKey.toLowerCase())
			{
				return i;
			}
		}
		return -1;
	},
	
	/*****************************************************************
	 * Returns the rotator with the passed key or null if not found. *
	 *****************************************************************/
	GetRotatorByKey: function(sKey)
	{
		var iRotatorCursor = this.GetRotatorCursorByKey(sKey);
		if(iRotatorCursor>-1)
		{
			return this.CurrentRotators[iRotatorCursor];
		}
		return null;
	},

	/********************************************
	 * Removes the rotator with the passed key. *
	 ********************************************/
	RemoveRotatorByKey: function(sKey)
	{
		var iRotatorCursor = this.GetRotatorCursorByKey(sKey);
		if(iRotatorCursor>-1)
		{
			//--- Keep old array ---
			var aOldRotators = this.CurrentRotators;
			//--- Init new array ---
			var aNewRotators = new Array();
			//--- Copy all entries except the one we want to delete... ---
			for(var i=0; i<aOldRotators.length; i++)
			{
				if(i!=iRotatorCursor)
				{
					aNewRotators[aNewRotators.length] = aOldRotators[i];
				}
			}
			//--- Set new array ---
			this.CurrentRotators = aNewRotators;
		}
	}
}
