
// Navigation Toolbar Core by Ascanio Orlandini - e*[comm]^3
// Version 0.9n Fixed the conditional loading fo css file and NN4 Mac homepage bug


var dad = new String();
// Generating the styles...
var ypos;
document.writeln("<style type=text/css> <!--");

ypos=TOP;
for (i=0; i<MAXL; i++) {
	n4? n4bk="layer-" : n4bk="";
	for (j=0; j<MAXC; j++) {
		document.writeln("#b", i, j, " { ", " position: absolute; top: ", ypos, "px; left: ", LEFT+INDNT*i+BWITH*j, "px; width: ", BWITH, "px; height: ", LHGT[i],"px; clip:rect(0px ", BWITH, "px ", LHGT[i], "px 0px); text-align:center;}");
	}
	ypos+=LHGT[i];	
}

document.writeln("#left1 { ", " position: absolute; top: ", TOP+LHGT[0]-1, "px; left: ", 0, "px; width: ", 30, "px; height: ", 16,"px; clip:rect(0px ", 30, "px ", 16, "px 0px); text-align:center; background:url(", BKGL1, ");}");
document.writeln("#right1 { ", " position: absolute; top: ", TOP+LHGT[0]-1, "px; left: ", LEFT+BWITH*6, "px; width: ", 30, "px; height: ", 16,"px; clip:rect(0px ", 30, "px ", 16, "px 0px); text-align:center; background:url(", BKGR1, ");}");

document.writeln("--></style>");

// Selection of the browser/OS specific CSS - Carlo fixed it to 96dpi

//if ((userAgent.indexOf("win") >0) || (userAgent.indexOf("mac") >0 && userAgent.indexOf("5.") >0)) {
//	alert("96dpi: "+userAgent);
	document.writeln('<link href="' + css96dpi + '" rel="styleSheet" type="text/css">');
//}
//else {
//	alert("72dpi: "+userAgent);
//	document.writeln('<link href="' + css72dpi + '" rel="styleSheet" type="text/css">');
//}



// Code to reload the window if resized, under NN4

function MM_reloadPage(init) {
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);


// Initialization function to associate events
function _setup() {

	//FIX NN4 browsing previous and next cached documents
	if (n4 && document.layers["b"+(MAXL-1)+(MAXC-1)]==null) {
		location.reload()
	}
	else {

	if (n4) for(i=0; i<MAXL; i++) // N4 code only
		for(j=0; j<MAXC; j++) {
			if (document.layers["b"+i+j] == null) location.reload(true); //NN4 fix on the HomePage definition
	 		else {
				document.layers["b"+i+j].captureEvents(Event.MOUSEOVER);
				document.layers["b"+i+j].onmouseover = function(ev) { eventHandler1(ev); }
			}
		}
		
	else if (ie) for(i=0; i<MAXL; i++) // IE only
                for(j=0; j<MAXC; j++) {
                 	document.all["b"+i+j].onmouseover = function() {
                 		window.event.cancelBubble=true;
                 		eventHandler1();	
                 	}
                 	
                 	document.all["b"+i+j].onmousedown = function() {
                 		window.event.cancelBubble=true;
                 		eventHandler2();	
                 	}	
	}

	else if (w3c) for(i=0; i<MAXL; i++) // w3c only / Includes Safari and NN6
                for(j=0; j<MAXC; j++) {

                 	document.getElementById("b"+i+j).onmouseover = function(ev) {
                 		ev.cancelBubble=true;
                 		eventHandler1(ev);	
                 	}

                 	document.getElementById("b"+i+j).onmousedown = function(ev) {
                		ev.cancelBubble=true;
                 		eventHandler2(ev);	
                 	}	
	}

	if(CURR==0) sk();	// IF no CURR defined, search for it
	hlcurr();   		// Set current toolbar selection

	} // END ELSE FIX N4 browsing prev-next

}

function eventHandler1(ev) { // Called by _setup() onmouseover

	var id = new String();

	if (n4 && ev.target.id) id=ev.target.id;
	else if(ie) id=window.event.srcElement.id;	
	else if(w3c) {
		id=ev.target.id;
		if (!id) id = ev.target.parentNode.id;
	}
		
	if (id) {
		l=Number(id.charAt(1));
		c=Number(id.charAt(2));
		cel=t.getCell(l,c);	
		if (cel) _mo(l,c,cel);
	}

}

function eventHandler2(ev) { // Called by _setup() onmousedown (IE and w3g)

	var id = new String();
	if (ie) id=window.event.srcElement.id;
	else if(w3c) id=window.event.srcElement.id;
	if (id) {
		l=Number(id.charAt(1));
		c=Number(id.charAt(2));
		myCell=t.getCell(l,c);
		
		if (myCell) window.location.href = b[t.getCell(l,c)].link;
	}
 }


function sk() { //Get SelfKnowledge!!
	dad=String(window.location); // Current document address and filename
	var lng=String();		 // Current document language
	var i,k;
	dad=dad.slice(dad.lastIndexOf("/")+1);
	(i=dad.indexOf("_"))>0? lng=dad.slice(0,i) : lng="" ;
	dad=dad.slice(dad.indexOf("_")+1);
	if (dad.length==0 ) dad="index.html";	// assumption!
	if (lng.length==0 ) lng="";		
	if (dad == "index.html") lng="e";	// assumption !
//alert("lng:"+lng+" dad:"+dad);	
	lng="e"; // CHANGED by Carlo, language is always English: assumpion!
	for(i=b.length-1; i>0; i--) {
//alert(i);
		if (lng == String(b[i].lang) && lng+"_"+dad == String(b[i].link)) break;
	}
	CURR=b[i].idx;
//alert("i="+i+" b[i].idx="+b[i].idx+" CURR="+CURR);
//	if (CURR==0) alert("It is not possible to find this document ('"+dad+"') language '"+lng+"' in the toolbar definition.");
	for (i=1,k=0; i<b.length && k<MAXC; i++) {   // Filling the Table object for the first line !!
//alert(b[i].lang+" "+lng);
		if (b[i].parent==null && b[i].lang==lng) {
			t.setCell(0,k,b[i].idx);
			k++; // Counts the number of first line buttons find
		}
	
	}
}


function hlcurr() {  // Highlight current page
	var pth = new Array(MAXL);
	var idx;
	var na; //Number of ancestors
	var button;
	idx = CURR;
	for (na=0; na<MAXL && b[idx].parent != null; na++) {
		pth[na]=idx;
		idx=b[idx].parent;
	}
	pth[na]=idx;

	for (; na>=0; na--) {
		button=t.getButton(pth[na]);
		dad=="index.html"? _mo(0, 0, 0) : _mo(Number(button.charAt(1)), Number(button.charAt(2)), pth[na]);
	}
}

// The real main function, called upon event by eventHandler(...)
function _mo(l,c,id) { // l=line number; c=column; id=index of the tree element
//alert ("l:"+l+" c:"+c+" id:"+id);
	var ncb = new Tree(); // For service reasons
        var idx;
        var cn; 	// child number
 	var i, j, k; 	// loop indexes
 	var strt;    	// where to start to render line
	
	var pth = new Array(MAXL);	// this variables for back-higliting
	var na; 			//Number of ancestors
	var button;
	
	if (l>=MAXL) l=MAXL-1; // Should be a bug but have it silently fix it...
	if (c>=MAXC) c=MAXC-1;
		
	if (id==0) { c=-1; l=0; } // EXCEPTION for handling UNKNOWN pages with CURR=0 (see test condition next second clcle for!)

	for(j=0; j<MAXC; j++) { // Highliting current level button (pointed)
		j!=c? col=DEFCL[l] : col=SELCL[l];
		j!=c? img=BKIMG[l][j] : img=SBKIMG[l][j];
	
		if (n4 && document.layers["b"+l+j] == null) location.reload(true); //NN4 fix on the HomePage definition
		else {
			if(n4) document.layers["b"+l+j].bgColor=col;
			else if(ie) document.all["b"+l+j].style.backgroundColor=col;
			else if(w3c) document.getElementById("b"+l+j).style.backgroundColor=col;
			
			if(n4) document.layers["b"+l+j].background.src=img;
			else if(ie) document.all["b"+l+j].style.background = "url("+img+")";
			else if(w3c) document.getElementById("b"+l+j).style.background = "url("+img+")";
						
			for(k=l+1; k<MAXL && k>0; k++) { // k>0 test because exception above
				if (n4 && document.layers["b"+k+j] == null) location.reload(true); //NN fix on the HomePage definition
		 		else {
					if(n4) document.layers["b"+k+j].bgColor=DEFCL[k];
					else if(ie) document.all["b"+k+j].style.backgroundColor=DEFCL[k];
					else if(w3c) document.getElementById("b"+k+j).style.backgroundColor=DEFCL[k];
					
					if(n4) document.layers["b"+k+j].background.src=BKIMG[k][j];
					else if(ie) document.all["b"+k+j].style.background = "url("+BKIMG[k][j]+")";
					else if(w3c) document.getElementById("b"+k+j).style.background = "url("+BKIMG[k][j]+")";
				}
			}
		}
	}



 	if (l<MAXL-1) { // If we are not working on the last line...
		// Check if current ID tree object has any child, if yes, show all of them!
		for(ncb = b[b[id].child], cn=0, k=id; ncb!=null; ncb=b[ncb.next], cn++); // Counts the number of childs in cn
		ncb = b[b[id].child]; 			// Next child button object
		MAXC-c<cn? strt=MAXC-cn : strt=c;  	// Where to stant placing childs?
		for(j=0; j<MAXC; j++) { 		// rendering next lines
               	       	(j<strt || ncb==null)? idx=0 : idx=ncb.idx; // If before start or no child show empty cell, else show it
        		writeBody("b"+(l+1)+j, idx, l+1, j);
			if (ncb!=null && j>=strt) ncb=b[ncb.next];
			for(k=l+2; k<MAXL; k++)
				writeBody("b"+k+j, 0, k,j);
		}
	}

	for(i=0; i<MAXL && !n4 && CLEARNU; i++)
		for(j=0; j<MAXC; j++)
				if (t.getCell(i,j)==0) {
// I am not clearing the background for n4 because it cames out bad
//					n4? document.layers["b"+i+j].bgColor=null : document.all["b"+i+j].style.backgroundColor="";
//					n4? document.layers["b"+i+j].bakground.src="" : document.all["b"+i+j].style.background = "";
    	           			if (ie) {
						document.all["b"+i+j].style.backgroundColor="ffffff";
						document.all["b"+i+j].style.background = "";
					}
    	           			else if (w3c) {
						document.getElementById("b"+i+j).style.backgroundColor="ffffff";
						document.getElementById("b"+i+j).style.background = "url("+EBKIMG+")";
					}
                		}

                		
	// back highliting					

	if (BH) {
		for (na=0; na<MAXL && b[id].parent != null; na++) {
			pth[na]=id;
			id=b[id].parent;
		}
		pth[na]=id;
		for (; na>=0; na--) {
			button=t.getButton(pth[na]);
			if(n4) document.layers[button].bgColor=SELCL[l];
			else if(ie) document.all[button].style.backgroundColor=SELCL[l];
			else if(w3c)  document.getElementById[button].style.backgroundColor=SELCL[l];
			
			if(n4) document.layers[button].background.src=SBKIMG[l][c];
			else if(ie) document.all[button].style.background = "url("+SBKIMG[l][c]+")";
			else if(w3c) document.getElementById(button).style.background = "url("+SBKIMG[l][c]+")";
		}
	}	               		

                		
                		                		
}	

function writeBody(id, idx, l, j) { //id=layer/object ID, idx=the button object index, l,j= cell position (line and coloumn)
 	if (n4) {
		if (document.layers[id] == null) location.reload(true);  // Fix for NN4 Home lcation bug
		else {
			document.layers[id].document.write("<table border=0 cellspacing=0 cellpadding=0 width=100><td align=center><a href="+b[idx].link+" class=ntbl"+(l+1)+">"+b[idx].data+"</a></td></table>");
			document.layers[id].document.close();
		}
	}
 	else if (ie)
 		document.all[id].innerHTML = "<a href="+b[idx].link+" class=ntbl"+(l+1)+" ID=l"+l+j+">"+b[idx].data+"</a>";
 	else if (w3c)
 		document.getElementById(id).innerHTML = "<a href="+b[idx].link+" class=ntbl"+(l+1)+" ID=l"+l+j+">"+b[idx].data+"</a>";
 	
	t.setCell(l, j, idx);
}


