
var couleur_fond_lettre_utilisee = "#86AEC7";
var couleur_fond_lettre_onmouseover = "#86AEC7";

var repertoire_images = '';
var partie_pendue_en_cours = 1;
var partie_finie = false;

function genere_pendu(mot, chemin_images)
{
	var lettre_a_trouver = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	repertoire_images = chemin_images;

	document.write("<TABLE STYLE='width:520px' background='"+repertoire_images+"/fond.gif' CELLPADDING='0' CELLSPACING='0'>");
	document.write("	<TR>");
	document.write("		<TD STYLE='width:260px;height:260px;border=1px;' VALIGN='top'>");
	document.write("			<DIV STYLE='position:relative;top:0px;left:0px'>");
	document.write("				<IMG id='part_pendu_2' SRC='"+chemin_images+"/pot_2.gif' STYLE='position:absolute;top:0px;left:0px;display:none' ALT=''>");
	document.write("				<IMG id='part_pendu_1' SRC='"+chemin_images+"/pot_1.gif' STYLE='position:absolute;top:0px;left:0px;display:none' ALT=''>");
	document.write("				<IMG id='part_pendu_3' SRC='"+chemin_images+"/pot_3.gif' STYLE='position:absolute;top:0px;left:0px;display:none' ALT=''>");
	document.write("				<IMG id='part_pendu_4' SRC='"+chemin_images+"/corde.gif' STYLE='position:absolute;top:0px;left:0px;display:none' ALT=''>");
	document.write("				<IMG id='part_pendu_5' SRC='"+chemin_images+"/tete.gif' STYLE='position:absolute;top:0px;left:0px;display:none' ALT=''>");
	document.write("				<IMG id='part_pendu_6' SRC='"+chemin_images+"/corps.gif' STYLE='position:absolute;top:0px;left:0px;display:none' ALT=''>");
	document.write("				<IMG id='part_pendu_7' SRC='"+chemin_images+"/bras_g.gif' STYLE='position:absolute;top:0px;left:0px;display:none' ALT=''>");
	document.write("				<IMG id='part_pendu_8' SRC='"+chemin_images+"/bras_d.gif' STYLE='position:absolute;top:0px;left:0px;display:none' ALT=''>");
	document.write("				<IMG id='part_pendu_9' SRC='"+chemin_images+"/jambe_g.gif' STYLE='position:absolute;top:0px;left:0px;display:none' ALT=''>");
	document.write("				<IMG id='part_pendu_10' SRC='"+chemin_images+"/jambe_d.gif' STYLE='position:absolute;top:0px;left:0px;display:none' ALT=''>");

	document.write("			</DIV>");

	document.write("		</TD>");
	document.write("		<TD STYLE='width:260px;left:260px;text-align:center'>");
	
	
	//affichage des mots
	var tab_mots = mot.split(" ");
	var current_x_pos = 0;
	var cpt=0;
	for(var i = 0 ; i < tab_mots.length ; i++)
	{
		if(current_x_pos + tab_mots[i].length*20 > 260)
		{
			document.write("<BR>");
			current_x_pos = 0;
		}
		for(var j = 0 ; j < tab_mots[i].length ; j++)
		{
			var mot = tab_mots[i];
			document.write("<IMG ID='pendu_lettre_"+cpt+"' _valeur='"+rot13(mot.charAt(j))+"' _trouvee='non' SRC='"+chemin_images+"/tiret.gif'>");
			current_x_pos += 20;
			cpt++
		}
		document.write("<IMG SRC='"+chemin_images+"/space.gif'>");
		current_x_pos += 20;
	}
	document.write("		<BR><BR><BR>");
	document.write("<SPAN id='span_rejouer' STYLE='display:none;cursor:hand;cursor:pointer' ONCLICK='document.location.reload();'>");
	document.write("<IMG SRC='"+chemin_images+"/R.gif' ALT='R' STYLE='background-color:#FFFFFF;'>");
	document.write("<IMG SRC='"+chemin_images+"/E.gif' ALT='E' STYLE='background-color:#FFFFFF;'>");
	document.write("<IMG SRC='"+chemin_images+"/J.gif' ALT='J' STYLE='background-color:#FFFFFF;'>");
	document.write("<IMG SRC='"+chemin_images+"/O.gif' ALT='O' STYLE='background-color:#FFFFFF;'>");
	document.write("<IMG SRC='"+chemin_images+"/U.gif' ALT='U' STYLE='background-color:#FFFFFF;'>");
	document.write("<IMG SRC='"+chemin_images+"/E.gif' ALT='E' STYLE='background-color:#FFFFFF;'>");
	document.write("<IMG SRC='"+chemin_images+"/R.gif' ALT='R' STYLE='background-color:#FFFFFF;'>");
	document.write("</SPAN>");

	document.write("		</TD>");
	document.write("	</TR>");
	//affichage de l'alphabet
	document.write("	<TR>");
	document.write("		<TD COLSPAN='2'>");
	for(var i = 0 ; i < lettre_a_trouver.length ; i++)
	{
		document.write("<IMG SRC='"+chemin_images+"/"+lettre_a_trouver.charAt(i)+".gif' ALT='' STYLE='cursor:pointer;cursor:hand;' ONCLICK=\"this.style.backgroundColor='"+couleur_fond_lettre_utilisee+"';decache_lettre('"+lettre_a_trouver.charAt(i)+"')\">");
	}
	document.write("		</TD>");
	document.write("	</TR>");
	document.write("</TABLE>");
}

function decache_lettre(lettre)
{
	if(partie_finie) { return; }

	var cpt=0;
	var lettre_trouvee=false;
	var reste_des_lettres = false;
	while(document.getElementById('pendu_lettre_'+cpt))
	{
		var tiret = document.getElementById('pendu_lettre_'+cpt);
		if(tiret.getAttribute('_valeur').toLowerCase() == lettre.toLowerCase())
		{
			tiret.src = repertoire_images+"/"+lettre.toUpperCase()+".gif";
			lettre_trouvee=true;
			tiret.setAttribute('_trouvee', 'oui');
		}
		if(tiret.getAttribute('_trouvee') == 'non')
		{
			reste_des_lettres = true;
		}
		cpt++;
	}
	if(!reste_des_lettres)
	{
		partie_finie = true;
		document.getElementById('span_rejouer').style.display='block';
		alert('gagne !!!');
	}
	if(!lettre_trouvee)
	{
		document.getElementById('part_pendu_'+partie_pendue_en_cours).style.display='block';
		partie_pendue_en_cours++;
		if(partie_pendue_en_cours > 10)
		{
			partie_finie = true;
			document.getElementById('span_rejouer').style.display='block';
			alert('perdu !!!');
			decache_tout();
		}
	}
}

function decache_tout()
{
	var cpt=0;
	while(document.getElementById('pendu_lettre_'+cpt))
	{
		var tiret = document.getElementById('pendu_lettre_'+cpt);
		tiret.src = repertoire_images+"/"+tiret.getAttribute('_valeur').toUpperCase()+".gif";
		tiret.setAttribute('_trouvee', 'oui');
		cpt++;
	}
}

function rot13(lettre)
{
	var liste_lettres = "abcdefghijklmnopqrstuvwxyz";
	var position_lettre = liste_lettres.indexOf(lettre.toLowerCase());
	if(position_lettre >= 0)
	{
		return liste_lettres.charAt((position_lettre+13)%26);
	}
	else
	{
		return lettre;
	}
}
