// alert('Ticker wird geladen');
var WiWo_Ticker = Class.create({
	iFps:             20,
	iDuration:        0.5,
	iDelay:           500,
	
	bScroll:          true,
	bAnim:            false,
	rTimer:           null,
	
	oElement:         null,
	oTicker:          null,
	iTickerHeight:    0,
	
	oList:            null,
	iListHeight:      0,
	
	oAnimation:       null,
	
	initialize: function(element)
	{
		this.oElement = element;
		
		this.oTicker = $(element).select('dd')[0];
		if (this.oTicker == null) return;
		
		this.oList = this.oTicker.select('ul')[0];
		if (this.oList == null) return;
		
		if (this.oList.getElementsByTagName('LI').length < 1) return;
		
		$(this.oList).setStyle({position: 'absolute'});
		$(this.oList).select('li').invoke('show');
		//      $(this.oElement).select('.overlay-gradient', '.scroll-up', '.scroll-down').invoke('show');
		
		this.iTickerHeight = $(this.oTicker).getHeight();
		this.iListHeight = $(this.oList).getHeight();
		
		// Start/Stop Events registrieren
		$(this.oTicker).observe('mouseover', this.stop.bindAsEventListener(this));
		$(this.oTicker).observe('mouseout', this.start.bindAsEventListener(this));
		
		// Buttons mit Funktionen ausstatten
		$(this.oTicker).select('a.scroll-up')[0].observe('click', function(e) {
			e.stop();
			this._scrollDown(false);
			return false;
		}.bindAsEventListener(this));
		
		$(this.oTicker).select('a.scroll-down')[0].observe('click', function(e) {
			e.stop();
			this._scrollUp(false);
			return false;
		}.bindAsEventListener(this));
		
		this.rTimer = window.setTimeout(function() {
			this._scrollUp(true);
		}.bind(this), this.iDelay);
	}, // function initialize
	
	_scrollUp: function()
	{
		if (this.bAnim) return;
		this.bAnim = true;
		
		if (this.rTimer != null)
		{
			window.clearTimeout(this.rTimer);
			this.rTimer = null;
		} // if
		
		var bAnimate = (arguments.length > 0) ? arguments[0] : false;
		var iCurPos = parseInt($(this.oList).getStyle('top') + 0);
		
		if ((iCurPos + this.iListHeight) <= 0)
		{
			if (bAnimate)
			{
				$(this.oList).setStyle({top: this.iTickerHeight + 'px'});
				this._doScroll(bAnimate, (this.iTickerHeight - 18), 'up');
				return;
			} // if
		} // if
		else
		{
			this._doScroll(bAnimate, (iCurPos - 18), 'up');
			return;
		} // else
		
		this.bAnim = false;;
	}, // function _scrollUp
	
	_scrollDown: function()
	{
		if (this.bAnim) return;
		this.bAnim = true;
		
		if (this.rTimer != null)
		{
			window.clearTimeout(this.rTimer);
			this.rTimer = null;
		} // if
		
		var iCurPos = parseInt($(this.oList).getStyle('top'));
		
		if (iCurPos < 0)
		{
			this._doScroll(false, (iCurPos + 18), 'down');
			return;
		} // if

		this.bAnim = false;
	}, // function _scrollDown
	
	_doScroll: function(bAnimate, iPos, sDirection)
	{
		if (bAnimate)
		{
			this.oAnimation = new Effect.Move($(this.oList), {
				fps: this.iFps,
				y: iPos,
				mode: 'absolute',
				transition: (bAnimate ? Effect.Transitions.linear : Effect.Transitions.full), 
				duration: this.iDuration,
				afterFinish: function() {
					this.bAnim = false;
					
					if (this.rTimer != null)
					{
						window.clearTimeout(this.rTimer);
					} // if
						
					if (sDirection == 'up')
					{
						this.rTimer = window.setTimeout(this._scrollUp.bind(this, true), this.iDelay * 2);
					} // if
				}.bind(this)
			});
		} // if
		else
		{
			$(this.oList).setStyle({top: iPos + 'px'});
			this.bAnim = false;
			
			if (this.bScroll && sDirection == 'up')
			{
				this.rTimer = window.setTimeout(this._scrollUp.bind(this, true), this.iDelay * 2);
			} // if
		} // else
	}, // function _doScroll
	
	stop: function(e)
	{
		this.bScroll = false;
		if (this.rTimer != null)
		{
			window.clearTimeout(this.rTimer);
			this.rTimer = null;
		} // if
	}, //  function stop
	
	start: function(e)
	{
		this.bScroll = true;
		if (this.bAnim) return;
		
		if (this.rTimer != null)
		{
			window.clearTimeout(this.rTimer);
		} // if
		this.rTimer = window.setTimeout(this._scrollUp.bind(this, true), 500);
	} // function start
});

var oTickerFunc = function() {
	$$('dl.box').each(function(oTicker, i) {
// alert('Neuer ticker');
		new WiWo_Ticker(oTicker);
	});
}

if (Prototype.Browser.IE)

	Event.observe(window, 'load', oTickerFunc);
else
	$(document).observe('dom:loaded', oTickerFunc);