// Scroller's variables
var intervalID;																			// Timer ID (to scroll one element)
var timerID;																			// Timer ID (delay between scrolls)
var visibleElementNb = 0;																// Identify the current (visible) element
var isScrolling      = 0;																// Are we currently scrolling ?

function launchHorizScroller(scrollerID, nbElementsInScroller, oneScrollLength, delayBetweenScrolls)
{
	// Return if we don't have two or more elements to show
	if(nbElementsInScroller<=1)
	{
		return;
	}
		
	// Get the scroller
	var scroller = document.getElementById(scrollerID);
	
	// Ensure we got the scroller
	if(scroller==null)
	{
		return;
	}
	
	// Get the clear div at the end of all our elements
	var clearDiv = document.getElementById(scrollerID+'_clear');
	
	// Init. the scroller's style (position it at 0)
	if(document.layers)
	{
		scroller.style = scroller;
	}

	scroller.style.position = 'relative';
	scroller.style.left     = '0px';
	
	// Scroll from right to left (x pixels at a time)
	window.scrollRLXpx=function()
	{
		var thisScroll       = Math.ceil(lengthToScroll/8);
		scroller.style.left  = parseInt(scroller.style.left) - thisScroll + 'px';
		lengthToScroll      -= thisScroll;
		
		// If the current element is completly visible
		if(lengthToScroll<=0)
		{
			// Hide the last element
			var lastElement = document.getElementById(scrollerID + '[' + (visibleElementNb) + ']');
			lastElement.style.display = 'none';
			
			// Put the last element at the end of the queue
			scroller.removeChild(lastElement);
			scroller.removeChild(clearDiv);
			scroller.appendChild(lastElement);
  			scroller.appendChild(clearDiv);
			
			// Update the scroller's variables
			visibleElementNb    = (visibleElementNb+1)%nbElementsInScroller;
			scroller.style.left = '0px';
			
			// Cancel the next call to scrollRLXpx
			clearInterval(intervalID);
			
			// Update the scroller's state
			isScrolling = 0;
		}
	}

	// Scroll from right to left (one element)
	window.scrollRL=function()
	{	
		// Update the scroller's state
		isScrolling = 1;
		
		// Show the element that will soon be visible
		document.getElementById(scrollerID + '[' + ((visibleElementNb+1)%nbElementsInScroller) + ']').style.display = 'block';	
	
		// Update the scroller's variables
		lengthToScroll = oneScrollLength;
		
		// Call scrollRLXpx at a given interval
		intervalID = setInterval('scrollRLXpx()', 20);	
				
		// Register the scroll to the next element
		timerID = setTimeout("scrollRL()", delayBetweenScrolls);
	}

	// Scroll from left to right (x pixels at a time)
	window.scrollLRXpx=function()
	{
		var thisScroll       = Math.ceil(lengthToScroll/8);
		scroller.style.left  = parseInt(scroller.style.left) + thisScroll + 'px';
		lengthToScroll      -= thisScroll;
		
		// If the current element is completly visible
		if(lengthToScroll<=0)
		{
			// Hide the last element
			var lastElement = document.getElementById(scrollerID + '[' + (visibleElementNb) + ']');
			lastElement.style.display = 'none';
						
			// Update the scroller's variables
			if(visibleElementNb==0)
				visibleElementNb = nbElementsInScroller-1;
			else
				visibleElementNb = visibleElementNb-1;

			scroller.style.left = '0px';
			
			// Cancel the next call to scrollLRXpx
			clearInterval(intervalID);
			
			// Update the scroller's state
			isScrolling = 0;
		}
	}

	// Scroll from left to right (one element)
	window.scrollLR=function()
	{	
		// Update the scroller's state
		isScrolling = 1;

		// Get the element to scroll to
		var nextElement;
		
		if(visibleElementNb==0)
			nextElement = document.getElementById(scrollerID + '[' + (nbElementsInScroller-1) + ']');
		else
			nextElement = document.getElementById(scrollerID + '[' + (visibleElementNb-1) + ']');

		// Move the element to show at the front of the queue (it's currently in last position)
		scroller.removeChild(nextElement);
		scroller.insertBefore(nextElement, document.getElementById(scrollerID + '[' + visibleElementNb + ']'));

		// Update the scroller's variables
		lengthToScroll      = oneScrollLength;
		scroller.style.left = -oneScrollLength + 'px';
		
		// Show the element that will soon be visible
		nextElement.style.display = 'block';
		
		// Call scrollLRXpx at a given interval
		intervalID = setInterval('scrollLRXpx()', 20);	
				
		// Register the scroll to the next element (back to the standard right to left scroll...)
		timerID = setTimeout("scrollRL()", delayBetweenScrolls);
	}	
	
	timerID = setTimeout("scrollRL()", delayBetweenScrolls);
}

// Process a click to scroll from right to left
function manualScrollRL()
{
	// If we are currently scrolling...
	if(isScrolling)
	{
		// Do not process the click
		return;
	}
	
	// Clear the scroll timeout
	clearTimeout(timerID);
	
	// Call the scrollRL function manually
	window.scrollRL();
}

// Process a click to scroll from left to right
function manualScrollLR()
{
	// If we are currently scrolling...
	if(isScrolling)
	{
		// Do not process the click
		return;
	}
	
	// Clear the scroll timeout
	clearTimeout(timerID);
	
	// Call the scrollLR function manually
	window.scrollLR();
}
