var currentSection = "bloc1-scroll"; // Section par défaut affichée

// Défilement en utilisant les flèches
function ScrollArrow(direction, toolbar, scrollArea, offset,paneTag,tabTag) {
	//récupère une référence sur l'élément toobar.
	toolbarElem = document.getElementById(toolbar);
	toolbarNames = new Array();
	
	//Trouve tous les éléments LI dont dans  toolbarElem
	if (toolbarElem.hasChildNodes())
	{
		var children = toolbarElem.childNodes;
		for (var i = 0; i < children.length; i++) 
		{
			if ((toolbarElem.childNodes[i].tagName == "LI")|| (toolbarElem.childNodes[i].tagName == "DIV")) {
				//ajout d'un élément dans le tableaux des tools. 
				//On ne récupère que la première partie de l'id. Cela permettra de retrouver la section à faire défiler.
				toolbarNames.push(toolbarElem.childNodes[i].id.split("-")[0]);
			}
		}
	}
	
	
	
	//Maintenant, on cherche quelle section est en cours. afin de savoir où on doit aller.
	for (var i = 0; i < toolbarNames.length; i++) {
		if (toolbarNames[i] == currentSection.split("-")[0]) {
			if (direction == "left") 
			{
				if (i - 1 < 0)
					gotoTab = toolbarNames[toolbarNames.length - 1];
				else
					gotoTab = toolbarNames[i - 1];
			} 
			else 
			{
				if ((i + 1) > (toolbarNames.length - 1))
					gotoTab = toolbarNames[0];
				else
					gotoTab = toolbarNames[i + 1];
			}
		}
	}
	//A plus qu'a scroller.
	if (arguments.length==6)
		ScrollSection(gotoTab+paneTag, scrollArea, offset,tabTag);
	else
		ScrollSection(gotoTab+paneTag, scrollArea, offset);

}



function ScrollSection(link, scrollArea, offset,tabTag)
{
	
		if (currentSection == link) {
			return;
		}
		
		lastSection = currentSection;
		//on change la section courante.
		currentSection = link;
		
	if (arguments.length==4){
		// Change the section highlight.
		// Extract the root section name, and use that to change the background image to 'top', revealing the alt. state
		sectionTab = currentSection.split("-")[0] + tabTag;
		
		document.getElementById(sectionTab).className = "active";
		if (lastSection) {
			lastTab = lastSection.split("-")[0] + tabTag;
			document.getElementById(lastTab).className = "inactive";
		}
    }
	// Get the element we want to scroll, get the position of the element to scroll to
	
	theScroll = document.getElementById(scrollArea);
	position = findElementPos(document.getElementById(link));

	// Get the position of the offset div -- the div at the far left.
	// This is the amount we compensate for when scrolling
	
	if (offset != "") {
		offsetPos = findElementPos(document.getElementById(offset));
		position[0] = position[0] - offsetPos[0];
	}

	scrollStart(theScroll, theScroll.scrollLeft, position[0], "horiz");
	// return false;
}




//
// Animated Scroll Functions
// Scrolls are synchronous -- only one at a time.
//

var scrollanim = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};


function scrollStart(elem, start, end, direction)
{
	//console.log("scrollStart from "+start+" to "+end+" in direction "+direction);

	if (scrollanim.timer != null) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
	}
	scrollanim.time = 0;
	scrollanim.begin = start;
	scrollanim.change = end - start;
	scrollanim.duration = 25;
	scrollanim.element = elem;
	
	if (direction == "horiz") {
		scrollanim.timer = setInterval("scrollHorizAnim();", 15);
	}
	else {
		scrollanim.timer = setInterval("scrollVertAnim();", 15);
	}
}

function scrollVertAnim()
{
	if (scrollanim.time > scrollanim.duration) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
	}
	else {
		move = sineInOut(scrollanim.time, scrollanim.begin, scrollanim.change, scrollanim.duration);
		scrollanim.element.scrollTop = move; 
		scrollanim.time++;
	}
}

function scrollHorizAnim()
{
	if (scrollanim.time > scrollanim.duration) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
	}
	else {
		move = sineInOut(scrollanim.time, scrollanim.begin, scrollanim.change, scrollanim.duration);
		scrollanim.element.scrollLeft = move;
		scrollanim.time++;
	}
}

//Petit Scroll sans gestion de flèches ni toolbar
function ScrollLigth(link, scrollArea, offset)
{
	theScroll = document.getElementById(scrollArea);
	position = findElementPos(document.getElementById(link));

	// Get the position of the offset div -- the div at the far left.
	// This is the amount we compensate for when scrolling
	
	if (offset != "") {
		offsetPos = findElementPos(document.getElementById(offset));
		position[1] = position[1] - offsetPos[1];
	}
	//debugger;
	ScrollLigthStart(theScroll, theScroll.scrollTop, position[1], "vertical");
	// return false;
}

var scrolllightanim = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};

function ScrollLigthStart(elem, start, end, direction)
{
	if (scrolllightanim.timer != null) {
		clearInterval(scrolllightanim.timer);
		scrolllightanim.timer = null;
	}
	scrolllightanim.time = 0;
	scrolllightanim.begin = start;
	scrolllightanim.change = end - start;
	scrolllightanim.duration = 25;
	scrolllightanim.element = elem;
	
	if (direction == "horiz") {
		scrolllightanim.timer = setInterval("scrollLHorizAnim();", 15);
	}
	else {
		scrolllightanim.timer = setInterval("scrollLVertAnim();", 15);
	}
}

function scrollLHorizAnim()
{
	if (scrolllightanim.time > scrolllightanim.duration) {
		clearInterval(scrolllightanim.timer);
		scrolllightanim.timer = null;
	}
	else {
		move = sineInOut(scrolllightanim.time, scrolllightanim.begin, scrolllightanim.change, scrolllightanim.duration);
		scrolllightanim.element.scrollLeft = move;
		scrolllightanim.time++;
	}
}

function scrollLVertAnim()
{
	if (scrolllightanim.time > scrolllightanim.duration) {
		clearInterval(scrolllightanim.timer);
		scrolllightanim.timer = null;
	}
	else {
		move = sineInOut(scrolllightanim.time, scrolllightanim.begin, scrolllightanim.change, scrolllightanim.duration);
		scrolllightanim.element.scrollTop = move; 
		scrolllightanim.time++;
	}
}
//
// LARGE POPUP: Full-Screen Pop-up Functions
//

function findElementPos(elemFind)
{
    var elemX = 0;
    var elemY = 0;
    do {
        elemX += elemFind.offsetLeft;
        elemY += elemFind.offsetTop;
    } while ( elemFind = elemFind.offsetParent )
 
    return Array(elemX, elemY);
}
function sineInOut(t, b, c, d)
{
    return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
}

