// ========================= Copyright Epistema 2002 ========================
//
// GlossaireEngine
//
// Finds all words in a page and highlights them
//
// $Revision: 743 $
//
// $Log: GlossaireEngine.js,v $
// Revision 1.2  2002/12/05 17:24:52  bertrand
// Added the CVS header
//
// ==========================================================================

function _Quizz_Manager_RenderNode(unMot, aNode, doc)
{
	var oNodeChildren = aNode.childNodes;

  if (aNode.tagName == "A")
    return;

	if (oNodeChildren != null)
	{
		if (oNodeChildren.length == 0)
		{
			// process the node:

			var strText = aNode.nodeValue;
			if (strText == null)
			  return;

			strText = strText.toLowerCase();

			if (strText != null)
			{
					var pos = strText.indexOf(unMot.Mot.toLowerCase());
					var l = unMot.Mot.length;

					if (pos >= 0)
					{
 						// teste si le mot est seul:
 						if (pos > 0)
 						{
 							var c = strText.charAt(pos - 1);
 							c.toLowerCase();

 							if (c >= "a" && c <= "z")
 								return;
 						}

 						if (pos + l < strText.length)
 						{
 							var c = strText.charAt(pos + l);
 							c.toLowerCase();

 							if (c >= "a" && c <= "z")
 							  if (c != "s")
 								   return;
 						}

						var oReplacedTextNode = aNode.splitText(pos);

						oReplacedTextNode.splitText(l);

						var strReplacedText = oReplacedTextNode.nodeValue;

						var newLink = doc.createElement("a");

						newLink.className = "glossaireWord";
						newLink.href = "_glossaire.htm#"+unMot.Mot;
						newLink.target = "glossaire";
						newLink.title = "Voir dans le glossaire";
						newLink.innerHTML = strReplacedText;
						newLink.onclick = ShowGlossaire;

						aNode.parentNode.replaceChild(newLink, oReplacedTextNode);
					}
		  }

		  return;
		}

		for (var j=0; j<oNodeChildren.length; j++)
			this.RenderNode(unMot, oNodeChildren.item(j), doc);
	}

  return;
}


function _Quizz_Manager_renderKeyWord()
{
	var frm = window.parent.frames;
	for (frameIndex=0; frameIndex < frm.length; frameIndex++)
	{
		if (frm[frameIndex].name == "mainframe")
		{
	    var oDoc = frm[frameIndex].document;

			var oChildren = oDoc.getElementById("EpistudioContentId");

			if (oChildren != null)
			{
				for (var k=0; k<this.mots.length; k++)
				  this.RenderNode(this.mots[k], oChildren, frm[frameIndex].document);
			}
			else
			{
				// EpistudioContentId is not found, maybe this is an old skin:

				oChildren = oDoc.body.childNodes;

				if (oChildren != null)
				{
					for (var k=0; k<this.mots.length; k++)
					{
						for (var i=0; i<oChildren.length; i++)
						{
							this.RenderNode(this.mots[k], oChildren.item(i), frm[frameIndex].document);
						}
					}
				}
			}
		}
	}


}
