var position =0;
var nbElem=0;
var afficheMax=3;
var current=0;
var visib="none";
function afficheImage(elem,cur) {
	document.getElementById("td"+current).setAttribute("border",0);
	current=cur;
	document.getElementById("td"+cur).setAttribute("border",1);
	document.getElementById("imagePrinc").innerHTML="<img src='"+elem+".jpg'>";
}
function afficheElem(courant) {
	var valeur = document.getElementById(courant).src;
	var image  = valeur.replace("thb/thb_","med/med_");
	document.getElementById("imagePrinc").innerHTML="<img src='"+image+".jpg'>";

}
function affichePict(dir,pict,i) {
    document.getElementById("imagePrinc").innerHTML="<img src='"+dir+"/med/med_"+pict+".jpg'>";
}
function arriere() {
	if (position == 0) return;
	position--;
	if (position+afficheMax < nbElem)
		document.getElementById("tdAvancer").style.visibility="visible";
	if (position == 0) 		{
		 document.getElementById("tdReculer").style.visibility="hidden";
	}
	var pos = position+afficheMax;
	if (pos<nbElem){
	    document.getElementById("td"+position).style.display=visib;
	    document.getElementById("td"+pos).style.display     ="none";
	}
}
function avant() {
    if (position == nbElem)	return;
    var pos=position;
    position++;
    if (position > 0) {
	document.getElementById("tdReculer").style.visibility="visible";	
    }
    var pos2 = pos+afficheMax;
    if (pos2 >= nbElem-1) 
	document.getElementById("tdAvancer").style.visibility="hidden";
    if (pos2>=nbElem) 	return;
    document.getElementById("td"+pos).style.display="none";
    document.getElementById("td"+pos2).style.display=visib;
}
// Faite pour afficher les boutons avant arriere
function insertTD(tableBody, link, value, nodeAfter) {
    var tdBouton = document.createElement("td");
    var aImg	 = document.createElement("a");
    aImg.href	 = '#';
    aImg.setAttribute("onclick",link);
    aImg.appendChild(document.createTextNode(value));
    tdBouton.appendChild(aImg);
    tdBouton.setAttribute("id",link);
    if (nodeAfter == null)	tableBody.appendChild(tdBouton);
    else		 	tableBody.insertBefore(tdBouton,nodeAfter);
}
// Fonction servant a extraire les informations du lien statique
function getInfoFromUrlToTab(url) {
    var slash = url.lastIndexOf("/")+1;
    var extension = url.lastIndexOf(".html");
    var tab = url.substr(slash,extension-slash).split('-');
    var dest = {"nameDir":tab[1],"currentDir":tab[2],"currentPosition":tab[3],"currentPict":tab[4]};
    return dest;
}
function convertRefToClick(id, num, fonc) {
    var lien  = document.getElementById(id);
    var tab   = getInfoFromUrlToTab(lien.href);
    lien.href = "#";
    var cmd   =  fonc+"('./rep/"+tab['nameDir']+"/"+tab['currentDir']+"','"+tab['currentPict']+"',"+num+");";
    if( lien.attachEvent ) lien.onclick = function() {affichePict('./rep/'+tab['nameDir']+"/"+tab['currentDir'],tab['currentPict'],num);};
    else                   lien.setAttribute('onclick', cmd); 
}
// Image, nombre d'images en tout nombre d'images a afficher, position courante
function fin(current, numElem, nbImg, currentPosition) {
    visib = (!window.XMLHttpRequest && window.ActiveXObject)?"block":"table-cell";
    position = currentPosition;
    nbElem = numElem;
    afficheMax = nbImg;
    var visible = position+afficheMax;
    // Conversion du mode statique au mode javascript des liens des images
    for (var i=0;i<nbElem;i++){
	convertRefToClick("a"+i,i,"affichePict");
    }
    // affichage de la photo
    afficheElem("image"+position);
 
    // Gestion des boutons de déplacement dans les vignettes 
    var lien = document.getElementById("reculer");
    lien.href="#";
    if( lien.attachEvent )
	lien.onclick = function() { arriere();};
    else lien.setAttribute('onclick', "arriere()"); 
    lien = document.getElementById("avancer");
    lien.href="#";
    if (lien.attachEvent)
	lien.onclick = function(){ avant();};
    else lien.setAttribute('onclick', "avant()"); 
}
