var topMargin = 0;
var originalY = -1;
var slideTime = 1200;
var ns6 = (!document.all && document.getElementById);
var ie4 = (document.all);
var ns4 = (document.layers);
var targetY;
var findHt;
var elementCibleId;
// Initialisation selon l'id
function init_scroll(element,topMargin_p,originalY_p,slideTime_p)
{
	elementCibleId = element;
	window.setInterval("main()", 10);
	topMargin = topMargin_p;
	originalY = originalY_p;
	slideTime = slideTime_p;
}

function getScrollTop() {
	if (window.pageYOffset) {
    	return window.pageYOffset;
  	}
  	else if (document.documentElement && document.documentElement.scrollTop) {
    	return document.documentElement.scrollTop;
  	}
	else if (document.body) {
    	return document.body.scrollTop
  	}
return 0;
} 

function getClientWidth() {
	if (window.innerWidth) {
    	theWidth = window.innerWidth;
  	}
  	else if (document.documentElement && document.documentElement.clientWidth) {
    	theWidth = document.documentElement.clientWidth;
  	}
  	else if (document.body) {
    	theWidth = document.body.clientWidth;
  	}
}

function floatObject() {
	if (ns4 || ns6) {
		findHt = getClientWidth();
	} 
	else if(ie4) {
		findHt = getClientWidth();
	}
}

function main() {
	currentY = (document.getElementById(elementCibleId).style.top == "" ? 0 : parseInt(document.getElementById(elementCibleId).style.top));
	scrollTop = getScrollTop();
	if (originalY <= 0) {
		originalY = getLayerY(document.getElementById(elementCibleId));
	}
	mainTrigger();
}

function mainTrigger() {
	var newTargetY = 0;
	if (scrollTop > originalY)
		newTargetY = scrollTop + topMargin - originalY;
	if ( currentY != newTargetY ) {
		if ( newTargetY != targetY ) {
			targetY = newTargetY
			floatStart();
		}
		animator();
	}
}

function floatStart() {
	var now = new Date()
	A = targetY - currentY
	B = Math.PI / ( 2 * slideTime )
	C = now.getTime()
	if (Math.abs(A) > findHt) {
		D = A > 0 ? targetY - findHt : targetY + findHt
		A = A > 0 ? findHt : -findHt
	} else {
		D = currentY
	}
}

function animator() {
	var now = new Date()
	var newY = A * Math.sin( B * ( now.getTime() - C ) ) + D
	newY = Math.round(newY)
	if (( A > 0 && newY > currentY ) || ( A < 0 && newY < currentY )) {
		document.getElementById(elementCibleId).style.top = newY + "px";
	}
}

function getLayerY(obj) {
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}