/*(c) 2001-2007 Kinetic Books Company */
//DerivationTable.js MUST BE PLACED BEFORE DERIVATION0x.JS FILES.
dNarr = new Array();
drvheadline = new Array();
drvNarration = new Array();
drvStepBreak = new Array();
drvExplanation = new Array();
st = 0;
drvNum = 0;
drvRowNum = 0;
imgwidth = 0;
agt=navigator.userAgent.toLowerCase();
SF = ((agt.indexOf('safari')!=-1));//&&(parseFloat(agt.substr(agt.lastIndexOf("/")+1))<100))?true:false;
ESF = (SF&&(parseFloat(agt.substr(agt.lastIndexOf("/")+1))<100));

if (typeof(dtype)=='undefined')dtype = 1;

function SetDerivationTable(Example){

	for(b=0;b<drvNarration.length;b++){
		for(a = 0; a < drvNarration[b].length; a++){
			//this makes the infinity symbol a larger size in the narration

			drvNarration[b][a] = drvNarration[b][a].replace(/&#8734;/g,"<span style=\"font-size:12 pt;\">&#8734;</span>");
			drvNarration[b][a] = drvNarration[b][a].replace(/&#8211;/g,"&minus;");
			}
		}
midpoint = (Example)?255:180;

i = 2;
total=drvNarration[drvNum].length;
tableWidth = (dtype!=2)?(dtype==1&&Example)?525:435:700;
divWidth =(Example)?170:265;
for(k = 0; k < drvStepBreak[drvNum].length;k++){
	total -= drvStepBreak[drvNum][k];
	}
	drvStepBreak[drvNum][drvStepBreak[drvNum].length] = total;

tmptxt = /=TEMPWIDTH=/g
	Derivations="";
	if(dtype!=3){
		Derivations+= "</TD>"
		Derivations+="<TD class=\"SectionTableCol2\">&nbsp;</TD>";
		Derivations+="<TD class=\""+((Example)?"drvNarrCol Thin":"SectionTableCol3")+"\"><sc"+"ript language=\"javascript\">writeImageCol();</scr"+"ipt></TD>";
		Derivations+="</TR>";
		Derivations+="<TR>";
		Derivations+="<TD class=\"DerivationCol\"";
		Derivations+=(dtype==2)?" colspan=\"3\">":">";
		}
	var nonbsp = /&nbsp\;/g;
	for(a = 0; a<drvExplanation[drvNum].length;a++){
			dNarr[a] = true;
			narrDiv = "<div id=\"narrDiv"+drvNum+""+a+"\" class=\"narrDiv "+((Example)?"Thin":"Fat")+"\">"+((dtype!=1)?"<table border=\"0\" width=\""+(tableWidth-28)+"\" cellpadding=\"0\" cellspacing=\"0\"><tr><td>":"")+"<p class=\"DerivationNarration\" id=\"narrtext"+drvNum+""+a+"\" align=\"left\">&nbsp;&nbsp;</p>"+((dtype!=1)?"</td><td style=\"width:15px\" valign=\"top\"><a href=\"javascript:clearDerivation("+drvNum+")\"><img src=\"../../media/chapters/derivations/minus.gif\" border=\"0\" align=\"right\"></a></td></tr></table>":"")+"</div>";
			Derivations +=(drvExplanation[drvNum][a]!="")?"<p class=\"SectionBody\">":"";

			Derivations += drvExplanation[drvNum][a];
			if(drvExplanation[drvNum][a]!=""){
				Derivations+=(drvExplanation[drvNum][a]!="")?"</p>":"";
				if(dtype!=3){
					Derivations+="</TD>";
					if(dtype!=2 || a == 0){
						Derivations+=" 	<TD class=\"SectionTableCol2\">&#160;</TD>";
						Derivations+="	   <TD class=\"drvNarrCol"
						Derivations+=(Example)?" Thin\"":" Fat\"";
						Derivations+=">&nbsp;";
						Derivations+="</TD>";
						}
					Derivations +="</TR>";
					Derivations+="	   <TR>";
					Derivations+="	<TD class=\"DerivationCol\"";
					Derivations+=(dtype==2)?" colspan=\"3\">":">";
					}
				}
			Derivations+="<table  border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"DerivationTable\" style=\"width:"+tableWidth+"px;\">";
			Derivations+="<tr>" ;
			Derivations+="<td valign=\"top\"  class=\"DerivationTableHead\" colspan=\"2\">" + drvheadline[drvNum][0] +"</td>";
			Derivations+="</td>" ;
			Derivations+="<td valign=\"top\" class=\"DerivationTableHead\">" + drvheadline[drvNum][1] +"</td>";
			Derivations+="</td></tr>" ;

			for(j=0;j<drvStepBreak[drvNum][st]*2;j++){
				if(i%2==1){
					Derivations+="<td valign=\"top\" class=\"DerivationReason hand\">" + drvheadline[drvNum][i] +"</td>";
					Derivations+="</tr>" ;
					}
				if(i%2==0){
					drvRowNum ++;
					Derivations+="<tr class=\"whitecls\" id=\"step"+(drvRowNum)+"\"  onClick=\"PlayClip("+drvNum+","+(drvRowNum)+");\">";
					if(drvheadline[drvNum][i].indexOf("<img")==-1){
						drvStepNum = drvheadline[drvNum][i].substring(drvheadline[drvNum][i].indexOf("<span class='equation'><b>")+23,drvheadline[drvNum][i].indexOf("</b>")+4);
						drvheadline[drvNum][i] = drvheadline[drvNum][i].replace(drvStepNum,"");
						drvStepNum = drvStepNum.replace(nonbsp,"");
						Derivations+="<td valign=\"top\" class=\"DerivationStep hand\" width=\"20\"><span class='equation'>"+drvStepNum+"</span></td>";
						Derivations+="<td valign=\"top\" class=\"DerivationStep2 hand\" =TEMPWIDTH=>" + drvheadline[drvNum][i] +"</td>";
						}

					else{
						timgwidth = drvheadline[drvNum][i].substring(drvheadline[drvNum][i].indexOf("<img width='")+12,drvheadline[drvNum][i].indexOf("height"));
						timgwidth = parseFloat(timgwidth);

						if (timgwidth > imgwidth && timgwidth > midpoint) imgwidth = timgwidth;
						drvStepNum = drvheadline[drvNum][i].substring(drvheadline[drvNum][i].indexOf("<span class='equation'><b>")+23,drvheadline[drvNum][i].indexOf("</b>")+4);
						drvheadline[drvNum][i] = drvheadline[drvNum][i].replace(drvStepNum,"");
						drvStepNum = drvStepNum.replace(nonbsp,"");
						Derivations+="<td colspan=\"2\" valign=\"top\" class=\"DerivationStep3 hand\"><span class='DerivationStepNum'>"+drvStepNum+"</span> " + drvheadline[drvNum][i] +"</td>";
						}
					}
					i++;
				}
			st ++;
			if(Mac&&Derivations.indexOf("<object")==-1) {
				twid = /width=\'(\d){2,3}\'/g;
				theight = /height=\'(\d){2,3}\'/g;
				Derivations = Derivations.replace(twid,"");
				Derivations = Derivations.replace(theight,"style='padding:7px'");
				}
			if(imgwidth > 0){ Derivations = Derivations.replace(tmptxt,"width=\""+imgwidth+"\"");}
			else if (Example) {Derivations = Derivations.replace(tmptxt,"width=\"255\"");}
			else {Derivations = Derivations.replace(tmptxt,"width=\"180\"");}
			imgwidth = 0;


			Derivations+="<tr><td class=\"DerivationTableFoot\">&nbsp;</td><td class=\"DerivationTableFoot\">&nbsp;</td>";
			Derivations+="<td align=\"right\" class=\"DerivationTableFoot\">";
			Derivations+="<a href=\"javascript:Prev("+drvNum+","+a+")\"><img src=\"../../media/chapters/derivations/back.gif\" border=\"0\"></a>&#160;";
			Derivations+="<a href=\"javascript:Next("+drvNum+","+a+")\"><img border=\"0\" src=\"../../media/chapters/derivations/fwd.gif\"></a>";
			Derivations+="</td>";
			Derivations+="</tr>";
			Derivations+="</table>";


			if(dtype != 1){Derivations += narrDiv;}
			if(dtype!=3){
				Derivations+="</TD>";
				if(dtype==1){
					Derivations+=" 	<TD class=\"SectionTableCol2\" bgcolor=\"#FF0000\">&#160;</TD>";
					Derivations+="	   <TD id=\"drvleftcol"+drvNum+""+a+"\"  class=\"drvNarrCol";
					Derivations+=(Example)?" Thin":" Fat";
					Derivations+=" Narr\" >"+narrDiv+"</TD>";
					}
				Derivations+="</TR>";
				Derivations+="	   <TR>";
				Derivations+="	<TD valign=\"top\" class=\"DerivationCol\"";
				Derivations+=(dtype==2 && a<drvStepBreak[drvNum].length - 1)?" colspan=\"3\">":">";
				}
		}
		document.write(Derivations);
		drvNum++;
		st = 0;
	}
stepNum=0;
function PlayClipMML(dnum,rownum){
rownum = parseFloat(rownum);
dnum = parseFloat(dnum);
	for(i=0;i<dnum;i++){
		rownum += drvNarration[i].length;
	}
	PlayClip(dnum,rownum);
	}
function PlayClip(DerivNum,Num){
	if(document.getElementById("step"+Num).className == "linencls"){
		clearDerivation(DerivNum);
		return;
		}
	stepNum=Num;
	setNarr(DerivNum);
	setBorders(stepNum,DerivNum);
	}
function setHeight(elm,parenelm){
//	parenelm.style.height = 'auto';
//	var x = parenelm.offsetHeight;
//	elm.style.overflow = "auto";

//	elm.style.height = (x) + "px";
	}
function Next(curDer,block){
	total = 0;
	for(m = 0; m < curDer;m++){
		total += drvNarration[m].length;
		}
	for(m = 0; m <= block;m++){
		total += drvStepBreak[curDer][m];
		}
	if(block!= 0 && (stepNum > total || stepNum <= total-drvStepBreak[curDer][block])){
		stepNum = total - drvStepBreak[curDer][block];
		}
	else if (block == 0 && stepNum > total && curDer == 0){
		stepNum = (curDer > 0)?drvNarration[curDer-1].length:0;
		}
	else if(block == 0 && curDer > 0 && stepNum < total-drvStepBreak[curDer][block]){
		stepNum = total - drvStepBreak[curDer][block];
		}
	if(block==0 && stepNum > total) stepNum = total - drvStepBreak[curDer][block];
	if(stepNum!=total||(block!=0 && stepNum < drvStepBreak[curDer][block-1]) ){
		stepNum++;
		setNarr(curDer);
		setBorders(stepNum,curDer);
		}
	else if (stepNum == total){
		clearDerivation(curDer);
		}
	}



function Prev(curDer,block){
	total = 0;
	for(m = 0; m < curDer;m++){
		total += drvNarration[m].length;
		}
	for(m = 0; m < block;m++){
		total += drvStepBreak[curDer][m];
		}
	if(stepNum > total+drvStepBreak[curDer][block] || stepNum <= total || stepNum == 0){
		stepNum = total+drvStepBreak[curDer][block]+1;
		}
	stepNum--;
	if(stepNum>0){
		if(stepNum == total) clearDerivation(curDer)
		else{
			setNarr(curDer);
			setBorders(stepNum,curDer);
			}
		}
	else clearDerivation(curDer);
	}

function setBorders(rowNum,curDer){
	mnum = 0;
	mml = rowNum;
	if(curDer > 0){
		for(i=0;i<curDer;i++){
			mnum += drvNarration[i].length;
			mml -= drvNarration[i].length;
			}
		}
	for(i = 1; i <= drvNarration[curDer].length;i++){
		ll = document.getElementById("step"+(mnum+i));
		ll.className = "whitecls";
		if(document.getElementById("divd"+(i)+"n"+curDer)&&document.getElementById("divd"+(i)+"n"+curDer).className=='linencls'){
			document.getElementById("divd"+(i)+"n"+curDer).className='whitecls';
			var obj = (SF)?eval('d'+(i)+'n'+curDer):eval('document.d'+(i)+'n'+curDer);
		if(obj.bgcolor){
			obj.bgcolor="0xFFFFFF";
			}
		else{
			var cwd = parseInt(obj.width);
			var nwd = cwd + 1;
			obj.width = nwd;
			}
		}
	}

	l = document.getElementById("step"+rowNum);
	l.className = "linencls";
	if(document.getElementById("divd"+mml+"n"+curDer)){
		document.getElementById("divd"+mml+"n"+curDer).className='linencls';
		var obj = (SF)?eval("d"+mml+"n"+curDer):eval("document.d"+mml+"n"+curDer);
	if(obj.bgcolor){
			obj.bgcolor="0xF9F89E";
			}
		else{
			var cwd = parseInt(obj.width);
			var nwd = cwd - 1;
			obj.width = nwd;
			}
		}

	}

function setNarr(NumDer){
	for(i=1;i<=drvStepBreak[NumDer].length;i++){
		document.getElementById("narrtext"+NumDer+""+(i-1)).innerHTML = "&#160;";
		document.getElementById("narrtext"+NumDer+""+(i-1)).style.fontSize="";
		document.getElementById("narrtext"+NumDer+""+(i-1)).style.lineHeight="";
		document.getElementById("narrDiv"+NumDer+""+(i-1)).style.visibility="hidden";
		}
	narrPlace = 0;
		total = 0
	if(NumDer != 0){
		for(i = 0; i < NumDer;i++){
		total += drvNarration[i].length;
			}
		}
	for(i = 0; i < drvStepBreak[NumDer].length; i++){
		if(stepNum <= drvStepBreak[NumDer][i]+total){
			narrPlace = i;
			break;
			}
		else if(stepNum > drvStepBreak[NumDer][i]+total){
			narrPlace = drvStepBreak[NumDer].length;
			}
		total += drvStepBreak[NumDer][i];
		}
	if(dNarr[narrPlace]){
		document.getElementById("narrDiv"+NumDer+""+narrPlace).style.visibility="visible";
		narration = document.getElementById("narrtext"+NumDer+""+narrPlace);
		if(NumDer==0){
			narrText = stepNum-1}
			else{
				mTotal = 0;
				for(i = 0; i < NumDer; i++){
					mTotal += drvNarration[i].length;
					}
				narrText = stepNum - mTotal -1;
				}
			setHeight(narration,document.getElementById("drvleftcol"+NumDer+""+narrPlace));
			narration.innerHTML = drvNarration[NumDer][narrText];
		}
	}


function clearDerivation(numDer){
	stepNum = 0;
	q = 1;
	while(document.getElementById("step"+q)){
		document.getElementById("step"+q).className="whitecls";
		if(document.getElementById("divd"+q+"n"+numDer) && document.getElementById("divd"+q+"n"+numDer).className=='linencls'){
			document.getElementById("divd"+q+"n"+numDer).className = 'whitecls';
			var obj = (SF)?eval("d"+q+"n"+numDer):eval("window.document.d"+q+"n"+numDer);
			if(obj.bgcolor){
				obj.bgcolor="0xFFFFFF";
				}
			else{
				var cwd = parseInt(obj.width);
				var nwd = cwd + 1;
				obj.width = nwd;
				}
			}
		q++
		}
	for(i = 0; i < drvStepBreak.length;i++){
		for(q = 0;q<drvStepBreak[i].length;q++){
			document.getElementById("narrtext"+i+""+(q)).innerHTML = "&#160;";
			document.getElementById("narrDiv"+i+""+(q)).style.visibility="hidden";
			}
		}
	}


function printNarration(){
	for(var i = 0; i < drvNum; i++){
		clearDerivation(i);
		narrnum = 0;
		leftnarrnum = 0;
		for(m = 0; m < drvStepBreak[i].length;m++){
			bigNarr = "";
			narrnum = (typeof(drvStepBreak[i][m]) == 'undefined')? drvNarration[i].length - leftnarrnum:drvStepBreak[i][m];
			for(j = 0; j < narrnum; j++){
				bigNarr += (leftnarrnum+1)+". &nbsp; ";
				bigNarr += drvNarration[i][leftnarrnum];
				leftnarrnum ++;
				}
			document.getElementById("narrDiv"+i+""+m).style.position="relative";
			document.getElementById("narrDiv"+i+""+m).style.visibility="visible";
			document.getElementById("narrtext"+i+""+m).style.fontSize = "9pt";
			document.getElementById("narrtext"+i+""+m).style.lineHeight = "1";
			document.getElementById("narrtext"+i+""+m).innerHTML = bigNarr;
			}
		}

	}


