var dep=0;
var t=0;
var couleur_base='#404550';
var couleur_selec='#FF99CC';
var couleur_fond='#404550';
var couleur_def;
var couleur_texte_base='#EEEEFF';
var couleur_texte_selec='#EEEEFF';
var nbre_coul=new Array();
var ordre=new Array(); 
var bloque=new Array(n*n);
for (i=0; i<n*n; i++) {bloque[i]=0;}
var felicite='C\'est la bonne solution.  Bravo !';
var felicite2="Non ! Ce n\'est pas le bon mot.";   

var mots=new Array();
var mots2=new Array();
var mots_base2=new Array();
for (i=0; i<mots_base.length; i++) {mots2[i]=new String(); for (j=0; j<mots_base[i].length; j++) {if (mots_base[i].charAt(j)!="-") {mots2[i]+=mots_base[i].charAt(j);}}}

var controle=new Array();
for (i=0; i<mots2.length; i++) {controle[i]=0; mots[i]=new String(); for (j=0; j<mots2[i].length-1; j++){mots[i]+=mots2[i].charAt(j);}
									nbre_coul[i]=mots2[i].charAt(mots2[i].length-1);}

for (i=0; i<mots_base.length; i++) {mots_base2[i]=new String(); for (j=0; j<mots_base[i].length-1; j++){mots_base2[i]+=mots_base[i].charAt(j);}}


function attribue_coul(a)
{if (nbre_coul[a]==0) {couleur_def='#ABABAB';}
if (nbre_coul[a]==1) {couleur_def='#D2B48C';}
if (nbre_coul[a]==2) {couleur_def='#EF7E7E';}
if (nbre_coul[a]==3) {couleur_def='#778899';}

}


//============================================================
//                >> jsImagePlayer 1.0 << 
//            for Netscape3.0+, September 1996
//============================================================
//                  by (c)BASTaRT 1996
//             Praha, Czech Republic, Europe
//
image_name = "im";  //the base "path/name" of the image set without the numbers
image_type = "gif";                 //"gif" or "jpg" or whatever your browser can display

first_image = 0;                    //first image number
last_image = 8;                    //last image number

   //!!! the size is very important - if incorrect, browser tries to 
   //!!! resize the images and slows down significantly
animation_height = 320;              //height of the images in the animation
animation_width = 600;               //width of the images in the animation
//**************************************************************************
//**************************************************************************




theImages = new Array();
normal_delay = 600;  
delay = normal_delay;  //delay between frames in 1/100 seconds
delay_step = 10;
delay_max = 4000;
delay_min = 10; 
current_image = first_image;     //number of the current image
timeID = null;
status = 0;            // 0-stopped, 1-playing
play_mode = 1;         // 0-normal, 1-loop, 2-swing
size_valid = 0;

if (first_image > last_image) 
{
   var help = last_image;
   last_image = first_image;
   first_image = help;
};

for (var i = first_image; i <= last_image; i++) 
{
   theImages[i] = new Image();
   //theImages[i].onerror = my_alert("\nError loading ",image_name,i,image_type,"!");
   //theImages[i].onabort = my_alert("\nLoading of ",image_name,i,image_type," aborted!");
   theImages[i].src = image_name + i + "." + image_type;
};



function animate_fwd() 
{
   current_image++;
   if(current_image > last_image)
   { 
      if (play_mode == 0) 
      {
         current_image = last_image; 
         status=0;
         return;
      };                           //NORMAL
      if (play_mode == 1) 
      {
         current_image = first_image; //LOOP
      };
      if (play_mode == 2) 
      {
         current_image = last_image; 
         animate_rev();
         return; 
      };
   };                             
   document.animation.src = theImages[current_image].src;
   timeID = setTimeout("animate_fwd()", delay);
}


function animate_rev() 
{
   current_image--;
   if(current_image < first_image)
   { 
      if (play_mode == 0) 
      {
         current_image = first_image; 
         status=0;
         return;
      };                           //NORMAL
      if (play_mode == 1) 
      {
         current_image = last_image; //LOOP
      };
      if (play_mode == 2) 
      {
         current_image = first_image; 
         animate_fwd();
         return; 
      };
   };                             
   document.animation.src = theImages[current_image].src;
 
   timeID = setTimeout("animate_rev()", delay);
}

function change_speed(dv) 
{
   delay+=dv;
   if(delay > delay_max) delay = delay_max;
   if(delay < delay_min) delay = delay_min;
}

//===> stop the movie
function stop() 
{
   if (status == 1) clearTimeout (timeID);
   status = 0;
}

function fwd() 
{
   stop();
   status = 1;
   animate_fwd();
}

function go2image(number)
{
   stop();
   if (number > last_image) number = last_image;
   if (number < first_image) number = first_image;
   current_image = number;
   document.animation.src = theImages[current_image].src;
   
}

function rev() 
{
   stop();
   status = 1;
   animate_rev();
}

function change_mode(mode) 
{
   play_mode = mode;
}

function launch() 
{
   stop();
   current_image = first_image;
   document.animation.src = theImages[current_image].src;
   
   // this is trying to set the text (Value property) on the START and END buttons 
   // to S(first_image number), E(last_image number). It's supposed (according to 
   // JavaScrtipt Authoring Guide) to be a read only value but for some reason
   // it works on win3.11 (on IRIX it doesn't).
   document.control_form.start_but.value = " S(" + first_image + ") ";
   document.control_form.end_but.value = " E(" + last_image + ") ";
   // this needs to be done to set the right mode when the page is manualy reloaded
   change_mode (document.control_form.play_mode_selection.selectedIndex);
}

function animation()
{
   document.write(" <P><IMG NAME=\"animation\" SRC=\"im0.gif\" HEIGHT=",animation_height, " WIDTH=", animation_width, "\" ALT=\"[jsMoviePlayer]\">");
   document.write(" <FORM Method=POST Name=\"control_form\"> ");
   
   
   document.write(" </FORM> ");
};


function precede()
{
	var grille_av=new String();
	grille_av+="puzzle"+(num_grille-1)+".htm";
	if (precedent==1) {document.getElementById('im_pre2').href=grille_av;  document.getElementById('pre2').href=grille_av; document.getElementById('im_pre2').innerHTML="\<img align=\"absmiddle\" src=\"gauche.gif\"  border=\"0\"\>"; document.getElementById('pre2').innerHTML="pr&eacute;c&eacute;dent"; }
}

function suit()
{
	var grille_ap=new String();
	grille_ap+="puzzle"+(num_grille+1)+".htm";
	if (suivant==1) {document.getElementById('im_apr2').href=grille_ap; document.getElementById('apr2').href=grille_ap; document.getElementById('apr2').innerHTML="suivant&nbsp;"; document.getElementById('im_apr2').innerHTML="\<img align=\"absmiddle\" src=\"droite.gif\"  border=\"0\"\>";}
}


function niveau()
{
if (a==0) {
		  document.getElementById('def').innerHTML=normal; document.getElementById('niveau').innerHTML='<font color="#EEEEFF" face="Arial" style="font-size: 13px">&gt;  Version simplifi&eacute;e<font color="#EEEEFF" face="Arial" style="font-size: 13px"></font>'; a=1; efface();}
else {document.getElementById('def').innerHTML=simple; document.getElementById('niveau').innerHTML='<font color="#EEEEFF" face="Arial" style="font-size: 13px">&gt;  Autre version<font color="#EEEEFF" face="Arial" style="font-size: 13px"></font>';a=0; efface();}
}



function donne_calque(a)
	{ var tampon=new String();
		 tampon+=a;
	if (document.getElementById) 
						{document.getElementById(tampon).style.visibility="visible";}
						
	else {alert("D&eacute;sol&eacute;, ce scritpt n&eacute;cessite un navigateur plus r&eacute;cent"); return;}
	}


function enleve_calque(a)
	{ var tampon=new String();
		 tampon+=a;
	if (document.getElementById) 
						{document.getElementById(tampon).style.visibility="hidden";}
						
	else {alert("D&eacute;sol&eacute;, ce scritpt n&eacute;cessite un navigateur plus r&eacute;cent"); return;}
}


function FondCell(a,b){
var nom=new String();
nom+='A'+a+b;
document.getElementById(nom).style.background=couleur_selec;
}





function constr(a)
{

if (dep==0) {return;}
var nom=new String();
nom+='A'+a;
if (bloque[a]==1) {return;}

if (ordre[t-2]==a) {var nom2=new String(); nom2+='A'+ordre[t-1]; document.getElementById(nom2).style.background=couleur_fond; var ordre2=new Array(); for (k=0; k<t-1; k++) {ordre2[k]=ordre[k];}
			ordre=new Array(); for (k=0; k<t-1; k++) {ordre[k]=ordre2[k];} t=t-1; 
			var encoursnew=new String(); for (k=0; k<t; k++) {encoursnew+=texte.charAt(ordre[k]);}
			document.getElementById('encours').innerHTML=encoursnew; return;}

if (t!=0 && !(ordre[t-1]==a-1 || ordre[t-1]==a+1 || ordre[t-1]==a-n || ordre[t-1]==a+n)) {return;}
for (i=0; i<ordre.length; i++) {if (a==ordre[i]) {return;}}
ordre[t]=a;

document.getElementById(nom).style.background=couleur_selec;
document.getElementById(nom).style.color=couleur_texte_selec;

document.getElementById('encours').innerHTML+=texte.charAt(a);;


t+=1;
precede=a;
return t



}

function conversion(a)
{	

	var converti=new String();
	
	switch(a) {
	 case 'â': converti+='A'; break;
	 case 'Â': converti+='A'; break;
	 case 'á': converti+='A'; break;
	 case 'Á': converti+='A'; break;
	 case 'à': converti+='A'; break;
	 case 'À': converti+='A'; break;
	 case 'ã': converti+='A'; break;
	 case 'Ã': converti+='A'; break;
	 case 'é': converti+='E'; break;
	 case 'É': converti+='E'; break;
	 case 'è': converti+='E'; break;
	 case 'È': converti+='E'; break;
	 case 'ê': converti+='E'; break;
	 case 'Ê': converti+='E'; break;
	 case 'ë': converti+='E'; break;
	 case 'Ë': converti+='E'; break;
	 case 'ç': converti+='C'; break;
	 case 'Ç': converti+='C'; break;
	 case 'í': converti+='I;'; break;
	 case 'Í': converti+='I'; break;
	 case 'ì': converti+='I'; break;
	 case 'Ì': converti+='I'; break;
	 case 'î': converti+='I'; break;
	 case 'Î': converti+='I'; break;
	 case 'ï': converti+='I'; break;
	 case 'Ï': converti+='I'; break;
	 case 'ô': converti+='O'; break;
	 case 'Ô': converti+='O'; break;
	 case 'ö': converti+='O'; break;
	 case 'Ö': converti+='O'; break;
	 case 'ù': converti+='U'; break;
	 case 'Ù': converti+='U'; break;
	 case 'û': converti+='U'; break;
	 case 'Û': converti+='U'; break;
	 case 'ü': converti+='U'; break;
	 case 'Ü': converti+='O'; break;
	 
	default: converti+=a; break;}
	
return converti;
}


function valide(a)
{
	if (bloque[a]==1) {return;}
if (dep==0){document.getElementById('encours').innerHTML=''; dep=1; constr(a); return}
var k=0;
var mot=new String();
for (i=0; i<ordre.length; i++) {mot+=texte.charAt(ordre[i]); }
for (i=0; i<mots.length; i++) {if (mot==mots[i]) {attribue_coul(i);  controle[i]=1; var nom2=new String(); nom2+='B'+i; document.getElementById(nom2).style.background=couleur_def;  for (j=0; j<ordre.length; j++) {nom2=new String(); nom2+='A'+ordre[j]; document.getElementById(nom2).style.color=couleur_texte_base; document.getElementById(nom2).style.background=couleur_def; bloque[ordre[j]]=1; k=1;}} }
if (k==0) {for (j=0; j<ordre.length; j++) {var nom2=new String(); nom2+='A'+ordre[j]; document.getElementById(nom2).style.color=couleur_texte_base; document.getElementById(nom2).style.background=couleur_fond; }document.getElementById('encours').innerHTML+=' n\'est pas dans la liste des mots &agrave; trouver !';}

ordre=new Array;
t=0;
dep=0;


}

function donne_calque(a)
	{ var tampon=new String();
		 tampon+=a;
	if (document.getElementById) 
						{document.getElementById(tampon).style.visibility="visible";}
						
	else {alert("D&eacute;sol&eacute;, ce scritpt n&eacute;cessite un navigateur plus r&eacute;cent"); return;}
	}


function enleve_calque(a)
	{ var tampon=new String();
		 tampon+=a;
	if (document.getElementById) 
						{document.getElementById(tampon).style.visibility="hidden";}
						
	else {alert("D&eacute;sol&eacute;, ce scritpt n&eacute;cessite un navigateur plus r&eacute;cent"); return;}
}

function valide2()
{
	if (window.document.jeu.mys.value=='') {return;}
	
	var test=window.document.jeu.mys.value;
	var test2=new String();
	for (i=0; i<test.length; i++) {if(test.charAt(i)!=' ' && test.charAt(i)!="'" && test.charAt(i)!='-'){test2+=conversion(test.charAt(i)).toUpperCase();}}
	if (test2==mot_mys) {document.getElementById('encours').innerHTML=felicite;}
else {document.getElementById('encours').innerHTML=felicite2;}

}
	
function efface()
{
for (i=0; i<n*n; i++) {document.getElementById('A'+i).style.background=couleur_fond; bloque[i]=0;}
for (i=0; i<mots.length; i++) {controle[i]=0; document.getElementById('B'+i).style.background=couleur_base;}
dep=0; t=0;
document.getElementById('encours').innerHTML='';
}

function grille()

{	
	for (i=0; i<n; i++) {	window.document.write("<tr>");
    						
    			for (j=0; j<n; j++) 
    					{
    					
    					 window.document.write("<a class=\"blanc\" href=\"javascript:{};\"> ");
    					window.document.write("<td id=\"A");
    					 window.document.write(n*i+j);
    					 window.document.write("\" onmouseover=\"javascript:constr(");
    					 window.document.write(n*i+j);
    					  window.document.write("); \"onClick=\"javascript:valide(");
						 window.document.write(n*i+j);
    					 window.document.write(");\" align=\"center\"  width=\"24\" height=\"26\"style=\"font-family:arial; font-size:12px; border: 1px solid #EEEEFF; color:");
    					 window.document.write(couleur_texte_base);
    					 window.document.write("; padding-left: 4; padding-right: 4; padding-top:4; padding-bottom:1; background-color:");
    					 window.document.write(couleur_base);
    					 window.document.write("\" >");
    					 
    					 window.document.write(texte.charAt(n*i+j));
    					 window.document.write("</td></a>");
    					 }
    					 window.document.writeln("</tr>");
    					 }

}

