/* 
*=pflicht

vParams={newpos*, [scrollsmoothness, scrollstep]} 

*/
var smoothTimeoutHandle=null;
var smoothSavedEvents={onmousedown: null, onkeydown: null};

function scrollKillEvent(vEvent)
{
	// Timeout-Handler löschen
	if (smoothTimeoutHandle!=null)
	{
		window.clearTimeout(smoothTimeoutHandle);
		smoothTimeoutHandle=null;
	}

	// Events wieder herstellen
	if (smoothSavedEvents.onmousedown!=null)
	{document.onmousedown=smoothSavedEvents.onmousedown;
	}
	if (smoothSavedEvents.onkeydown!=null)
	{document.onkeydown=smoothSavedEvents.onkeydown;
	}
}

function smoothScrollTo(vParams)
{
  var currentPos=0;
	var documentHeight=0;
	var viewHeight=0;
  
  // Wenn Partnerlayout, dann kein Scrollen unterstützen
  if (typeof atraveo_layoutdata!="undefined" && atraveo_layoutdata.partnerlayout==true)
  {return;
  }

	// Scroll-Kill-Events erstellen
	if ((smoothSavedEvents.onmousedown==null && smoothSavedEvents.onkeydown==null) &&
			(document.onmousedown!=scrollKillEvent && document.onkeydown!=scrollKillEvent)
		)
	{
		// Events sichern
		smoothSavedEvents.onmousedown=document.mousedown;
		smoothSavedEvents.onkeydown=document.onkeydown;

		// KillEvent setzen
		document.onmousedown=document.onkeydown=scrollKillEvent;
	}

	// Document-Höhe ermitteln
	if (document.body && document.body.scrollHeight) // IE
	{documentHeight=document.body.scrollHeight;
	}
	else
	{documentHeight=document.height;
	}

	// View-Höhe ermitteln
	if (window.innerHeight)
	{viewHeight=window.innerHeight;
	}
	else // !IE
	{viewHeight=document.body.offsetHeight; // IE;
	}

	// Falls die neue Position einen negativen Wert enthält, wird die Höhe des documents ermittelt und der neue Positionswert abgezogen
	if (vParams.newpos<0)
	{
		// Neue Position auf Wert "von hinten" setzen
		vParams.newpos=documentHeight-Math.abs(vParams.newpos);
		//alert(vParams.newpos);
	}

	// Abbrechen, wenn die neue Position größer ist als die Document-Höhe
	if (vParams.newpos>=documentHeight-viewHeight)
	{vParams.newpos=documentHeight-viewHeight;
	}

  // Aktuelle Scroll-Position ermitteln
	if (window.pageYOffset)  // !IE
	{currentPos=window.pageYOffset;
	} 
	else 
	{
		if (document.body && document.body.scrollTop) // IE
		{currentPos=document.body.scrollTop;
		}
	}

	// Überprüfen ob ggfs. zwischendurch gescrollt wurde und der Abstand entsprechend groß ist, dann evtl. abbgrechen
	if (typeof vParams.currentpos!="undefined" && currentPos!=vParams.currentpos)
	{
		if (smoothTimeoutHandle!=null)
		{window.clearTimeout(smoothTimeoutHandle);
		}
		scrollKillEvent(null);
		return;
	}

	scrollStep=( typeof vParams.scrollstep!="undefined"?vParams.scrollstep:(Math.abs(vParams.newpos-currentPos)/(typeof vParams.scrollsmoothnesss!="undefined"?vParams.scrollsmoothnesss:10)) );
	
	if (scrollStep<1)
	{scrollStep=1;
	}
	
	//alert("CurrentPos: "+currentPos+", ScrollStep: "+scrollStep);

	if (currentPos<=vParams.newpos-(typeof vParams.scrollstep!="undefined"?vParams.scrollstep:2) || currentPos>=vParams.newpos+(typeof vParams.scrollstep!="undefined"?vParams.scrollstep:2))
	{
		var tmpScrollByStep=scrollStep*(vParams.newpos<currentPos?-1:+1);
		tmpScrollByStep=Math.round(tmpScrollByStep);

		window.scrollBy(0, tmpScrollByStep );
		
		if (smoothTimeoutHandle!=null)
		{window.clearTimeout(smoothTimeoutHandle);
		}

		smoothTimeoutHandle=window.setTimeout("smoothScrollTo({newpos: "+vParams.newpos+", currentpos: "+(currentPos+tmpScrollByStep)+(typeof vParams.scrollstep!="undefined"?", scrollstep: "+vParams.scrollstep:"")+(typeof vParams.scrollsmoothnesss!="undefined"?", scrollsmoothnesss: "+vParams.scrollsmoothnesss:"")+"})", 0);
	}
	else
	{
		window.scrollTo(0, vParams.newpos);
		//alert("scrollTo");
	}

}