insertStyleSheet("/lls/assets/styles/showlinks.css");
var evt = new universalEvent();
var numSelects,numCorrect=0;
var selAns=document.getElementById("select-answers").firstChild.nodeValue.split("|");

function its_evaluate(e) {

	var tmp,currentSentence,i;
	evt.refresh(e);

	if (evt.evtSrcId.indexOf("sans-") == -1 &&  evt.evtSrcId.indexOf("sss-") == -1) return false;
	else {
		tmp=evt.evtSrc.parentNode;

		for (i=0;i<tmp.childNodes.length-1;i++)
		{
				//alert (tmp.childNodes[i].id) ;
				if ((tmp.childNodes[i].id)) tmp.childNodes[i].className = "";
				//tmp.childNodes[i].style.background = '#FFFFFF';
		}

		evt.evtSrc.className="highlightactive";
		//evt.evtSrc.style.background =	'#FCFF64';
		
		while (tmp.id.indexOf("sentence") != 0) tmp = tmp.parentNode;
		currentSentence = parseInt(tmp.id.substr(tmp.id.lastIndexOf("-")+1));
		tmp = document.getElementById("its-fb-img-" + currentSentence);
		tmp.src = (evt.evtSrcId.indexOf("sans-") == 0) ? correct.src : incorrect.src;
	}
}
function txtSelectShowAnswer(e)
{
	var n=0; // the first sentence is done seperately since in some pages this is set as an example by default... 
	if (txtCorrectSelChunks = document.getElementById("sans-"+(++n)))
	{
		txtCorrectSelChunks.className = "highlightactive";	
		//txtCorrectSelChunks.style.background = "#FFFFFF";

		if (tickImg = document.getElementById("its-fb-img-"+n))
			tickImg.src = "assets/images/correct.gif";  	
	}
	var n=1;
	while (txtCorrectSelChunks = document.getElementById("sans-"+(++n)))
	{
		txtCorrectSelChunks.className = "highlightactive";	
		//txtCorrectSelChunks.style.background = "#FFFFFF";

		if (tickImg = document.getElementById("its-fb-img-"+n))
			tickImg.src = "assets/images/correct.gif";  	
	}
}
function txtSelectClearAnswer(e)
{
	if (txtSel = document.getElementById('int-txtSelect'))	
	{
		var n=0;
		while (txtSelChunks = document.getElementById("sss-"+(++n)))
		{
			//alert (n);
			txtSelChunks.className = "optionChunksNormal";	
			//txtSelChunks.style.background = "#FFFFFF";
			
		}
		// special case for the first sentence since it differs in may pages [as example and as exercise sentence]
		var n=0; //
		if (txtCorrectSelChunks = document.getElementById("sans-"+(++n)))
		{
			txtCorrectSelChunks.className = "optionChunksNormal";	
			//txtCorrectSelChunks.style.background = "#FFFFFF";

			if (tickImg = document.getElementById("its-fb-img-"+n))
				tickImg.src = "assets/images/spacer.gif";  	
		}
		
		var n=1; //n=1 since the first correct ans in the example is always highlighted
		while (txtCorrectSelChunks = document.getElementById("sans-"+(++n)))
		{
			txtCorrectSelChunks.className = "optionChunksNormal";	
			//txtCorrectSelChunks.style.background = "#FFFFFF";

			if (tickImg = document.getElementById("its-fb-img-"+n))
				tickImg.src = "assets/images/spacer.gif";  	
		}
		
	}
}
function changestyle(e) {

	evt.refresh(e);
	curElementId	=	evt.evtSrc.id;
	curElement		=	document.getElementById(curElementId);
	if ((curElement) && (curElement.className!="highlightactive")) {
		//curElement.style.background = '#FCFF64';
		curElement.className = "optionChunks";
		// specifying the className does not work here...
		//alert ('In-chg');
	}
}
function revertstyle(e) {
	evt.refresh(e);
	curElementId	=	evt.evtSrc.id;
	curElement		=	document.getElementById(curElementId);
	if ((curElement) && (curElement.className!="highlightactive")) {
		//curElement.style.background = '#FFFFFF';
			curElement.className = "optionChunksNormal";
		// specifying the className does not work here...
		//alert ('In-revert');
	}
}
var intDiv,intDivCpy;
function init() {
	var cs,st,p1,p2,ts,tmp,currentSentence;
	if (intDiv=document.getElementById("int-txtSelect")) {
		var spans=intDiv.getElementsByTagName("span"),img,sCount=0;
		for (var i=0;i<spans.length;i++) {
			if (spans[i].id.indexOf("sentence") == 0) {
				currentSentence = parseInt(spans[i].id.substr(spans[i].id.lastIndexOf("-")+1));
				//alert (currentSentence);
				cs=spans[i]
				st = cs.firstChild.nodeValue.toString();
				p1=st.indexOf(selAns[currentSentence-1]);
				p2=p1+selAns[currentSentence-1].length;
				cs.removeChild(cs.firstChild);
				cs.appendChild(document.createTextNode(" "));
				tmp = st.substr(p2).split(" ");
				for (j=tmp.length-1;j>=0;j--) {
					cs.insertBefore(document.createTextNode(" "),cs.firstChild);
					ts=document.createElement("span");
					ts.id = "sss-"+(++sCount);
					ts.onmouseover 	=	changestyle;
					ts.onmouseout	=	revertstyle;
					//ts.style.background = "#ffccff";
					ts.appendChild(document.createTextNode(tmp[j]));
					cs.insertBefore(ts,cs.firstChild);
				}
				ts=document.createElement("span");
				ts.appendChild(document.createTextNode(st.substring(p1,p2)));
				ts.id="sans-"+currentSentence;
				ts.onmouseover =	changestyle;
				ts.onmouseout	 =	revertstyle;
				//ts.style.background = "#ffccff";
				cs.insertBefore(ts,cs.firstChild);
				tmp = st.substr(0,p1).split(" ");
				for (j=tmp.length-1;j>=0;j--) {
					cs.insertBefore(document.createTextNode(" "),cs.firstChild);
					ts=document.createElement("span");
					ts.id = "sss-"+(++sCount);
					ts.onmouseover =	changestyle;
					ts.onmouseout	 =	revertstyle;
					//ts.style.background = "#ffccff";
					ts.appendChild(document.createTextNode(tmp[j]));
					cs.insertBefore(ts,cs.firstChild);
				}
				
				img = spans[i].appendChild(document.createElement("img"));
				img.width=25;
				img.height=25;
				img.src="assets/images/spacer.gif";
				img.id="its-fb-img-"+currentSentence;
				//alert (spans[i].id);
				spans[i].style.cursor="pointer";
				spans[i].onclick=its_evaluate;
				
			}
		}
	}
	
	if (cBut=document.getElementById("txtselect-but-clear"))
	{
		var i =0;
		e=document.createElement("input");
		e.setAttribute("type","button");
		e.setAttribute("class","response");
		e.setAttribute("value","Show answers");
		e.setAttribute("id","txtSelect-showanswer-button-"+(i));
		e.onclick=txtSelectShowAnswer;
		cBut.appendChild(e);
		
		var i =0;
		e=document.createElement("input");
		e.setAttribute("type","button");
		e.setAttribute("class","response");
		e.setAttribute("value","Clear answers");
		e.setAttribute("id","txtSelect-clearanswer-button-"+(i));
		e.onclick=txtSelectClearAnswer;
		cBut.appendChild(e);
	}
}
init();