var isFr = false;
if (document.URL.indexOf('_eng')<0) {isFr = true;}

var image0 = new Image();
image0.src = "solitaire/0.jpg";
var image1 = new Image();
image1.src = "solitaire/1.jpg"; //bleu
var image2 = new Image();
image2.src = "solitaire/2.jpg"; //rouge
var image3 = new Image();
image3.src = "solitaire/3.jpg";

var boule = new Array (
	"13", "14", "15",
	"22", "23", "24", "25", "26",
	"31", "32", "33", "34", "35", "36", "37",
	"41", "42", "43", "44", "45", "46", "47",
	"51", "52", "53", "54", "55", "56", "57",
	"62", "63", "64", "65", "66",
	"73", "74", "75");

var bouleBarre = "";
for (var i=0; i<boule.length; i++)
{
bouleBarre += "|" + boule[i] + "|";
}

function boite(leNom, on)
{
	var lasrc = eval("image"+on+".src");
	var uneboite = '<td><a href="javascript:jeclick(' + leNom  
	+ ');"><img name="' + leNom + '" src=' + lasrc + ' width="40" height="40" alt="' + on + '" /><\/a><\/td>';
	
	return uneboite;
}

var laregle = "Jump over the next peg to capture it.\rLeave only one to win the game.";
	if (isFr) {
	laregle = "Sautez par-dessus la voisine pour la capturer.\rNe laissez qu\'une boule pour gagner la partie.";
	}


var lejeu = '<table width="360" border="0" cellpadding="0" cellspacing="0">'
+ '<tr align="center">'
+ '<td colspan="9"><img src="solitaire/s_01.jpg" width="360" height="40" alt="" /><\/td>'
+ '<\/tr><tr>'
+ '<td rowspan="7"><img src="solitaire/s_02.jpg" width="40" height="280" alt="" /><\/td>'
+ '<td colspan="2"><img src="solitaire/s_03.jpg" width="80" height="40" alt="" /><\/td>'
+ boite("13",0)
+ boite("14",1)
+ boite("15",1)
+ '<td colspan="2"><img src="solitaire/s_04.jpg" width="80" height="40" alt="" /><\/td>'
+ '<td rowspan="7"><img src="solitaire/s_05.jpg" width="40" height="280" alt=""/><\/td>'
+ '<\/tr><tr>'
+ '<td><img src="solitaire/s_06.jpg" width="40" height="40" alt=""/><\/td>'
+ boite("22",1)
+ boite("23",1)
+ boite("24",1)
+ boite("25",1)
+ boite("26",1)
+ '<td><img src="solitaire/s_07.jpg" width="40" height="40" alt=""/><\/td>'
+ '<\/tr><tr>'
+ boite("31",1)
+ boite("32",1)
+ boite("33",1)
+ boite("34",1)
+ boite("35",1)
+ boite("36",1)
+ boite("37",1)
+ '<\/tr><tr>'
+ boite("41",1)
+ boite("42",1)
+ boite("43",1)
+ boite("44",1)
+ boite("45",1)
+ boite("46",1)
+ boite("47",1)
+ '<\/tr><tr>'
+ boite("51",1)
+ boite("52",1)
+ boite("53",1)
+ boite("54",1)
+ boite("55",1)
+ boite("56",1)
+ boite("57",1)
+ '<\/tr><tr>'
+ '<td><img src="solitaire/s_08.jpg" width="40" height="40" alt="" /><\/td>'
+ boite("62",1)
+ boite("63",1)
+ boite("64",1)
+ boite("65",1)
+ boite("66",1)
+ '<td><img src="solitaire/s_09.jpg" width="40" height="40" alt="" /><\/td>'
+ '<\/tr><tr>'
+ '<td colspan="2"><img src="solitaire/s_10.jpg" width="80" height="40" alt="" /><\/td>'
+ boite("73",1)
+ boite("74",1)
+ boite("75",1)
+ '<td colspan="2"><img src="solitaire/s_11.jpg" width="80" height="40" alt="" /><\/td>'
+ '<\/tr><tr>'
+ '<td colspan="9"><img src="solitaire/s_12.jpg" width="360" height="40" alt="" /><\/td>'
+ '<\/tr><\/table>';

function jeclick(leNom)
{
var questions = 0;
var coupPossible = 0;
var haut = 0;
var bas = 0;
var gauche = 0;
var droite = 0;
var moins2 = leNom-2;
var moins2Rech = "|"+moins2+"|";
var plus2 = leNom+2;
var moins20 = leNom-20;
var plus20 = leNom+20;
var moins1 = leNom-1;
var plus1 = leNom+1;
var moins10 = leNom-10;
var plus10 = leNom+10;
var imageAutre = "";
var boulesRouges = 0;

	// y a-t-il des questions ?
	for (var j=0; j< boule.length; j++)
	{
	var laBouleEnQuestion = boule[j];
			
		if (document[laBouleEnQuestion].src == image3.src)
		{
		questions++;
		}
	}
	
	// il a des questions mais
	if (questions != 0)
	{
		// on n'a pas clique sur la question
		if (document[leNom].src != image3.src)
		{
		var destination = "Choose your destination!";
			if (isFr) {destination = "Choisissez votre destination !";}
		document.f.allo.value = destination;
		}
		// on a clique sur la question
		else
		{
		var choix = leNom + ((document.f.leCoupJaune.value - leNom) / 2);
		document[choix].src = image0.src;
		document[leNom].src = image1.src;
		document[document.f.leCoupJaune.value].src = image0.src;
		
			for (j=0; j< boule.length; j++)
			{
			laBouleEnQuestion = boule[j];
				
				if (document[laBouleEnQuestion].src == image3.src)
				{
				document[laBouleEnQuestion].src = image0.src;
				document.f.allo.value = laregle;
				}
			}
		}
	}
	
	// pas de question
	else
	{
		// on a clique sur un trou
		if (document[leNom].src == image0.src)
		{
		var bouletxt = "Click on a peg!";
			if (isFr) {bouletxt = "Cliquez sur une boule !";}
		document.f.allo.value = bouletxt;
		}
		// on a clique sur une boule rouge
		else
		{
			if (document[leNom].src == image1.src)
			{
				if (bouleBarre.indexOf(moins2Rech) != -1)
				{
					if (document[moins2].src == image0.src && document[moins1].src == image1.src)
					{
					coupPossible++;
					gauche++;
					}
				}
				if (bouleBarre.indexOf("|"+plus2+"|") != -1)
				{
					if (document[plus2].src == image0.src && document[plus1].src == image1.src)
					{
					coupPossible++;
					droite++;
					}
				}
				if (bouleBarre.indexOf("|"+moins20+"|") != -1)
				{
					if (document[moins20].src == image0.src && document[moins10].src == image1.src)
					{
					coupPossible++;
					haut++;
					}
				}
				if (bouleBarre.indexOf("|"+plus20+"|") != -1)
				{
					if (document[plus20].src == image0.src && document[plus10].src == image1.src)
					{
					coupPossible++;
					bas++;
					}
				}
		
				if (coupPossible == 0)
				{
				var coupimpossible = "Incorrect move!";
					if (isFr) {coupimpossible = "Coup impossible !";}
				document.f.allo.value = coupimpossible;
				}
			}
			
		
		
			if (coupPossible == 1)
			{
			document[leNom].src = image0.src;
				if (gauche == 1)
				{
				document[moins2].src = image1.src;
				document[moins1].src = image0.src;
				}
				if (droite == 1)
				{
				document[plus2].src = image1.src;
				document[plus1].src = image0.src;
				}
				if (haut == 1)
				{
				document[moins20].src = image1.src;
				document[moins10].src = image0.src;
				}
				if (bas == 1)
				{
				document[plus20].src = image1.src;
				document[plus10].src = image0.src;
				}	
			document.f.allo.value = laregle;
			}
			
			if (coupPossible > 1)
			{
			document[leNom].src = image2.src;
			document.f.leCoupJaune.value = leNom;
				if (gauche == 1)
				{
				document[moins2].src = image3.src;
				}
				if (droite == 1)
				{
				document[plus2].src = image3.src;
				}
				if (haut == 1)
				{
				document[moins20].src = image3.src;
				}
				if (bas == 1)
				{
				document[plus20].src = image3.src;
				}
			}			
		} // click sur rouge
	} // pas de question

	// combien de boules rouges ?
	for (j=0; j< boule.length; j++)
	{
	var laBouleEnQuestion2 = boule[j];
		
		if (document[laBouleEnQuestion2].src == image1.src)
		{
		boulesRouges++;
		}
	}
	if (boulesRouges == 1)
	{
	var gagnetxt = "Congratulations! You won the game!";
		if (isFr) {gagnetxt = "Bravo ! Vous gagnez la partie !";}
	document.f.allo.value = gagnetxt;
	}
}


// solution
var depart = new Array ("15","34","26","14","45","37","57","34","37","25","45","66","47","32","13","34","55","43","35","41","22","43","54","75","56","63","51","31","54","51","43","74","62","54","73");

var milieu = new Array ("14","24","25","24","35","36","47","35","36","35","46","56","46","33","23","33","45","44","45","42","32","42","55","65","55","53","52","41","53","52","53","64","63","64","74");

var arrivee = new Array ("13","14","24","34","25","35","37","36","35","45","47","46","45","34","33","32","35","45","55","43","42","41","56","55","54","43","53","51","52","53","63","54","64","74","75");

var a = 0;
var foix = depart.length;

function solution()
{
	if (!a)
	{
		for (var i=0; i<boule.length; i++)
		{
			if (boule[i] == "13")
			{
			document[boule[i]].src = image0.src;
			}
			else
			{
			document[boule[i]].src = image1.src;
			}
		}
	}
	if (a <= foix)
	{
		if (a)
		{
		document[depart[a-1]].src = image0.src;
		document[milieu[a-1]].src = image0.src;
		document[arrivee[a-1]].src = image1.src;		
		}
	a++;
	document.les_foix.nb_foix.value = a;
	}
	
	if (a > foix)
	{
	clearInterval(solveRunning);
	var facile = "That's it!\nTry it yourself.";
		if (isFr) {facile = "Facile n'est-ce pas ?\nA vous d'essayer.";}
	document.f.allo.value = facile;
	document.les_foix.nb_foix.value = 0;
	window.location.reload();
	}
}

function solve()
{
solveRunning=setInterval('solution()',1200);
}
