// JavaScript Document

window.onload = gererPhotos;
var i;
var ULphotosAccueil;
var debut;
var fin;
var largeur = 490;
var position;
var noPhoto;
var photoPrec;
var avance;
var timer;
var tabLiNavPhotos;

function gererPhotos(){
	

/*	
	rendre navphoto dynamique en se basant sur ulphotosintruments.
	var UlPhotos = document.getElementById("photosinstruments");
	tabLiPhotos = UlPhotos.getElementsByTagName("li");
*/	

	ULphotosAccueil = document.getElementById("photosinstruments");
	tabLiNavPhotos = document.getElementById("navphotos").getElementsByTagName("li");
	var index;
	for (i = 0, n = tabLiNavPhotos.length; i < n; i++)
	{
		tabLiNavPhotos[i].index = i;
		tabLiNavPhotos[i].onclick =  function() {changePhoto(this.index);window.clearTimeout(timer);};
	}
	animation(0);
}

function animation(index){
		changePhoto(index);
		if (index < tabLiNavPhotos.length-1){
			timer = window.setTimeout(function() {
				//alert(index);
				animation(tabLiNavPhotos[index].index);
				}, 6000);
			
		}else{
			//la dernière photo...
			if (index = tabLiNavPhotos.length){
				timer = window.setTimeout(function() {
					changePhoto(0);
					}, 7000);
				}
			}
		index++;
}

function changePhoto(index){
	for (i = 0, n = tabLiNavPhotos.length; i < n; i++)
		{
			tabLiNavPhotos[i].className="";
		}
	tabLiNavPhotos[index].className="actif";
	noPhoto = index;
	if (isNaN(parseInt(ULphotosAccueil.style.marginLeft))) {
		debut = 0;
	} else {
		debut = parseInt(ULphotosAccueil.style.marginLeft);
	}
	position = debut;
	fin = 0 - (largeur * (noPhoto));
	if (debut > fin) {
		avance = 0; /*recule*/
	} else {
		avance = 1; /*avance*/
	}
	slide();
}

function slide() {
		var acceleration = 0.08;
		
		if (position == debut) {
			position = position - 1;
		}

		if (!avance) {
			if (position <= (fin + 2)) {
				position = fin;
			} else {
				position = position - ((position - fin ) * acceleration);
			}
		} else {
			if (position >= (fin - 2)) {
				position = fin;
			} else {
				position = position + ((fin - position) * acceleration);
			}
		}

		document.getElementById("photosinstruments").style.marginLeft = position + 'px';
		
		if (position != fin ) {
			window.setTimeout('slide()', 30);
		}
	}

