var refDivId;
var tags = new Array();
var bScrollActive = true;
var nextWaitTag = 0;
var cycleTick = 10;
var cycleWaiting = false;
var cycleWaitCount = 300;
var cycleCurWaitCount = 300;
var ie;
var isWaitingForTimeout = false;

function stopScroll(){bScrollActive = false;}
function startScroll(){bScrollActive = true; if(isWaitingForTimeout== false) setTimeout('doScroll()', cycleTick);}
function initScroll(divId)
{
	try
	{
		agent = navigator.userAgent.toLowerCase(),
		version = navigator.appVersion;
		docBase = (document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body? document.body : null;
		op = !!(window.opera && document.getElementById);
		ie = agent.indexOf("msie") != -1 && document.all && docBase && !op;
		refDivId = divId
		// Récupération du contenu de la div
		var one=document.getElementById(divId).innerHTML;
		var all="";

		// Cas IE
		if(ie)
		{
			for(i=0;i<7;i++)
				document.getElementById(divId).insertAdjacentHTML("beforeEnd", one);
		}
		else
		{
			for(i=0;i<8;i++)
				all+=one;
			document.getElementById(divId).innerHTML = all;
		}

		// Récupération des offsets de tag
		var elts = document.getElementById(divId).getElementsByTagName("A");
		var count=0;
		for(i=0; i<elts.length; i++)
		{
			if(elts[i].href == "")
			{
				tags[count] = getOffsetContainer(elts[i], document.getElementById(divId));
				count++;
			}
		}
		if(count>1)
			nextWaitTag=1;
		/*for(i=0;i<count;i++)
		{
			alert('tags['+i+']='+tags[i]);
		}*/
		// Déclenchement du scroll
		setTimeout('doScroll()', cycleTick);
		isWaitingForTimeout = true;
	}
	catch (e)
	{
	}
}
function doScroll()
{
	var divScrollTop = document.getElementById(refDivId).scrollTop;
	// Scroll Actif ?
	if(bScrollActive == false)
	{
		isWaitingForTimeout = false;
		return;
	}

	// Recherche du prochain tag sur lequel s'arrêter
	if( (cycleWaiting == false) && (divScrollTop >= tags[nextWaitTag]) )
		cycleWaiting = true;

	// Gestion de le tempo pour les temps d'arrêts
	if( cycleWaiting == true){
		if( (cycleCurWaitCount--)>0 )
		{
			setTimeout('doScroll()', cycleTick);
			return;
		}
		else
		{
			cycleWaiting=false;
			cycleCurWaitCount=cycleWaitCount;
			nextWaitTag++;
			if(nextWaitTag >= tags.length) nextWaitTag=0;
		}
	}

	// Détection de fin de page	
	document.getElementById(refDivId).scrollTop+=1;
	if(document.getElementById(refDivId).scrollTop == divScrollTop)
	{
		document.getElementById(refDivId).scrollTop = 0;
		cycleWaiting=false;
		cycleCurWaitCount=cycleWaitCount;
		nextWaitTag = 0;
	}
	setTimeout('doScroll()', cycleTick);
	isWaitingForTimeout = true;
}
function getOffsetContainer(elt, container)
{
	var height=0;
	var parent = elt.offsetParent;
	height = elt.offsetTop;
	while((parent.tagName!="DIV")&&(parent.tagName!="BODY"))
	{
		height += parent.offsetTop;
		parent=parent.offsetParent;
	}
	if(!ie){
		height -= container.offsetTop;
	}
	return height;
}
