/**
 * Funktionen für DHTML-Menülisten
 * @created 2009-03-24
 * @author agewert@lynet.de
 */

function scrollingMenu(elemId, maxEntries)
{
	var app = this;
	app.functions = new ly_functions();
	app.frameElem = document.getElementById(elemId);
	app.linkPrev = null;
	app.linkNext = null;
	app.listContainer = null;
	app.listLength = 0;
	app.activeOffset = 0;
	app.currentOffset = 0;
	app.maxEntries = maxEntries;

	app.showEntries = function() {
		var childItems = app.functions.filterChildNodes(app.listContainer.childNodes, 'LI');

		for (var i = 0; i < app.listLength; i++) {
			var pos = i +app.currentOffset;
			if (pos >= 0 && pos < app.maxEntries) {
				childItems[i].style['display'] = 'block';
			} else {
				childItems[i].style['display'] = 'none';
			}
		}
	}

	app.getMaxLeft = function() {
		var maxLeft = -(-app.maxEntries +app.listLength);
		return(maxLeft);
	}

	app.scrollBy = function(delta) {
		var maxLeft = app.getMaxLeft();
		var maxRight = 0;

		var newOffset = app.currentOffset +delta;
		if (newOffset < maxLeft) newOffset = maxLeft;
		if (newOffset > maxRight) newOffset = maxRight;

		// Nichts machen, wenn sich die Position nicht ändert
		if (newOffset == app.currentOffset) return;

		var childItems = app.functions.filterChildNodes(app.listContainer.childNodes, 'LI');
		var outElem = null;
		var inElem = null;

		// Klick auf "Links"
		if (delta > 0) {
			outElem = childItems[-app.currentOffset +app.maxEntries -1];
			inElem = childItems[-app.currentOffset -1];
		}

		// Klick auf "Rechts"
		else {
			inElem = childItems[-app.currentOffset +app.maxEntries];
			outElem = childItems[-app.currentOffset];
		}

		if (inElem && outElem) {
			app.currentOffset = newOffset;
			// Element ausblenden
			app.functions.transform(
				outElem,
				100,
				0,
				5,
				3,
				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) {
					elem.style['display'] = 'none';

					inElem.style['opacity'] = '0';
					inElem.style['-moz-opacity'] = '0';
					inElem.style['filter'] = 'Alpha(opacity=0,finishopacity=0,style=1)';
					inElem.style['display'] = 'block';

					// Element einblenden
					app.functions.transform(
						inElem,
						0,
						100,
						5,
						3,
						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)';
							app.setLinkClasses();
						}
					);
				}
			);
		}
	}

	app.setLinkClasses = function() {
		var maxLeft = app.getMaxLeft();
		var maxRight = 0;

		if (app.currentOffset <= maxLeft) {
			app.linkNext.className = app.functions.addClass(app.linkNext.className, 'inactive');
		} else {
			app.linkNext.className = app.functions.removeClass(app.linkNext.className, 'inactive');
		}

		if (app.currentOffset >= maxRight) {
			app.linkPrev.className = app.functions.addClass(app.linkPrev.className, 'inactive');
		} else {
			app.linkPrev.className = app.functions.removeClass(app.linkPrev.className, 'inactive');
		}
	}

	if (app.frameElem)
	{
		var childItems;

		// Links ermitteln
		//app.linkPrev = app.functions.filterChildNodesRecursive(app.functions.filterChildNodesRecursive(app.frameElem, 'DIV', 'previous')[0], 'A')[0];
		//app.linkNext = app.functions.filterChildNodesRecursive(app.functions.filterChildNodesRecursive(app.frameElem, 'DIV', 'next')[0], 'A')[0];

		app.linkPrev = app.functions.filterChildNodesRecursive(app.frameElem, 'A', 'slider_top')[0];
		app.linkNext = app.functions.filterChildNodesRecursive(app.frameElem, 'A', 'slider_bottom')[0];

		app.functions.addEvent(app.linkPrev, 'click', function() {
			app.scrollBy(1);
		} );

		app.functions.addEvent(app.linkNext, 'click', function() {
			app.scrollBy(-1);
		} );

		// Listenelemente ermitteln
		app.listContainer = app.functions.filterChildNodes(app.frameElem.childNodes, 'UL')[0];
		childItems = app.functions.filterChildNodes(app.listContainer.childNodes, 'LI');
		app.listLength = childItems.length;
		for (var i = 0; i < childItems.length; i++) {
			if (app.functions.hasClass('active', childItems[i].className)) {
				app.activeOffset = i;
			}
		}

		// Initiales Offset ermitteln
		app.currentOffset = 0;
		if (app.listLength >= app.maxEntries) {
			while ((app.activeOffset +app.currentOffset) >= app.maxEntries) {
				app.currentOffset -= 1;
			}
		}

		app.showEntries();
		app.setLinkClasses();
	}
}