/**
 * DHTML-Newsticker
 * @created 2009-06-09
 * @author agewert@lynet.de
 */

function newsticker(listId, duration)
{
	var app = this;
	app.functions = new ly_functions();
	app.currentOffset = 0;
	app.listContainer = null;
	app.listLength = 0;
	app.duration = 5000;

	app.showNextItem = function() {
		var newOffset = app.currentOffset +1;
		if (newOffset >= app.listLength) newOffset = 0;
		var childItems = app.functions.filterChildNodes(app.listContainer.childNodes, 'LI');

		app.functions.transform(
			childItems[app.currentOffset],
			100,
			0,
			3,
			2,
			75,

			function(elem, o)
			{
				elem.style['opacity'] = String(o /100);
				elem.style['-moz-opacity'] = String(o /100);
				elem.style['filter'] = 'Alpha(opacity=' + String(o) + ',finishopacity=' + String(o) + ',style=1)';
			},

			function(elem)
			{
				childItems[app.currentOffset].className = app.functions.removeClass(childItems[app.currentOffset].className, 'active');
				childItems[newOffset].style['opacity'] = '0';
				childItems[newOffset].style['-moz-opacity'] = '0';
				childItems[newOffset].style['opacity'] = 'Alpha(opacity=0,finishopacity=0,style=1)';
				childItems[newOffset].className = app.functions.addClass(childItems[newOffset].className, 'active');

				app.functions.transform(
					childItems[newOffset],
					0,
					100,
					3,
					2,
					75,

					function(elem2, o) {
						elem2.style['opacity'] = String(o /100);
						elem2.style['-moz-opacity'] = String(o /100);
						elem2.style['filter'] = 'Alpha(opacity=' + String(o) + ',finishopacity=' + String(o) + ',style=1)';
					},

					function(elem2) {
						app.currentOffset = newOffset;
						window.setTimeout(app.showNextItem, app.duration);
					}

				);
			}
		);
	}

	if (duration) app.duration = parseInt(duration);
	if (app.duration < 10) app.duration = 10;

	if (listId)
	{
		app.listId = listId;
		var childItems;

		app.listContainer = document.getElementById(app.listId);
		if (app.listContainer)
		{
			childItems = app.functions.filterChildNodes(app.listContainer.childNodes, 'LI');
			app.listLength = childItems.length;

			// Initial (nur) das erste Element einblenden
			for (var i = 0; i < app.listLength; i++)
			{
				if (i == 0) {
					childItems[i].className = app.functions.addClass(childItems[i].className, 'active');
				} else {
					childItems[i].className = app.functions.removeClass(childItems[i].className, 'active');
				}
			}

			// Anschließend Timer setzen mit FadeIn / FadeOut
			window.setTimeout(app.showNextItem, app.duration);
		}
	}
}
