var chemin_photos ="images/photos/";
var chemin_vignettes ="images/vignettes/";
var vignettes = new Array;  // tableau des vignettes
var photos = new Array;     // tableau d'images
var tabCreation = new Array;// tableau d'infos sur les créations
var ProduitVisible = 0;     // indexe des produits en cours d'affichage
var menuIntro = 0;          // indexe des produits à afficher au démarage
var couleurTitre = "";
	
// on cherche l'origine du click

function menu_onmousedown(ev)
{
	var clef=(ev.target)? ev.target.id : window.event.srcElement.id; 
	if (document.getElementById(clef).tagName !='A' ) return;
	
	var myLien = document.getElementById(clef).style;
	var myTag = document.getElementsByTagName('A');
	if (couleurTitre == "") couleurTitre = myTag[0].currentStyle.color;
	
	for(i=0;i<myTag.length;i++)
	{	
	 	if (myTag[i].style.color=="bisque") 
		{	
		    //if (myTag[i].parentNode.id=="top_menu") myCouleur=couleurTitre;
		    myTag[i].style.color = couleurTitre;
		    myTag[i].style.backgroundImage = ""; 
		    break;
		}   	
	} 
	myLien.backgroundRepeat="no-repeat";
	myLien.backgroundPosition="left center";
	myLien.backgroundImage="url(decors/PointOrange.gif)"
	myLien.color = "bisque";
	changeInfoImage(clef);
}

// on génère un tableau de liste d'images
function chargement()
{
	var nbPhotos = tabCreation.length;
	var no_menu = 0;
	var ligne=0;

	while (no_menu != nbPhotos)
	{
	 	  myCategorie = tabCreation[no_menu][0];
		  while (tabCreation[no_menu][0] == myCategorie)
		  { 
				var nom_photo = tabCreation[no_menu][3];
				var nom_vignette = nom_photo.substr(0,nom_photo.length-4)+"_MIN.JPG";
		  	 	
                ligne = tabCreation[no_menu][1];
                
				if (!vignettes[myCategorie]) vignettes[myCategorie] = new Array();
                vignettes[myCategorie][ligne] = new Image;		
                vignettes[myCategorie][ligne].src = chemin_vignettes + nom_vignette,50;
                
                if (!photos[myCategorie]) photos[myCategorie] = new Array();
                photos[myCategorie][ligne] = new Image();
                photos[myCategorie][ligne].onLoad = Preload(no_menu,photos[myCategorie][ligne],0);
				photos[myCategorie][ligne].src = chemin_photos + nom_photo;	
                				                	
    			no_menu++;
    			if (no_menu==nbPhotos) break; 	
		 }		 
		 test(myCategorie,0);
	}
}

// alimente tabCreation quand une photo est complètement chargée

function Preload(cle,photo,cpt)
{	
	if (photo.complete)tabCreation[cle][2]=1; 	 
    else {cpt++;if (cpt<1000) setTimeout(function(){Preload(cle,photo,cpt)},1000);else return;}
}

// met en surgras les lignes de menus dont toutes les photos on été chargées

function test(categorie,j)
{		 
 		 var cpt = 0;
 		 var nbP = photos[categorie].length;
		 
		 for(var i=0;i<tabCreation.length;i++)
 	     	{if (tabCreation[i][0]==categorie) break;} 

    	 while (tabCreation[i][0] == categorie)
    	 {
    		if (tabCreation[i][2]==1) cpt++;
    		if (i+1==tabCreation.length) break;
    		i++;
    	 }

		 if (cpt<nbP) 
		 {
		  	j++;
		  	if (j<1000) setTimeout(function(){test(categorie,j)},1000);
			else return;
		 }
		 else 
		 {
		  	//ligneMenu=document.getElementById(categorie);
			//ligneMenu.style.fontWeight="bold";
			
			//couleur="white";
		 	//couleur = (ligneMenu.parentNode.id=="top_menu") ? "#CCFF65" : "white"; 
		 	//ligneMenu.style.color = couleur;
		 	if (menuIntro==0) menuIntro=categorie;
			return;
		 }	 
}



function chargeInfoPhoto(clef)
{	
// PK_menu,i,0,nom_photo_pdt,description_pdt,reference_pdt,prix_pdt,libelle_pdt,texte_argument
	imageArray.length=0;
	var j=0;
	for (i=0;i<tabCreation.length;i++)
	{
		if (tabCreation[i][0]==clef) 
		{	
			imageArray[j] = new Array();
			imageArray[j]['credit'] = tabCreation[i][7];
			imageArray[j]['reference']= tabCreation[i][5];
			imageArray[j]['caption'] = tabCreation[i][4];
			imageArray[j]['arguments'] = tabCreation[i][8];
			imageArray[j]['prix']= tabCreation[i][6];
			imageArray[j]['link'] = "<A href='"+photos[clef][j].src+"' rel='lightbox' title='"+tabCreation[i][6]+"'></A>";
			j++;
		}
	}
}

// --------------------- Affecte le nouveau tableau d'images à Frog -------------------------------------------

function changeInfoImage(clef,j)
{	
	if (clef > 0)
	{
    	var cpt = 0;
    	for (i=0;i<tabCreation.length;i++)
    	{	if (tabCreation[i][0]==clef) cpt++;}
    		
    	if (cpt>0) // on test si la famille de produits existe
    	{
    		// on regarde si c'est la meme clef qui est affiché actuellement sinon on l'afiche		
    		if (clef!=ProduitVisible)
    		{  
    			chargeInfoPhoto(clef);
    			ProduitVisible = clef; 
    			myFrog.depart(clef);
    		}
		}	
	}
	else    // si on ne connait pas un menu actif on rappel la fonction 5 secondes
	{ 	
	 	j++;
		if (j<5000) setTimeout(function(){changeInfoImage(menuIntro,j)},1000); 
		else alert('Il n\'y pas de créations dans la Base de données !\n\n site en cours de création');
	}		
}	

// --------- Initialisation des données ----------------------------

function initialisation()
{	
 	if (tabCreation.length > 0) // si il y à des infos dans le tableau
	{  
	   chargement();
	   initFrog();
   	   changeInfoImage(menuIntro,0);
	}   
	else alert ('Il n\'y pas de créations dans la Base de données !\n\n site en cours de création');
	
}