/* (c) 2005 Koen Peters */
// @uses getScrollingOffset

var UITSLAG			= 0.5;	// De afstand die wordt doorgeschoten in verhouding tot de scroll lengte
var OMSLAGPUNT		= 0.5;	// Het deel van de totale tijd die de beweging in beslag neemt die wordt gebrukt tot aan de maximale uitslag
var TIJD			= 500;	// De tijd in milleseconden die de totale beweging in beslag neemt
var STAPPEN			= 100;	// Het aantal stappen dat wordt uitgevoerd binnen de tijd die de beweging in beslag neemt
var CONTROLETIJD	= 500;	// Het aantal milleseconden dat tussen twee controles op het wel of niet gescrolled zijn van de pagina zit
var MAXUITSLAG		= 40;	// De maxmale uitslag

var smoothScrollControl = null;

function SmoothScrollControl() {
	this.oudeScrollafstand				= 0; 
	this.huidigePositieBewegendVoorwerp	= 0; 
	this.compensatieAfstand				= 0;
	this.stapTimerTijd					= TIJD / STAPPEN;
	this.stappenInFase1					= Math.floor(OMSLAGPUNT * STAPPEN);
	this.stappenInFase2					= STAPPEN - this.stappenInFase1;	
	this.huidigeStap					= 0;
	this.bewegendVoorwerp				= document.getElementById('googleAd2');
	
	this.stapTimer						= null;
	this.controleTimer					= null;
	
	SmoothScrollControl.prototype.scrollVoorwerpOmlaag	= scrollVoorwerpOmlaag;
	SmoothScrollControl.prototype.scrollVoorwerpOmhoog	= scrollVoorwerpOmhoog;
	SmoothScrollControl.prototype.setBlok				= setBlok;
	SmoothScrollControl.prototype.controleerScroll		= controleerScroll;
	this.controleerScroll();
}

function controleerScroll() {
	//alert('controle');
	var nieuweScrollAfstand	= getScrollingOffsetY();
	if (nieuweScrollAfstand != this.oudeScrollafstand) {
		// Alle eventuele bewegingen stoppen en een nieuwe beginnen
		if (this.stapTimer) 
			clearTimeout(stapTimer);
		if (this.controleTimer) 
			clearTimeout(controleTimer);
		
		this.huidigeStap		= 0;
		this.oudeScrollafstand	= nieuweScrollAfstand;
		this.compensatieAfstand	= nieuweScrollAfstand - this.huidigePositieBewegendVoorwerp;
		if (this.compensatieAfstand > 0)
			this.scrollVoorwerpOmlaag();
		else 
			this.scrollVoorwerpOmlaag();
	}
	controleTimer = setTimeout('smoothScrollControl.controleerScroll()', CONTROLETIJD);
}

function scrollVoorwerpOmlaag() {
	if (this.huidigeStap < STAPPEN) {
		var newY = 0;
		if (this.huidigeStap < (STAPPEN * OMSLAGPUNT))
			// We zitten nog in de eerste fase van de beweging
			newY = this.oudeScrollafstand - this.compensatieAfstand * ((1 - ((this.huidigeStap * UITSLAG) / this.stappenInFase1)) * Math.cos((Math.PI * this.huidigeStap) / this.stappenInFase1));
		else
			// We zitten in de tweede fase van de beweging
			newY = this.oudeScrollafstand + this.compensatieAfstand * ((UITSLAG / 2) + (UITSLAG / 2) * Math.cos((Math.PI * (this.huidigeStap - this.stappenInFase1)) / this.stappenInFase2));
		
		this.setBlok(newY);
		this.huidigeStap++;
		stapTimer = setTimeout('smoothScrollControl.scrollVoorwerpOmlaag()', this.stapTimerTijd);
	}
}

function scrollVoorwerpOmhoog() {
}

function setBlok(y) {
	if (this.bewegendVoorwerp) {
		this.bewegendVoorwerp.style.marginTop	= y + 'px';
		this.huidigePositieBewegendVoorwerp		= y;
	}
}	