/************************************
* Autor: shennemann
* Erstellt am: 30.10.2010 12:44:17
*
* Zuletzt gändert
* von:
************************************/

function scroller(element,pfeil_links,pfeil_rechts,pfeil_breite,endlessScrolling) {
	var doc = document,
		win = window,
		caller = this,
		steps = 0,
		scrollposition = 0,
		length = 0;
		
		if (typeof(element) != "object") {
			element = doc.getElementById(element);
		}
		if (typeof(pfeil_links != "object")) {
			pfeil_links = doc.getElementById(pfeil_links);
		}
		if (typeof(pfeil_rechts != "object")) {
			pfeil_rechts = doc.getElementById(pfeil_rechts);
		}

		if (!pfeil_breite) {
			pfeil_breite = 20;
		}

		if (typeof(endlessScrolling) == "undefined") {
			endlessScrolling = false;
		}


	/* bilder verdreifachen */
	function setWidth() {
		if (endlessScrolling) {
			var childs = element.childNodes;
			for (i = 0; i < childs.length; i++ ) {
				//alert(childs[i].nodeName);
				if (childs[i].nodeName.toLowerCase() == "table") {
					subchilds = childs[i].getElementsByTagName("tr");
					length = subchilds[0].offsetWidth;
					scrollposition = - length;

					subTr = subchilds[0].children;
					checkLength = parseInt(subTr.length, 10);
					for (x = 0; x < checkLength; x++) {
						firstNewTd = subTr[(x * 2)].cloneNode(true);
						//firstNewTd.id = firstNewTd.id + "a";
						subchilds[0].appendChild(firstNewTd);

						secondNewTd = subTr[(x * 2)].cloneNode(true);
						//secondNewTd.id = secondNewTd.id + "b";
						subchilds[0].insertBefore(secondNewTd, subTr[x]);
					}

				}
			}
		}
	}

	if (endlessScrolling) {
		if (window.addEventListener) {
			window.addEventListener('load', setWidth, false);
		} else if (window.attachEvent) {
			window.attachEvent('onload',setWidth);
		} else {
			window.onload = setWidth;
		}
	}




	this.scrollstart = function(steps)
		{
			var stop=false;
			scrollposition += steps;

			if (scrollposition > 0)
			{
				if (!endlessScrolling) {
					scrollposition = 0;
					stop = true;
				} else {
					scrollposition = scrollposition - length  - (pfeil_breite*2) + 10 + steps;
				}
			}

			if (scrollposition < -(element.offsetWidth - element.parentNode.offsetWidth + 10 + pfeil_breite*2))
			{
				if (!endlessScrolling) {
					scrollposition = -(element.offsetWidth - element.parentNode.offsetWidth + 10 + pfeil_breite*2);
					pfeil_rechts.style.backgroundPosition = "center center";
					stop = true;
				} else {
					scrollposition = scrollposition + length + (pfeil_breite*2) - 10 + steps;
				}
			}
			else
			{
				pfeil_rechts.style.backgroundPosition = "center left";
			}

			//doc.getElementById("jstest").value = scrollposition;

			element.style.left = scrollposition + "px";

			if (scrollposition < 0)
			{
				pfeil_links.style.backgroundPosition = "center left";
			}
			else
			{
				pfeil_links.style.backgroundPosition = "center center";
			}

			if (stop == false)
			{
				if (steps > 0)
				{
					pfeil_links.style.backgroundPosition = "center right";
				}
				else
				{
					pfeil_rechts.style.backgroundPosition = "center right";
				}
				this.scroller = setTimeout(function() {caller.scrollstart(steps)},20);
			}
			else
			{
				this.scrollstop();
			}

		}

		this.scrollstop = function()
		{
			if (scrollposition <= -(element.offsetWidth - element.parentNode.offsetWidth))
			{
				pfeil_rechts.style.backgroundPosition = "center center";
			}
			else
			{
				pfeil_rechts.style.backgroundPosition = "center left";
			}
			if (scrollposition < 0)
			{
				pfeil_links.style.backgroundPosition = "center left";
			}
			else
			{
				pfeil_links.style.backgroundPosition = "center center";
			}
			clearTimeout(this.scroller);
		}

}
